#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 */
struct bson_info {
  int dummy;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ bson_cleanup(struct bson_info *);
struct bson_info *bson_init();
int /*<<< orphan*/ bson_print(int /*<<< orphan*/, struct bson_info *);
int bson_readf(struct bson_info *, int);
int /*<<< orphan*/ check_syscall(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf_blue(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf_cyan(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fprintf_light_red(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int f;
  struct bson_info *bson;
  int ret;
  fprintf_blue(stdout, "BSON Printer -- By: Wolfgang Richter "
                       "<wolf@cs.cmu.edu>\n");
  if (argc < 2) {
    fprintf_light_red(stderr, "Usage: %s <BSON file>\n", argv[0]);
    return EXIT_FAILURE;
  }
  fprintf_cyan(stdout, "Analyzing BSON File: %s\n", argv[1]);
  f = open(argv[1], O_RDONLY);
  if (f < 0) {
    fprintf_light_red(stderr, "Error opening BSON file.\n");
    return EXIT_FAILURE;
  }
  bson = bson_init();
  while ((ret = bson_readf(bson, f)) == 1)
    bson_print(stdout, bson);
  bson_cleanup(bson);
  check_syscall(close(f));
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1030 <fprintf_blue@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1204 <main+0x64>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf_light_red@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <main+0x12a>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe33(%rip),%rsi
call   10a0 <fprintf_cyan@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126c <main+0xcc>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf_light_red@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <main+0x12a>
mov    $0x0,%al
call   10c0 <bson_init@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <bson_readf@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x1,%eax
jne    12a6 <main+0x106>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
call   1070 <bson_print@plt>
jmp    1277 <main+0xd7>
mov    -0x20(%rbp),%rdi
call   1050 <bson_cleanup@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%edi
call   1080 <check_syscall@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e18(%rip),%r14
mov    (%r14),%edi
lea    0xe46(%rip),%rsi
call   1030 <fprintf_blue@plt>
cmp    $0x1,%ebp
jg     11e3 <main+0x43>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe61(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf_light_red@plt>
jmp    1272 <main+0xd2>
mov    (%r14),%edi
mov    0x8(%rbx),%rdx
lea    0xe5e(%rip),%rsi
call   10a0 <fprintf_cyan@plt>
mov    0x8(%rbx),%rdi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
js     125b <main+0xbb>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <bson_init@plt>
mov    %rax,%rbx
mov    %rax,%rdi
nopl   0x0(%rax,%rax,1)
mov    %ebp,%esi
call   1090 <bson_readf@plt>
cmp    $0x1,%eax
jne    123c <main+0x9c>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1070 <bson_print@plt>
mov    %rbx,%rdi
jmp    1220 <main+0x80>
mov    %rbx,%rdi
call   1050 <bson_cleanup@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %eax,%edi
call   1080 <check_syscall@plt>
mov    0x2d67(%rip),%rax
jmp    1279 <main+0xd9>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf_light_red@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.bson_info = type { i32 }
@stdout = global i32 0
@.str = [56 x i8] c"BSON Printer -- By: Wolfgang Richter <wolf@cs.cmu.edu>\0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Usage: %s <BSON file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [25 x i8] c"Analyzing BSON File: %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [26 x i8] c"Error opening BSON file.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.bson_info*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @stdout
%10 = call i32 @fprintf_blue(i32 %9, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf_light_red(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %3
br label %55
20: ; preds = %2
%21 = load i32, i32* @stdout
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @fprintf_cyan(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf_light_red(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %3
br label %55
37: ; preds = %20
%38 = call %struct.bson_info* (...) @bson_init()
store %struct.bson_info* %38, %struct.bson_info** %7
br label %39
39: ; preds = %44, %37
%40 = load %struct.bson_info*, %struct.bson_info** %7
%41 = load i32, i32* %6
%42 = call i32 @bson_readf(%struct.bson_info* %40, i32 %41)
store i32 %42, i32* %8
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @stdout
%46 = load %struct.bson_info*, %struct.bson_info** %7
%47 = call i32 @bson_print(i32 %45, %struct.bson_info* %46)
br label %39
48: ; preds = %39
%49 = load %struct.bson_info*, %struct.bson_info** %7
%50 = call i32 @bson_cleanup(%struct.bson_info* %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = call i32 @check_syscall(i32 %52)
%54 = load i32, i32* @EXIT_SUCCESS
store i32 %54, i32* %3
br label %55
55: ; preds = %48, %33, %13
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.bson_info = type { i32 }
@stdout = global i32 0
@.str = [56 x i8] c"BSON Printer -- By: Wolfgang Richter <wolf@cs.cmu.edu>\0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Usage: %s <BSON file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [25 x i8] c"Analyzing BSON File: %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [26 x i8] c"Error opening BSON file.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @stdout
%4 = tail call i32 @fprintf_blue(i32 %3, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf_light_red(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %35
10: ; preds = %2
%11 = load i32, i32* @stdout
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @fprintf_cyan(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %12
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* %15, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf_light_red(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %35
22: ; preds = %10
%23 = tail call %struct.bson_info* (...) @bson_init()
%24 = tail call i32 @bson_readf(%struct.bson_info* %23, i32 %17)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %31
26: ; preds = %22, %26
%27 = load i32, i32* @stdout
%28 = tail call i32 @bson_print(i32 %27, %struct.bson_info* %23)
%29 = tail call i32 @bson_readf(%struct.bson_info* %23, i32 %17)
%30 = icmp eq i32 %29, 1
br i1 %30, label %26, label %31
31: ; preds = %26, %22
%32 = tail call i32 @bson_cleanup(%struct.bson_info* %23)
%33 = tail call i32 @close(i32 %17)
%34 = tail call i32 @check_syscall(i32 %33)
br label %35
35: ; preds = %31, %19, %6
%36 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_FAILURE, %19 ], [ @EXIT_SUCCESS, %31 ]
%37 = load i32, i32* %36
ret i32 %37
}