#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ __llvm_profile_merge_from_buffer(char *, scalar_t__);
int /*<<< orphan*/ __llvm_profile_reset_counters();
int /*<<< orphan*/ __llvm_profile_set_filename(char const *);
scalar_t__ __llvm_profile_write_file();
int /*<<< orphan*/ bar();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ foo(int);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ truncate(char const *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  int i;
  if (argc < 2)
    return 1;
  const char *FileN = argv[1];
  __llvm_profile_set_filename(FileN);
  __llvm_profile_reset_counters();
  foo(1);
  if (__llvm_profile_write_file())
    return 1;
  FILE *File = fopen(FileN, "r");
  if (!File)
    return 1;
  fseek(File, 0, SEEK_END);
  uint64_t Size = ftell(File);
  fseek(File, 0, SEEK_SET);
  char *Buffer = (char *)malloc(Size);
  if (Size != fread(Buffer, 1, Size, File))
    return 1;
  fclose(File);
  for (i = 0; i < 10; i++)
    bar();
  __llvm_profile_reset_counters();
  __llvm_profile_merge_from_buffer(Buffer, Size);
  foo(2);
  truncate(FileN, 0);
  if (__llvm_profile_write_file())
    return 1;
  bar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11fc <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x199>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <__llvm_profile_set_filename@plt>
mov    $0x0,%al
call   1070 <__llvm_profile_reset_counters@plt>
mov    $0x1,%edi
call   10b0 <foo@plt>
mov    $0x0,%al
call   10f0 <__llvm_profile_write_file@plt>
cmp    $0x0,%rax
je     123f <main+0x6f>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x199>
mov    -0x20(%rbp),%rdi
lea    0xdb6(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126a <main+0x9a>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x199>
mov    -0x28(%rbp),%rdi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     12e6 <main+0x116>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x199>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    1315 <main+0x145>
mov    $0x0,%al
call   1050 <bar@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12f6 <main+0x126>
mov    $0x0,%al
call   1070 <__llvm_profile_reset_counters@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10d0 <__llvm_profile_merge_from_buffer@plt>
mov    $0x2,%edi
call   10b0 <foo@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1090 <truncate@plt>
mov    $0x0,%al
call   10f0 <__llvm_profile_write_file@plt>
cmp    $0x0,%rax
je     135b <main+0x18b>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x199>
mov    $0x0,%al
call   1050 <bar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x1,%ebp
cmp    $0x2,%edi
jl     1210 <main+0x40>
mov    0x8(%rsi),%r12
mov    %r12,%rdi
call   1030 <__llvm_profile_set_filename@plt>
xor    %eax,%eax
call   1070 <__llvm_profile_reset_counters@plt>
mov    $0x1,%ebp
mov    $0x1,%edi
call   10b0 <foo@plt>
xor    %eax,%eax
call   10f0 <__llvm_profile_write_file@plt>
test   %rax,%rax
je     121b <main+0x4b>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdde(%rip),%rsi
mov    %r12,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1210 <main+0x40>
mov    %rax,%rbx
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r14
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r14,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    $0x1,%ebp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %rax,%r14
jne    1210 <main+0x40>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %eax,%eax
call   1070 <__llvm_profile_reset_counters@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10d0 <__llvm_profile_merge_from_buffer@plt>
mov    $0x2,%edi
call   10b0 <foo@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <truncate@plt>
xor    %eax,%eax
call   10f0 <__llvm_profile_write_file@plt>
test   %rax,%rax
jne    1210 <main+0x40>
xor    %eax,%eax
call   1050 <bar@plt>
xor    %ebp,%ebp
jmp    1210 <main+0x40>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %3
br label %74
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = call i32 @__llvm_profile_set_filename(i8* %18)
%20 = call i32 (...) @__llvm_profile_reset_counters()
%21 = call i32 @foo(i32 1)
%22 = call i64 (...) @__llvm_profile_write_file()
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %14
store i32 1, i32* %3
br label %74
25: ; preds = %14
%26 = load i8*, i8** %7
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp ne i32* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
store i32 1, i32* %3
br label %74
31: ; preds = %25
%32 = load i32*, i32** %8
%33 = load i32, i32* @SEEK_END
%34 = call i32 @fseek(i32* %32, i32 0, i32 %33)
%35 = load i32*, i32** %8
%36 = call i64 @ftell(i32* %35)
store i64 %36, i64* %9
%37 = load i32*, i32** %8
%38 = load i32, i32* @SEEK_SET
%39 = call i32 @fseek(i32* %37, i32 0, i32 %38)
%40 = load i64, i64* %9
%41 = call i64 @malloc(i64 %40)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %10
%43 = load i64, i64* %9
%44 = load i8*, i8** %10
%45 = load i64, i64* %9
%46 = load i32*, i32** %8
%47 = call i64 @fread(i8* %44, i32 1, i64 %45, i32* %46)
%48 = icmp ne i64 %43, %47
br i1 %48, label %49, label %50
49: ; preds = %31
store i32 1, i32* %3
br label %74
50: ; preds = %31
%51 = load i32*, i32** %8
%52 = call i32 @fclose(i32* %51)
store i32 0, i32* %6
br label %53
53: ; preds = %58, %50
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 10
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = call i32 (...) @bar()
br label %58
58: ; preds = %56
%59 = load i32, i32* %6
%60 = add nsw i32 %59, 1
store i32 %60, i32* %6
br label %53
61: ; preds = %53
%62 = call i32 (...) @__llvm_profile_reset_counters()
%63 = load i8*, i8** %10
%64 = load i64, i64* %9
%65 = call i32 @__llvm_profile_merge_from_buffer(i8* %63, i64 %64)
%66 = call i32 @foo(i32 2)
%67 = load i8*, i8** %7
%68 = call i32 @truncate(i8* %67, i32 0)
%69 = call i64 (...) @__llvm_profile_write_file()
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %61
store i32 1, i32* %3
br label %74
72: ; preds = %61
%73 = call i32 (...) @bar()
store i32 0, i32* %3
br label %74
74: ; preds = %72, %71, %49, %30, %24, %13
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %45, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @__llvm_profile_set_filename(i8* %6)
%8 = tail call i32 (...) @__llvm_profile_reset_counters()
%9 = tail call i32 @foo(i32 1)
%10 = tail call i64 (...) @__llvm_profile_write_file()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %45
12: ; preds = %4
%13 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %45, label %15
15: ; preds = %12
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseek(i32* %13, i32 0, i32 %16)
%18 = tail call i64 @ftell(i32* %13)
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %13, i32 0, i32 %19)
%21 = tail call i64 @malloc(i64 %18)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i64 @fread(i8* %22, i32 1, i64 %18, i32* %13)
%24 = icmp eq i64 %18, %23
br i1 %24, label %25, label %45
25: ; preds = %15
%26 = tail call i32 @fclose(i32* %13)
%27 = tail call i32 (...) @bar()
%28 = tail call i32 (...) @bar()
%29 = tail call i32 (...) @bar()
%30 = tail call i32 (...) @bar()
%31 = tail call i32 (...) @bar()
%32 = tail call i32 (...) @bar()
%33 = tail call i32 (...) @bar()
%34 = tail call i32 (...) @bar()
%35 = tail call i32 (...) @bar()
%36 = tail call i32 (...) @bar()
%37 = tail call i32 (...) @__llvm_profile_reset_counters()
%38 = tail call i32 @__llvm_profile_merge_from_buffer(i8* %22, i64 %18)
%39 = tail call i32 @foo(i32 2)
%40 = tail call i32 @truncate(i8* %6, i32 0)
%41 = tail call i64 (...) @__llvm_profile_write_file()
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %25
%44 = tail call i32 (...) @bar()
br label %45
45: ; preds = %4, %43, %15, %25, %12, %2
%46 = phi i32 [ 1, %2 ], [ 1, %4 ], [ 1, %12 ], [ 0, %43 ], [ 1, %15 ], [ 1, %25 ]
ret i32 %46
}