#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 int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char const *argv[]) {
  if (argc < 2) {
    return -1;
  }
  char *filename = argv[1];
  char *data = NULL;
  if (argc >= 3) {
    data = argv[2];
  }
  FILE *f = fopen(filename, "r+");
  if (!f) {
    return -1;
  }
  if (data) {
    fwrite(data, 1, strlen(data), f);
  } else {
    char buf[1024] = "";
    fread(buf, 1, sizeof(buf), f);
    printf("%s\n", buf);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119f <main+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <main+0x10f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11c9 <main+0x59>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f4 <main+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <main+0x10f>
cmpq   $0x0,-0x20(%rbp)
je     122f <main+0xbf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
jmp    126f <main+0xff>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
lea    -0x430(%rbp),%rsi
lea    0xd99(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    $0xffffffff,%ebp
cmp    $0x2,%edi
jl     121e <main+0xae>
mov    0x8(%rsi),%rdi
jne    11a5 <main+0x35>
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11de <main+0x6e>
jmp    121e <main+0xae>
mov    0x10(%rsi),%r14
lea    0xe50(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     121e <main+0xae>
mov    %rax,%rbx
test   %r14,%r14
je     11de <main+0x6e>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
jmp    1214 <main+0xa4>
mov    %rsp,%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xdf7(%rip),%rdi
mov    %rbp,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %48
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
store i8* null, i8** %7
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 3
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
store i8* %22, i8** %7
br label %23
23: ; preds = %19, %13
%24 = load i8*, i8** %6
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %23
store i32 -1, i32* %3
br label %48
29: ; preds = %23
%30 = load i8*, i8** %7
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i8*, i8** %7
%34 = load i8*, i8** %7
%35 = call i32 @strlen(i8* %34)
%36 = load i32*, i32** %8
%37 = call i32 @fwrite(i8* %33, i32 1, i32 %35, i32* %36)
br label %45
38: ; preds = %29
%39 = bitcast [1024 x i8]* %9 to i8*
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%41 = load i32*, i32** %8
%42 = call i32 @fread(i8* %40, i32 1, i32 1024, i32* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %38, %32
%46 = load i32*, i32** %8
%47 = call i32 @fclose(i32* %46)
store i32 0, i32* %3
br label %48
48: ; preds = %45, %28, %12
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %30, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i32 %0, 2
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %30, label %17
14: ; preds = %5
%15 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %30, label %22
17: ; preds = %9
%18 = icmp eq i8* %11, null
br i1 %18, label %22, label %19
19: ; preds = %17
%20 = tail call i32 @strlen(i8* %11)
%21 = tail call i32 @fwrite(i8* %11, i32 1, i32 %20, i32* %12)
br label %27
22: ; preds = %14, %17
%23 = phi i32* [ %12, %17 ], [ %15, %14 ]
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = call i32 @fread(i8* %24, i32 1, i32 1024, i32* %23)
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %27
27: ; preds = %22, %19
%28 = phi i32* [ %23, %22 ], [ %12, %19 ]
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %14, %27, %9, %2
%31 = phi i32 [ -1, %2 ], [ 0, %27 ], [ -1, %9 ], [ -1, %14 ]
ret i32 %31
}