#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int write(int, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int ret;
  int fd;
  uint8_t buf[0x1000];
  if (argc < 2) {
    printf("usage: %s filename\n", argv[0]);
    exit(1);
  }
  fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0);
  if (fd == -1) {
    perror("open error");
    exit(1);
  }
  while (1) {
    ret = read(STDIN_FILENO, buf, sizeof(buf));
    if (ret == -1) {
      perror("read error");
      exit(1);
    }
    if (ret == 0) {
      break;
    }
    ret = write(fd, buf, ret);
    if (ret == -1) {
      perror("write file error");
      exit(2);
    }
    ret = write(STDOUT_FILENO, buf, ret);
    if (ret == -1) {
      perror("write stdout error");
      exit(2);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a0 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ed <main+0x8d>
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11f2 <main+0x92>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    -0x4020(%rbp),%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122f <main+0xcf>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    123e <main+0xde>
jmp    12b3 <main+0x153>
mov    -0x18(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1273 <main+0x113>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    -0x4020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ae <main+0x14e>
lea    0xd9c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    11f2 <main+0x92>
xor    %eax,%eax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4008,%rsp
cmp    $0x1,%edi
jle    1241 <main+0xe1>
mov    0x2e53(%rip),%rcx
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x8(%rsi),%rdi
mov    0x2e5d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xfa>
mov    %eax,%ebp
mov    0x2e10(%rip),%r14
mov    %rsp,%rbx
mov    0x2e26(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1205 <main+0xa5>
cmp    $0xffffffff,%eax
je     1215 <main+0xb5>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xcb>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x60>
lea    0xe38(%rip),%rdi
jmp    1232 <main+0xd2>
xor    %eax,%eax
add    $0x4008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    (%rsi),%rsi
lea    0xdb5(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdb3(%rip),%rdi
jmp    121c <main+0xbc>

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

LLVM IR (-O2):
@.str = [20 x i8] c"usage: %s filename\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@STDIN_FILENO = global i32 0
@.str.2 = [11 x i8] c"read error\00"
@.str.3 = [17 x i8] c"write file error\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [19 x i8] c"write stdout error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i32]
%4 = bitcast [4096 x i32]* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %12, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %20
20: ; preds = %10
%21 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 0
br label %25
22: ; preds = %10
%23 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %20, %37
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @read(i32 %26, i32* %21, i32 16384)
switch i32 %27, label %31 [
i32 -1, label %28
i32 0, label %44
]
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25
%32 = call i32 @write(i32 %18, i32* %21, i32 %27)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @exit(i32 2)
unreachable
37: ; preds = %31
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i32* %21, i32 %32)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %25
41: ; preds = %37
%42 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%43 = tail call i32 @exit(i32 2)
unreachable
44: ; preds = %25
ret i32 0
}