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