#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup(int);
int open(char *, int /*<<< orphan*/);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  if (argc > 1) {
    int fd = open(argv[1], O_RDONLY);
    close(0);
    dup(fd);
  }
  char buf[1000];
  int len;
  while ((len = read(0, buf, 1000)))
    write(1, buf, len);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    119b <main+0x4b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <dup@plt>
jmp    11a0 <main+0x50>
lea    -0x400(%rbp),%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1060 <read@plt>
mov    %eax,-0x404(%rbp)
cmp    $0x0,%eax
je     11de <main+0x8e>
lea    -0x400(%rbp),%rsi
mov    -0x404(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11a0 <main+0x50>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x3f0,%rsp
cmp    $0x2,%edi
jl     117f <main+0x2f>
mov    0x8(%rsi),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1040 <dup@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11c2 <main+0x72>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11a0 <main+0x50>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
ret

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

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