#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*/ buf;

/* Variables and functions */
int EINVAL;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char buf[4096];
  int ret, len, i;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "Invalid usage\n");
    fprintf(stderr, "Usage: %s <file>\n", argv[0]);
    return -EINVAL;
  }
  fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC);
  if (fd < 0) {
    fprintf(stderr, "Failed to open: %s\n", strerror(errno));
    return -errno;
  }
  while (1) {
    ret = read(STDIN_FILENO, buf, sizeof(buf));
    if (ret < 0) {
      fprintf(stderr, "Failed to write: %s\n", strerror(errno));
      ret = -errno;
      goto end;
    } else if (ret == 0) {
      break;
    }
    for (i = 0, len = ret; i < len;) {
      ret = write(fd, buf + i, len - i);
      if (ret < 0) {
        fprintf(stderr, "Failed to write: %s\n", strerror(errno));
        ret = -errno;
        goto end;
      }
      i += ret;
    }
  }
  ret = 0;
end:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x6b>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2dff(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c7 <main+0x267>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1020(%rbp)
cmpl   $0x0,-0x1020(%rbp)
jge    124d <main+0xed>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1024(%rbp),%edi
mov    %rax,%rdx
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d85(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c7 <main+0x267>
jmp    1252 <main+0xf2>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    12c9 <main+0x169>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1028(%rbp),%edi
mov    %rax,%rdx
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d0c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1014(%rbp)
jmp    13b3 <main+0x253>
cmpl   $0x0,-0x1014(%rbp)
jne    12db <main+0x17b>
jmp    13a9 <main+0x249>
jmp    12e0 <main+0x180>
movl   $0x0,-0x101c(%rbp)
mov    -0x1014(%rbp),%eax
mov    %eax,-0x1018(%rbp)
mov    -0x101c(%rbp),%eax
cmp    -0x1018(%rbp),%eax
jge    13a4 <main+0x244>
mov    -0x1020(%rbp),%edi
lea    -0x1010(%rbp),%rsi
movslq -0x101c(%rbp),%rax
add    %rax,%rsi
mov    -0x1018(%rbp),%edx
sub    -0x101c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    138d <main+0x22d>
mov    0x2c86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x102c(%rbp)
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x102c(%rbp),%edi
mov    %rax,%rdx
lea    0xcc5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2c48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1014(%rbp)
jmp    13b3 <main+0x253>
mov    -0x1014(%rbp),%eax
add    -0x101c(%rbp),%eax
mov    %eax,-0x101c(%rbp)
jmp    12f6 <main+0x196>
jmp    1252 <main+0xf2>
movl   $0x0,-0x1014(%rbp)
mov    -0x1020(%rbp),%edi
call   1040 <close@plt>
mov    -0x1014(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    124d <main+0xed>
mov    0x2e26(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1286 <main+0x126>
mov    %eax,%r12d
mov    0x2e07(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1213 <main+0xb3>
mov    %eax,%ebp
mov    %rsp,%r14
test   %ebp,%ebp
je     12c8 <main+0x168>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     1213 <main+0xb3>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     11e0 <main+0x80>
mov    (%r15),%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11ca <main+0x6a>
mov    0x2db6(%rip),%rax
mov    (%rax),%r14d
mov    0x2da4(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
lea    0xe03(%rip),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
sub    (%rbx),%ebp
mov    %r12d,%edi
call   1040 <close@plt>
jmp    12b6 <main+0x156>
mov    0x2d7c(%rip),%r14
mov    (%r14),%edi
lea    0xda2(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
mov    (%rbx),%rdx
lea    0xd9b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d3e(%rip),%rax
sub    (%rax),%ebp
jmp    12b6 <main+0x156>
mov    0x2d43(%rip),%rax
mov    (%rax),%ebx
mov    0x2d32(%rip),%r14
mov    (%r14),%edi
call   1080 <strerror@plt>
lea    0xd7c(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
sub    (%r14),%ebp
mov    %ebp,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    1243 <main+0xe3>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Invalid usage\0A\00"
@.str.1 = [18 x i8] c"Usage: %s <file>\0A\00"
@EINVAL = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [20 x i8] c"Failed to open: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.3 = [21 x i8] c"Failed to write: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %23
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EINVAL
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %96
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %26, i32 %31)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %23
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @errno
%41 = sub nsw i32 0, %40
store i32 %41, i32* %3
br label %96
42: ; preds = %23
br label %43
43: ; preds = %42, %90
%44 = load i32, i32* @STDIN_FILENO
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 4096)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %43
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @errno
%55 = sub nsw i32 0, %54
store i32 %55, i32* %7
br label %92
56: ; preds = %43
%57 = load i32, i32* %7
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %56
br label %91
60: ; preds = %56
br label %61
61: ; preds = %60
store i32 0, i32* %9
%62 = load i32, i32* %7
store i32 %62, i32* %8
br label %63
63: ; preds = %86, %61
%64 = load i32, i32* %9
%65 = load i32, i32* %8
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %90
67: ; preds = %63
%68 = load i32, i32* %10
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i32, i32* %8
%74 = load i32, i32* %9
%75 = sub nsw i32 %73, %74
%76 = call i32 @write(i32 %68, i8* %72, i32 %75)
store i32 %76, i32* %7
%77 = load i32, i32* %7
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %86
79: ; preds = %67
%80 = load i32, i32* @stderr
%81 = load i32, i32* @errno
%82 = call i8* @strerror(i32 %81)
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %82)
%84 = load i32, i32* @errno
%85 = sub nsw i32 0, %84
store i32 %85, i32* %7
br label %92
86: ; preds = %67
%87 = load i32, i32* %7
%88 = load i32, i32* %9
%89 = add nsw i32 %88, %87
store i32 %89, i32* %9
br label %63
90: ; preds = %63
br label %43
91: ; preds = %59
store i32 0, i32* %7
br label %92
92: ; preds = %91, %79, %49
%93 = load i32, i32* %10
%94 = call i32 @close(i32 %93)
%95 = load i32, i32* %7
store i32 %95, i32* %3
br label %96
96: ; preds = %92, %35, %13
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Invalid usage\0A\00"
@.str.1 = [18 x i8] c"Usage: %s <file>\0A\00"
@EINVAL = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [20 x i8] c"Failed to open: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.3 = [21 x i8] c"Failed to write: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
br label %62
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = tail call i32 @open(i8* %16, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %14
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @read(i32 %25, i8* %4, i32 4096)
%27 = icmp slt i32 %26, 0
br i1 %27, label %52, label %39
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
br label %62
35: ; preds = %42
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @read(i32 %36, i8* %4, i32 4096)
%38 = icmp slt i32 %37, 0
br i1 %38, label %52, label %39
39: ; preds = %24, %35
%40 = phi i32 [ %37, %35 ], [ %26, %24 ]
%41 = icmp eq i32 %40, 0
br i1 %41, label %59, label %44
42: ; preds = %44
%43 = icmp sgt i32 %40, %51
br i1 %43, label %44, label %35
44: ; preds = %39, %42
%45 = phi i32 [ %51, %42 ], [ 0, %39 ]
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %46
%48 = sub nsw i32 %40, %45
%49 = call i32 @write(i32 %22, i8* %47, i32 %48)
%50 = icmp slt i32 %49, 0
%51 = add nsw i32 %49, %45
br i1 %50, label %52, label %42
52: ; preds = %35, %44, %24
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = tail call i8* @strerror(i32 %54)
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @errno
%58 = sub nsw i32 0, %57
br label %59
59: ; preds = %39, %52
%60 = phi i32 [ %58, %52 ], [ 0, %39 ]
%61 = tail call i32 @close(i32 %22)
br label %62
62: ; preds = %59, %28, %6
%63 = phi i32 [ %13, %6 ], [ %34, %28 ], [ %60, %59 ]
ret i32 %63
}