#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 /*<<< orphan*/ *stdout;
int main() {
  char ch;
  char str;
  int ret;
  FILE *fp1;
  fp1 = fopen("test.txt", "r");
  FILE *fp2;
  fp2 = fopen("file2.txt", "w");
  while (1) {
    ret = fscanf(fp1, "%c", &ch);
    fprintf(fp2, "%c", ch);
    fprintf(stdout, "%c", ch);
    if (ret <= 0)
      break;
  }
  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    0xeac(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe9e(%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)
mov    -0x20(%rbp),%rdi
lea    0xe78(%rip),%rsi
movsbl -0x5(%rbp),%edx
call   1050 <fprintf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%rdi
lea    0xe5e(%rip),%rsi
movsbl -0x5(%rbp),%edx
call   1050 <fprintf@plt>
cmpl   $0x0,-0xc(%rbp)
jg     11d1 <main+0x91>
jmp    11d6 <main+0x96>
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   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeae(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0xea3(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xe99(%rip),%r15
lea    0x7(%rsp),%r12
mov    0x2e56(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <fscanf@plt>
mov    %eax,%ebp
movsbl 0x7(%rsp),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    0x0(%r13),%rsi
movsbl 0x7(%rsp),%edi
call   1050 <fputc@plt>
test   %ebp,%ebp
jg     1190 <main+0x50>
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    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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