#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDWR;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  if (argc < 3)
    exit(0);
  int fdrd, fdwr;
  if ((fdrd = open(argv[1], O_RDWR)) < 0) {
    printf("open source file failed!\n");
    exit(0);
  }
  if ((fdwr = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0) {
    printf("open target file failed!\n");
    exit(0);
  }
  char *buf = malloc(4096);
  int size;
  while ((size = read(fdrd, buf, 4096)) == 4096)
    write(fdwr, buf, 4096);
  write(fdwr, buf, 4096);
  close(fdrd);
  close(fdwr);
  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)
jge    1197 <main+0x27>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ce <main+0x5e>
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    121c <main+0xac>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x1000,%eax
jne    125f <main+0xef>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
jmp    122a <main+0xba>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jle    123b <main+0xcb>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1242 <main+0xd2>
mov    %eax,%r14d
mov    0x2e1a(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     124b <main+0xdb>
mov    %eax,%ebp
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %ebp,%edi
cmp    $0x1000,%eax
jne    1218 <main+0xa8>
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
jmp    11f0 <main+0x80>
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdd0(%rip),%rdi
jmp    1252 <main+0xe2>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [26 x i8] c"open source file failed!\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [26 x i8] c"open target file failed!\0A\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 i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %32, i32 438)
store i32 %33, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %24
%39 = call i8* @malloc(i32 4096)
store i8* %39, i8** %8
br label %40
40: ; preds = %45, %38
%41 = load i32, i32* %6
%42 = load i8*, i8** %8
%43 = call i32 @read(i32 %41, i8* %42, i32 4096)
store i32 %43, i32* %9
%44 = icmp eq i32 %43, 4096
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load i32, i32* %7
%47 = load i8*, i8** %8
%48 = call i32 @write(i32 %46, i8* %47, i32 4096)
br label %40
49: ; preds = %40
%50 = load i32, i32* %7
%51 = load i8*, i8** %8
%52 = call i32 @write(i32 %50, i8* %51, i32 4096)
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@str = [25 x i8] c"open target file failed!\00"
@str.2 = [25 x i8] c"open source file failed!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 (i8*, i32, ...) @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %6
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %19, %18
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = tail call i32 (i8*, i32, ...) @open(i8* %17, i32 %22, i32 438)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @exit(i32 0)
unreachable
28: ; preds = %15
%29 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%30 = tail call i32 @read(i32 %10, i8* %29, i32 4096)
%31 = icmp eq i32 %30, 4096
br i1 %31, label %32, label %36
32: ; preds = %28, %32
%33 = tail call i32 @write(i32 %23, i8* %29, i32 4096)
%34 = tail call i32 @read(i32 %10, i8* %29, i32 4096)
%35 = icmp eq i32 %34, 4096
br i1 %35, label %32, label %36
36: ; preds = %32, %28
%37 = tail call i32 @write(i32 %23, i8* %29, i32 4096)
%38 = tail call i32 @close(i32 %10)
%39 = tail call i32 @close(i32 %23)
ret i32 0
}