1*0a6a1f1dSLionel Sambuc // RUN: %clang_profgen -DCHECK_SYMBOLS -O3 -o %t.symbols %s
2*0a6a1f1dSLionel Sambuc // RUN: llvm-nm %t.symbols | FileCheck %s --check-prefix=CHECK-SYMBOLS
3*0a6a1f1dSLionel Sambuc // RUN: %clang_profgen -O3 -o %t %s
4*0a6a1f1dSLionel Sambuc // RUN: %run %t %t.profraw
5*0a6a1f1dSLionel Sambuc // RUN: llvm-profdata merge -o %t.profdata %t.profraw
6*0a6a1f1dSLionel Sambuc // RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s
7*0a6a1f1dSLionel Sambuc
8*0a6a1f1dSLionel Sambuc #include <stdint.h>
9*0a6a1f1dSLionel Sambuc #include <stdlib.h>
10*0a6a1f1dSLionel Sambuc
11*0a6a1f1dSLionel Sambuc #ifndef CHECK_SYMBOLS
12*0a6a1f1dSLionel Sambuc #include <stdio.h>
13*0a6a1f1dSLionel Sambuc #endif
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc int __llvm_profile_runtime = 0;
16*0a6a1f1dSLionel Sambuc uint64_t __llvm_profile_get_size_for_buffer(void);
17*0a6a1f1dSLionel Sambuc int __llvm_profile_write_buffer(char *);
18*0a6a1f1dSLionel Sambuc int write_buffer(uint64_t, const char *);
main(int argc,const char * argv[])19*0a6a1f1dSLionel Sambuc int main(int argc, const char *argv[]) {
20*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define i32 @main(
21*0a6a1f1dSLionel Sambuc // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof !1
22*0a6a1f1dSLionel Sambuc if (argc < 2)
23*0a6a1f1dSLionel Sambuc return 1;
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc const uint64_t MaxSize = 10000;
26*0a6a1f1dSLionel Sambuc static char Buffer[MaxSize];
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc uint64_t Size = __llvm_profile_get_size_for_buffer();
29*0a6a1f1dSLionel Sambuc if (Size > MaxSize)
30*0a6a1f1dSLionel Sambuc return 1;
31*0a6a1f1dSLionel Sambuc int Write = __llvm_profile_write_buffer(Buffer);
32*0a6a1f1dSLionel Sambuc if (__llvm_profile_write_buffer(Buffer))
33*0a6a1f1dSLionel Sambuc return Write;
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #ifdef CHECK_SYMBOLS
36*0a6a1f1dSLionel Sambuc // Don't write it out. Since we're checking the symbols, we don't have libc
37*0a6a1f1dSLionel Sambuc // available.
38*0a6a1f1dSLionel Sambuc return 0;
39*0a6a1f1dSLionel Sambuc #else
40*0a6a1f1dSLionel Sambuc // Actually write it out so we can FileCheck the output.
41*0a6a1f1dSLionel Sambuc FILE *File = fopen(argv[1], "w");
42*0a6a1f1dSLionel Sambuc if (!File)
43*0a6a1f1dSLionel Sambuc return 1;
44*0a6a1f1dSLionel Sambuc if (fwrite(Buffer, 1, Size, File) != Size)
45*0a6a1f1dSLionel Sambuc return 1;
46*0a6a1f1dSLionel Sambuc return fclose(File);
47*0a6a1f1dSLionel Sambuc #endif
48*0a6a1f1dSLionel Sambuc }
49*0a6a1f1dSLionel Sambuc // CHECK: !1 = metadata !{metadata !"branch_weights", i32 1, i32 2}
50*0a6a1f1dSLionel Sambuc
51*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: ___cxx_global_var_init
52*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: ___llvm_profile_register_write_file_atexit
53*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: ___llvm_profile_set_filename
54*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: ___llvm_profile_write_file
55*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: _fdopen
56*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: _fopen
57*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: _fwrite
58*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: _getenv
59*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: _malloc
60*0a6a1f1dSLionel Sambuc // CHECK-SYMBOLS-NOT: _open
61