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