1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "../profile_test.h"
5 
6 int __llvm_profile_runtime = 0;
7 uint64_t __llvm_profile_get_size_for_buffer(void);
8 int __llvm_profile_write_buffer(char *);
9 void __llvm_profile_reset_counters(void);
10 int  __llvm_profile_check_compatibility(const char *, uint64_t);
11 
12 int g = 0;
foo(char c)13 void foo(char c) {
14   if (c == '1')
15     g++;
16   else
17     g--;
18 }
19 
20 extern uint64_t libEntry(char *Buffer, uint64_t MaxSize);
21 
main(int argc,const char * argv[])22 int main(int argc, const char *argv[]) {
23   const uint64_t MaxSize = 10000;
24   static char ALIGNED(sizeof(uint64_t)) Buffer[MaxSize];
25 
26   uint64_t Size = __llvm_profile_get_size_for_buffer();
27   if (Size > MaxSize)
28     return 1;
29 
30   __llvm_profile_reset_counters();
31   foo('0');
32 
33   if (__llvm_profile_write_buffer(Buffer))
34     return 1;
35 
36   /* Now check compatibility. Should return 0.  */
37   if (__llvm_profile_check_compatibility(Buffer, Size))
38     return 1;
39 
40   /* Clear the buffer. */
41   memset(Buffer, 0, MaxSize);
42 
43   /* Collect profile from shared library.  */
44   Size = libEntry(Buffer, MaxSize);
45 
46   if (!Size)
47     return 1;
48 
49   /* Shared library's profile should not match main executable's. */
50   if (!__llvm_profile_check_compatibility(Buffer, Size))
51     return 1;
52 
53   return 0;
54 }
55 
56