#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Wrong number of arguments\n");
    printf("please use: ./binary file1.txt file2.txt\n");
    exit(1);
  }
  int pipeFds1[2];
  int inputSize = 0;
  struct stat fileStat;
  char *buffer = 0;
  int bufferSize = 0;
  if (pipe(pipeFds1) < 0) {
    perror("pipe");
    exit(1);
  }
  stat(argv[1], &fileStat);
  bufferSize = sizeof(char) * fileStat.st_size;
  if (!fork()) {
    close(pipeFds1[1]);
    if (!(buffer = malloc(bufferSize))) {
      perror("malloc");
      exit(1);
    }
    read(pipeFds1[0], buffer, bufferSize);
    FILE *dest = fopen(argv[2], "w");
    if (!dest) {
      perror("fopen");
      exit(1);
    }
    fwrite(buffer, sizeof(char), fileStat.st_size, dest);
    free(buffer);
    close(pipeFds1[0]);
    fclose(dest);
    exit(0);
  } else {
    close(pipeFds1[0]);
    FILE *origin = fopen(argv[1], "r");
    if (!origin) {
      perror("fopen");
      exit(1);
    }
    buffer = malloc(bufferSize);
    if (!buffer) {
      exit(1);
    }
    fread(buffer, sizeof(char), fileStat.st_size, origin);
    write(pipeFds1[1], buffer, bufferSize);
    wait(NULL);
    free(buffer);
    close(pipeFds1[0]);
    fclose(origin);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1242 <main+0x42>
lea    0xdd9(%rip),%rdi
call   1070 <printf@plt>
lea    0xde8(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x18(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
jge    1281 <main+0x81>
lea    0xdd3(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1120 <fork@plt>
cmp    $0x0,%eax
jne    135f <main+0x15f>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x2c(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12e1 <main+0xe1>
lea    0xd78(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   10a0 <read@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd52(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1329 <main+0x129>
lea    0xd39(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1100 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xce3(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    13a0 <main+0x1a0>
lea    0xcc2(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    13c1 <main+0x1c1>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <write@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x40(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jne    12cf <main+0xcf>
mov    %rsi,%r14
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
js     12f1 <main+0xf1>
mov    0x8(%r14),%rdi
lea    0x10(%rsp),%rsi
call   10b0 <stat@plt>
mov    0x10(%rsp),%r15d
xor    %eax,%eax
call   1120 <fork@plt>
test   %eax,%eax
je     12fa <main+0xfa>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%r14),%rdi
lea    0xdb5(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1340 <main+0x140>
mov    %rax,%rbx
mov    %r15d,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12e7 <main+0xe7>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1060 <write@plt>
xor    %edi,%edi
call   1110 <wait@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd40(%rip),%rdi
call   1040 <puts@plt>
lea    0xd4e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xd08(%rip),%rdi
jmp    1347 <main+0x147>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    %r15d,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
jne    1319 <main+0x119>
lea    0xcee(%rip),%rdi
jmp    1347 <main+0x147>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   10a0 <read@plt>
mov    0x10(%r14),%rdi
lea    0xcd6(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
jne    1356 <main+0x156>
lea    0xcc7(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   1100 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [27 x i8] c"Wrong number of arguments\0A\00"
@.str.1 = [42 x i8] c"please use: ./binary file1.txt file2.txt\0A\00"
@.str.2 = [5 x i8] c"pipe\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [6 x i8] c"fopen\00"
@.str.6 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
store i32 0, i32* %7
store i8* null, i8** %9
store i32 0, i32* %10
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%21 = call i64 @pipe(i32* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @stat(i8* %29, %struct.stat* %8)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
store i32 %35, i32* %10
%36 = call i32 (...) @fork()
%37 = icmp ne i32 %36, 0
br i1 %37, label %77, label %38
38: ; preds = %26
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %10
%43 = call i8* @malloc(i32 %42)
store i8* %43, i8** %9
%44 = icmp ne i8* %43, null
br i1 %44, label %48, label %45
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %38
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%50 = load i32, i32* %49
%51 = load i8*, i8** %9
%52 = load i32, i32* %10
%53 = call i32 @read(i32 %50, i8* %51, i32 %52)
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = call i32* @fopen(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %57, i32** %11
%58 = load i32*, i32** %11
%59 = icmp ne i32* %58, null
br i1 %59, label %63, label %60
60: ; preds = %48
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %48
%64 = load i8*, i8** %9
%65 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%66 = load i32, i32* %65
%67 = load i32*, i32** %11
%68 = call i32 @fwrite(i8* %64, i32 1, i32 %66, i32* %67)
%69 = load i8*, i8** %9
%70 = call i32 @free(i8* %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = load i32*, i32** %11
%75 = call i32 @fclose(i32* %74)
%76 = call i32 @exit(i32 0)
unreachable
77: ; preds = %26
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%79 = load i32, i32* %78
%80 = call i32 @close(i32 %79)
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 1
%83 = load i8*, i8** %82
%84 = call i32* @fopen(i8* %83, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %84, i32** %12
%85 = load i32*, i32** %12
%86 = icmp ne i32* %85, null
br i1 %86, label %90, label %87
87: ; preds = %77
%88 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%89 = call i32 @exit(i32 1)
unreachable
90: ; preds = %77
%91 = load i32, i32* %10
%92 = call i8* @malloc(i32 %91)
store i8* %92, i8** %9
%93 = load i8*, i8** %9
%94 = icmp ne i8* %93, null
br i1 %94, label %97, label %95
95: ; preds = %90
%96 = call i32 @exit(i32 1)
unreachable
97: ; preds = %90
%98 = load i8*, i8** %9
%99 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%100 = load i32, i32* %99
%101 = load i32*, i32** %12
%102 = call i32 @fread(i8* %98, i32 1, i32 %100, i32* %101)
%103 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%104 = load i32, i32* %103
%105 = load i8*, i8** %9
%106 = load i32, i32* %10
%107 = call i32 @write(i32 %104, i8* %105, i32 %106)
%108 = call i32 @wait(i32* null)
%109 = load i8*, i8** %9
%110 = call i32 @free(i8* %109)
%111 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%112 = load i32, i32* %111
%113 = call i32 @close(i32 %112)
%114 = load i32*, i32** %12
%115 = call i32 @fclose(i32* %114)
br label %116
116: ; preds = %97
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.2 = [5 x i8] c"pipe\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [6 x i8] c"fopen\00"
@.str.6 = [2 x i8] c"r\00"
@str = [26 x i8] c"Wrong number of arguments\00"
@str.7 = [41 x i8] c"please use: ./binary file1.txt file2.txt\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca %struct.stat
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.7, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = bitcast [2 x i32]* %3 to i8*
%12 = bitcast %struct.stat* %4 to i8*
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @stat(i8* %21, %struct.stat* %4)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 (...) @fork()
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %19
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = call i8* @malloc(i32 %24)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load i32, i32* %13
%38 = call i32 @read(i32 %37, i8* %31, i32 %24)
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %36
%47 = call i32 @fwrite(i8* %31, i32 1, i32 %24, i32* %41)
%48 = call i32 @free(i8* %31)
%49 = load i32, i32* %13
%50 = call i32 @close(i32 %49)
%51 = call i32 @fclose(i32* %41)
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %19
%54 = load i32, i32* %13
%55 = call i32 @close(i32 %54)
%56 = load i8*, i8** %20
%57 = call i32* @fopen(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %53
%63 = call i8* @malloc(i32 %24)
%64 = icmp eq i8* %63, null
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %62
%68 = call i32 @fread(i8* %63, i32 1, i32 %24, i32* %57)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%70 = load i32, i32* %69
%71 = call i32 @write(i32 %70, i8* %63, i32 %24)
%72 = call i32 @wait(i32* null)
%73 = call i32 @free(i8* %63)
%74 = load i32, i32* %13
%75 = call i32 @close(i32 %74)
%76 = call i32 @fclose(i32* %57)
ret i32 0
}