aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/gcc.dg/strlenopt-12.c
blob: 1005fc6709e53265f4804384ebfbea7ac6124662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* { dg-do run } */
/* { dg-options "-O2" } */

#include "strlenopt.h"

__attribute__((noinline, noclone)) char *
fn1 (char *p, size_t *l)
{
  char *q = strcat (p, "abcde");
  *l = strlen (p);
  return q;
}

__attribute__((noinline, noclone)) char *
fn2 (char *p, const char *q, size_t *l1, size_t *l2)
{
  size_t l = strlen (q);
  char *r = strcat (p, q);
  *l1 = l;
  *l2 = strlen (p);
  return r;
}

__attribute__((noinline, noclone)) char *
fn3 (char *p, const char *q, size_t *l)
{
  char *r = strcpy (p, q);
  *l = strlen (p);
  return r;
}

__attribute__((noinline, noclone)) char *
fn4 (char *p, const char *q, size_t *l)
{
  char *r = strcat (p, q);
  *l = strlen (p);
  return r;
}

__attribute__((noinline, noclone)) char *
fn5 (char *p, const char *q, size_t *l1, size_t *l2, size_t *l3)
{
  size_t l = strlen (q);
  size_t ll = strlen (p);
  char *r = strcat (p, q);
  *l1 = l;
  *l2 = strlen (p);
  *l3 = ll;
  return r;
}

__attribute__((noinline, noclone)) char *
fn6 (char *p, const char *q, size_t *l1, size_t *l2)
{
  size_t l = strlen (p);
  char *r = strcat (p, q);
  *l1 = strlen (p);
  *l2 = l;
  return r;
}

int
main ()
{
  char buf[64];
  const char *volatile q = "fgh";
  size_t l, l1, l2, l3;
  memset (buf, '\0', sizeof buf);
  memset (buf, 'a', 3);
  if (fn1 (buf, &l) != buf || l != 8 || memcmp (buf, "aaaabcde", 9) != 0)
    abort ();
  if (fn2 (buf, q, &l1, &l2) != buf || l1 != 3 || l2 != 11
      || memcmp (buf, "aaaabcdefgh", 12) != 0)
    abort ();
  if (fn3 (buf, q, &l) != buf || l != 3
      || memcmp (buf, "fgh\0bcdefgh", 12) != 0)
    abort ();
  if (fn4 (buf, q, &l) != buf || l != 6
      || memcmp (buf, "fghfgh\0efgh", 12) != 0)
    abort ();
  l1 = 0;
  l2 = 0;
  if (fn5 (buf, q, &l1, &l2, &l3) != buf || l1 != 3 || l2 != 9 || l3 != 6
      || memcmp (buf, "fghfghfgh\0h", 12) != 0)
    abort ();
  if (fn6 (buf, q, &l1, &l2) != buf || l1 != 12 || l2 != 9
      || memcmp (buf, "fghfghfghfgh", 13) != 0)
    abort ();
  return 0;
}