c-decl.c (finish_decl): When prototype with asmspec is found for built-in...
[gcc.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-asm-1.c
1 /* Copyright (C) 2000, 2003 Free Software Foundation.
2
3 Ensure all expected transformations of builtin strstr occur and
4 perform correctly in presence of redirect. */
5
6 typedef __SIZE_TYPE__ size_t;
7 extern void abort (void);
8 extern char *strstr (const char *, const char *)
9 __asm ("my_strstr");
10 extern char *strchr (const char *, int);
11 extern int strcmp (const char *, const char *);
12 extern int strncmp (const char *, const char *, size_t);
13
14 const char *p = "rld", *q = "hello world";
15
16 int
17 main (void)
18 {
19 const char *const foo = "hello world";
20
21 if (strstr (foo, "") != foo)
22 abort ();
23 if (strstr (foo + 4, "") != foo + 4)
24 abort ();
25 if (strstr (foo, "h") != foo)
26 abort ();
27 if (strstr (foo, "w") != foo + 6)
28 abort ();
29 if (strstr (foo + 6, "o") != foo + 7)
30 abort ();
31 if (strstr (foo + 1, "world") != foo + 6)
32 abort ();
33 if (strstr (foo + 2, p) != foo + 8)
34 abort ();
35 if (strstr (q, "") != q)
36 abort ();
37 if (strstr (q + 1, "o") != q + 4)
38 abort ();
39
40 /* Test at least one instance of the __builtin_ style. We do this
41 to ensure that it works and that the prototype is correct. */
42 if (__builtin_strstr (foo + 1, "world") != foo + 6)
43 abort ();
44
45 return 0;
46 }
47
48 /* There should be no calls to real strstr. */
49 static char *real_strstr (const char *, const char *)
50 __asm ("strstr");
51
52 __attribute__ ((noinline))
53 static char *
54 real_strstr (const char *s1, const char *s2)
55 {
56 abort ();
57 }
58
59 static char *
60 strstr (const char *s1, const char *s2)
61 __asm ("my_strstr");
62
63 __attribute__ ((noinline))
64 static char *
65 strstr (const char *s1, const char *s2)
66 {
67 size_t len = strlen (s2);
68
69 #ifdef __OPTIMIZE__
70 /* If optimizing, we should be called only in the
71 strstr (foo + 2, p) case above. All other cases should
72 be optimized. */
73 if (s2 != p || strcmp (s1, "hello world" + 2) != 0)
74 abort ();
75 #endif
76 if (len == 0)
77 return (char *) s1;
78 for (s1 = strchr (s1, *s2); s1; s1 = strchr (s1 + 1, *s2))
79 if (strncmp (s1, s2, len) == 0)
80 return (char *) s1;
81 return (char *) 0;
82 }