#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_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ dup2(int, int);
int open(char *, int, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fdIn = open(argv[1], O_RDONLY);
  int fdOut = open(argv[2], O_WRONLY | O_CREAT, 0777);
  dup2(fdIn, 0);
  dup2(fdOut, 1);
  int readCount;
  char buffer[20];
  while (readCount = read(0, buffer, 20)) {
    write(1, buffer, readCount);
  }
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    -0x30(%rbp),%rsi
xor    %edi,%edi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     11e3 <main+0xa3>
lea    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11b1 <main+0x71>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
mov    0x2e5e(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1ff,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11d2 <main+0x92>
mov    %rsp,%rbx
xchg   %ax,%ax
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    11b0 <main+0x70>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
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
%9 = alloca [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %20, i32 511)
store i32 %21, i32* %7
%22 = load i32, i32* %6
%23 = call i32 @dup2(i32 %22, i32 0)
%24 = load i32, i32* %7
%25 = call i32 @dup2(i32 %24, i32 1)
br label %26
26: ; preds = %30, %2
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%28 = call i32 @read(i32 0, i8* %27, i32 20)
store i32 %28, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%32 = load i32, i32* %8
%33 = call i32 @write(i32 1, i8* %31, i32 %32)
br label %26
34: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* %5, i32 %6)
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %12, i32 511)
%14 = tail call i32 @dup2(i32 %7, i32 0)
%15 = tail call i32 @dup2(i32 %13, i32 1)
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%17 = call i32 @read(i32 0, i8* %16, i32 20)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %2, %19
%20 = phi i32 [ %22, %19 ], [ %17, %2 ]
%21 = call i32 @write(i32 1, i8* %16, i32 %20)
%22 = call i32 @read(i32 0, i8* %16, i32 20)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %19
24: ; preds = %19, %2
ret i32 0
}