#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(void) {
  FILE *in, *out;
  if ((in = fopen("test.txt", "rt")) == NULL) {
    fprintf(stderr, "Cannot open input file.\n");
    return 1;
  }
  if ((out = fopen("test.txt.bak", "wt")) == NULL) {
    fprintf(stderr, "Cannot open output file.\n");
    return 1;
  }
  while (!feof(in))
    fputc(fgetc(in), out);
  fclose(in);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11b1 <main+0x51>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <main+0xe8>
lea    0xe6d(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f3 <main+0x93>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <main+0xe8>
jmp    11f8 <main+0x98>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1216 <main+0xb6>
jmp    122f <main+0xcf>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
call   1050 <fputc@plt>
jmp    11f8 <main+0x98>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d4 <main+0x74>
mov    %rax,%rbx
lea    0xe9f(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e6 <main+0x86>
mov    %rax,%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11c0 <main+0x60>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
jmp    11a0 <main+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1200 <main+0xa0>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
jmp    11f6 <main+0x96>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Cannot open input file.\0A\00"
@.str.3 = [13 x i8] c"test.txt.bak\00"
@.str.4 = [3 x i8] c"wt\00"
@.str.5 = [26 x i8] c"Cannot open output file.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %31
9: ; preds = %0
%10 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %10, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %31
15: ; preds = %9
br label %16
16: ; preds = %21, %15
%17 = load i32*, i32** %2
%18 = call i32 @feof(i32* %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32*, i32** %2
%23 = call i32 @fgetc(i32* %22)
%24 = load i32*, i32** %3
%25 = call i32 @fputc(i32 %23, i32* %24)
br label %16
26: ; preds = %16
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
%29 = load i32*, i32** %3
%30 = call i32 @fclose(i32* %29)
store i32 0, i32* %1
br label %31
31: ; preds = %26, %12, %6
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Cannot open input file.\0A\00"
@.str.3 = [13 x i8] c"test.txt.bak\00"
@.str.4 = [3 x i8] c"wt\00"
@.str.5 = [26 x i8] c"Cannot open output file.\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %23
6: ; preds = %0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i32 @feof(i32* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %20
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %23
15: ; preds = %9, %15
%16 = tail call i32 @fgetc(i32* %1)
%17 = tail call i32 @fputc(i32 %16, i32* %7)
%18 = tail call i32 @feof(i32* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %15, label %20
20: ; preds = %15, %9
%21 = tail call i32 @fclose(i32* %1)
%22 = tail call i32 @fclose(i32* %7)
br label %23
23: ; preds = %20, %12, %3
%24 = phi i32 [ 1, %3 ], [ 1, %12 ], [ 0, %20 ]
ret i32 %24
}