#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*/ fprintf(int /*<<< orphan*/ *, char *, char);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int main() {
  char str;
  int ret;
  FILE *fp1;
  FILE *fp2;
  fp1 = fopen("test1.txt", "r");
  fp2 = fopen("test.txt", "w");
  while (1) {
    ret = fscanf(fp1, "%c", &str);
    if (ret <= 0)
      break;
    fprintf(fp2, "%c", str);
  }
  fclose(fp1);
  fclose(fp2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe9f(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe8f(%rip),%rsi
lea    -0x5(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     11a3 <main+0x63>
jmp    11bc <main+0x7c>
mov    -0x20(%rbp),%rdi
lea    0xe69(%rip),%rsi
movsbl -0x5(%rbp),%edx
call   1050 <fprintf@plt>
jmp    117d <main+0x3d>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb1(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0xea7(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xe9c(%rip),%rsi
lea    0x7(%rsp),%rdx
mov    %r14,%rdi
call   1040 <fscanf@plt>
test   %eax,%eax
jle    11bf <main+0x7f>
lea    0xe84(%rip),%r15
lea    0x7(%rsp),%r12
nopl   0x0(%rax,%rax,1)
movsbl 0x7(%rsp),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <fscanf@plt>
test   %eax,%eax
jg     11a0 <main+0x60>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test1.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"test.txt\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %7, i32** %5
br label %8
8: ; preds = %0, %14
%9 = load i32*, i32** %4
%10 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %2)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %8
br label %18
14: ; preds = %8
%15 = load i32*, i32** %5
%16 = load i8, i8* %2
%17 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8 signext %16)
br label %8
18: ; preds = %13
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
%21 = load i32*, i32** %5
%22 = call i32 @fclose(i32* %21)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"test1.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"test.txt\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @main() {
%1 = alloca i8
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%4 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%5 = icmp slt i32 %4, 1
br i1 %5, label %12, label %6
6: ; preds = %0, %6
%7 = load i8, i8* %1
%8 = sext i8 %7 to i32
%9 = call i32 @fputc(i32 %8, i32* %3)
%10 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %6
12: ; preds = %6, %0
%13 = call i32 @fclose(i32* %2)
%14 = call i32 @fclose(i32* %3)
ret i32 0
}