#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*/ 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) {
    close(0);
    open(argv[1], O_RDONLY);
  }
  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    1180 <main+0x40>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
jmp    1185 <main+0x45>
lea    -0x400(%rbp),%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
mov    %eax,-0x404(%rbp)
cmp    $0x0,%eax
je     11c3 <main+0x83>
lea    -0x400(%rbp),%rsi
mov    -0x404(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1185 <main+0x45>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x3f0,%rsp
cmp    $0x2,%edi
jl     1169 <main+0x29>
mov    %rsi,%rbx
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rbx),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11a2 <main+0x62>
mov    %rsp,%rbx
nop
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1180 <main+0x40>
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 [1000 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = call i32 @close(i32 0)
%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)
br label %17
17: ; preds = %10, %2
br label %18
18: ; preds = %22, %17
%19 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%20 = call i32 @read(i32 0, i8* %19, i32 1000)
store i32 %20, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* %7
%25 = call i32 @write(i32 1, i8* %23, i32 %24)
br label %18
26: ; preds = %18
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 %11
5: ; preds = %2
%6 = tail call i32 @close(i32 0)
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
br label %11
11: ; preds = %5, %2
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 0, i8* %12, i32 1000)
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %11, %15
%16 = phi i32 [ %18, %15 ], [ %13, %11 ]
%17 = call i32 @write(i32 1, i8* %12, i32 %16)
%18 = call i32 @read(i32 0, i8* %12, i32 1000)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %15
20: ; preds = %15, %11
ret i32 0
}