#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  if (argc == 1) {
    printf("fw inputfile outputfile start_of_output\n");
    return 0;
  }
  if (argc != 4) {
    printf("arguments error!\n");
    return -1;
  }
  FILE *input = fopen(argv[1], "r");
  if (input == NULL) {
    printf("input file open error!\n");
    return -1;
  }
  FILE *output = fopen(argv[1], "a+");
  if (output == NULL) {
    printf("output file open error!\n");
    fclose(input);
    return -1;
  }
  fclose(input);
  fclose(output);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1168 <main+0x38>
lea    0xea9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121b <main+0xeb>
cmpl   $0x4,-0x8(%rbp)
je     118a <main+0x5a>
lea    0xeb0(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <main+0xeb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <main+0x95>
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <main+0xeb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe81(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <main+0xd9>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <main+0xeb>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x4,%edi
je     114c <main+0x1c>
cmp    $0x1,%edi
jne    1193 <main+0x63>
lea    0xf00(%rip),%rdi
call   1030 <puts@plt>
jmp    118f <main+0x5f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <main+0x6c>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
lea    0xe90(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b5 <main+0x85>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11ad <main+0x7d>
lea    0xe9a(%rip),%rdi
jmp    11a3 <main+0x73>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11a8 <main+0x78>

LLVM IR (-O0):
@.str = [41 x i8] c"fw inputfile outputfile start_of_output\0A\00"
@.str.1 = [18 x i8] c"arguments error!\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"input file open error!\0A\00"
@.str.4 = [3 x i8] c"a+\00"
@.str.5 = [25 x i8] c"output file open error!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %42
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
17: ; preds = %12
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
26: ; preds = %17
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
store i32 -1, i32* %3
br label %42
37: ; preds = %26
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
br label %42
42: ; preds = %37, %33, %24, %15, %10
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"a+\00"
@str = [24 x i8] c"output file open error!\00"
@str.6 = [23 x i8] c"input file open error!\00"
@str.7 = [17 x i8] c"arguments error!\00"
@str.8 = [40 x i8] c"fw inputfile outputfile start_of_output\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %5 [
i32 1, label %3
i32 4, label %7
]
3: ; preds = %2
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.8, i64 0, i64 0))
br label %24
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
br label %24
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.6, i64 0, i64 0))
br label %24
14: ; preds = %7
%15 = load i8*, i8** %8
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @fclose(i32* %10)
br label %24
21: ; preds = %14
%22 = tail call i32 @fclose(i32* %10)
%23 = tail call i32 @fclose(i32* %16)
br label %24
24: ; preds = %12, %21, %18, %3, %5
%25 = phi i32 [ 0, %3 ], [ -1, %5 ], [ -1, %12 ], [ -1, %18 ], [ 0, %21 ]
ret i32 %25
}