#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 EOF;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *stdout;
int main(int argc, char *argv[]) {
  FILE *fp1, *fp2;
  int c;
  if (argc != 3) {
    printf("wrong command\n");
    exit(1);
  }
  if ((fp1 = fopen(argv[1], "r")) == NULL) {
    perror("fail to open");
    exit(1);
  }
  if ((fp2 = fopen(argv[2], "w+")) == NULL) {
    perror("fail to open");
    exit(1);
  }
  while ((c = fgetc(fp1)) != EOF) {
    if (fputc(c, fp2) == EOF) {
      perror("fail to write");
      exit(1);
    }
    if (fputc(c, stdout) == EOF) {
      perror("fail to write");
      exit(1);
    }
  }
  if (errno != 0) {
    perror("fail to read");
    exit(1);
  }
  fclose(fp1);
  fclose(fp2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a6 <main+0x36>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11de <main+0x6e>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe31(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1216 <main+0xa6>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    121b <main+0xab>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%eax
je     12a3 <main+0x133>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%eax
jne    1267 <main+0xf7>
lea    0xdc9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%rsi
call   1060 <fputc@plt>
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%eax
jne    129e <main+0x12e>
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    121b <main+0xab>
mov    0x2d2e(%rip),%rax
cmpq   $0x0,(%rax)
je     12ca <main+0x15a>
lea    0xd74(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x3,%edi
jne    1242 <main+0xd2>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1239 <main+0xc9>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe64(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1239 <main+0xc9>
mov    %rax,%r15
mov    0x2e25(%rip),%rbx
mov    0x2e06(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    (%rbx),%eax
je     1211 <main+0xa1>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
cmp    (%rbx),%eax
je     11fb <main+0x8b>
mov    (%r12),%rsi
mov    %ebp,%edi
call   1060 <fputc@plt>
cmp    (%rbx),%eax
jne    11d0 <main+0x60>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dc0(%rip),%rax
cmpq   $0x0,(%rax)
jne    1258 <main+0xe8>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
jmp    1202 <main+0x92>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdc1(%rip),%rdi
jmp    1202 <main+0x92>

LLVM IR (-O0):
@.str = [15 x i8] c"wrong command\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"fail to open\00"
@.str.3 = [3 x i8] c"w+\00"
@EOF = global i32 0
@.str.4 = [14 x i8] c"fail to write\00"
@stdout = global i32* null
@errno = global i64 0
@.str.5 = [13 x i8] c"fail to read\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %27, i32** %7
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
br label %33
33: ; preds = %56, %32
%34 = load i32*, i32** %6
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %8
%36 = load i32, i32* @EOF
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %57
38: ; preds = %33
%39 = load i32, i32* %8
%40 = load i32*, i32** %7
%41 = call i32 @fputc(i32 %39, i32* %40)
%42 = load i32, i32* @EOF
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = load i32, i32* %8
%49 = load i32*, i32** @stdout
%50 = call i32 @fputc(i32 %48, i32* %49)
%51 = load i32, i32* @EOF
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %47
br label %33
57: ; preds = %33
%58 = load i64, i64* @errno
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %57
%64 = load i32*, i32** %6
%65 = call i32 @fclose(i32* %64)
%66 = load i32*, i32** %7
%67 = call i32 @fclose(i32* %66)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"fail to open\00"
@.str.3 = [3 x i8] c"w+\00"
@EOF = global i32 0
@.str.4 = [14 x i8] c"fail to write\00"
@stdout = global i32* null
@errno = global i64 0
@.str.5 = [13 x i8] c"fail to read\00"
@str = [14 x i8] c"wrong command\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
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.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15, %34
%24 = tail call i32 @fgetc(i32* %10)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %42, label %27
27: ; preds = %23
%28 = tail call i32 @fputc(i32 %24, i32* %18)
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i32*, i32** @stdout
%36 = tail call i32 @fputc(i32 %24, i32* %35)
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %23
39: ; preds = %34
%40 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %23
%43 = load i64, i64* @errno
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %42
%49 = tail call i32 @fclose(i32* %10)
%50 = tail call i32 @fclose(i32* %18)
ret i32 0
}