#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*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ dup2(int, int);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  if (argc > 1) {
    int fd = creat(argv[1], 0644);
    close(1);
    dup2(fd, 1);
  }
  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    119f <main+0x4f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
jmp    11a4 <main+0x54>
lea    -0x400(%rbp),%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1060 <read@plt>
mov    %eax,-0x404(%rbp)
cmp    $0x0,%eax
je     11e2 <main+0x92>
lea    -0x400(%rbp),%rsi
mov    -0x404(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11a4 <main+0x54>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x3f0,%rsp
cmp    $0x2,%edi
jl     1183 <main+0x33>
mov    0x8(%rsi),%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
mov    %eax,%ebx
mov    $0x1,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x3e8,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11c2 <main+0x72>
mov    %rsp,%rbx
nopl   0x0(%rax)
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):
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 %19
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @creat(i8* %14, i32 420)
store i32 %15, i32* %6
%16 = call i32 @close(i32 1)
%17 = load i32, i32* %6
%18 = call i32 @dup2(i32 %17, i32 1)
br label %19
19: ; preds = %11, %2
br label %20
20: ; preds = %24, %19
%21 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%22 = call i32 @read(i32 0, i8* %21, i32 1000)
store i32 %22, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%26 = load i32, i32* %8
%27 = call i32 @write(i32 1, i8* %25, i32 %26)
br label %20
28: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
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 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @creat(i8* %7, i32 420)
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @dup2(i32 %8, i32 1)
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
}