#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 ssize_t;

/* Variables and functions */
int MAP_ANONYMOUS;
unsigned char *MAP_FAILED;
int MAP_PRIVATE;
int O_CREAT;
int O_WRONLY;
int PROT_READ;
int PROT_WRITE;
scalar_t__ feof(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(unsigned char *, int, size_t const, int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, size_t const);
unsigned char *mmap(int /*<<< orphan*/ *, size_t const, int, int,
                    int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, size_t const);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int write(int, unsigned char *, size_t const);
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Expected 1 argument: the file to read.\n");
    return 1;
  }
  int fd = open(argv[1], O_WRONLY | O_CREAT | 00040000, 0644);
  if (fd == -1) {
    perror("failed to open file");
    return 1;
  }
  const size_t buf_size = 4096;
  unsigned char *buf = mmap(NULL, buf_size, PROT_READ | PROT_WRITE,
                            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  if (buf == MAP_FAILED) {
    perror("mmap failed");
    return 1;
  }
  size_t total_size = 0;
  while (1) {
    if (feof(stdin)) {
      break;
    }
    memset(buf, 0, buf_size);
    size_t amt_read = fread(buf, 1, buf_size, stdin);
    if (ferror(stdin)) {
      perror("failed to read stdin");
      return 1;
    }
    if (amt_read == 0) {
      continue;
    }
    total_size += amt_read;
    ssize_t res = write(fd, buf, buf_size);
    if (res == -1) {
      perror("failed to write");
      return 1;
    }
    if (res != buf_size) {
      fprintf(stderr, "Failed to write exactly %lu bytes",
              (unsigned long)amt_read);
    }
  }
  munmap(buf, buf_size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e3 <main+0x43>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13be <main+0x21e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
or     $0x4000,%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1232 <main+0x92>
lea    0xe07(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13be <main+0x21e>
movq   $0x1000,-0x20(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    0x2d6e(%rip),%rax
or     (%rax),%edx
mov    0x2d7d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d54(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1060 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d27(%rip),%rcx
cmp    (%rcx),%rax
jne    12a2 <main+0x102>
lea    0xdab(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13be <main+0x21e>
movq   $0x0,-0x30(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1090 <feof@plt>
cmp    $0x0,%rax
je     12c7 <main+0x127>
jmp    13a9 <main+0x209>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    0x2cf6(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1327 <main+0x187>
lea    0xd32(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13be <main+0x21e>
cmpq   $0x0,-0x38(%rbp)
jne    1337 <main+0x197>
jmp    12aa <main+0x10a>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <write@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1379 <main+0x1d9>
lea    0xcf5(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13be <main+0x21e>
movslq -0x3c(%rbp),%rax
cmp    $0x1000,%rax
je     13a4 <main+0x204>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xcd0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12aa <main+0x10a>
mov    -0x28(%rbp),%rdi
mov    $0x1000,%esi
call   10a0 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1255 <main+0xb5>
mov    0x8(%rsi),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
or     $0x4000,%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1308 <main+0x168>
mov    %eax,%r15d
mov    0x2e03(%rip),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd1(%rip),%rax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x1000,%esi
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2d82(%rip),%rax
cmp    (%rax),%rbx
je     1311 <main+0x171>
mov    0x2da2(%rip),%r12
mov    (%r12),%edi
call   1090 <feof@plt>
test   %rax,%rax
je     1280 <main+0xe0>
mov    $0x1000,%esi
mov    %rbx,%rdi
call   10a0 <munmap@plt>
xor    %eax,%eax
jmp    1271 <main+0xd1>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d39(%rip),%r13
lea    0xddf(%rip),%r14
jmp    129e <main+0xfe>
mov    (%r12),%edi
call   1090 <feof@plt>
test   %rax,%rax
jne    1244 <main+0xa4>
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    (%r12),%ecx
mov    $0x1000,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rbp
mov    (%r12),%edi
call   1030 <ferror@plt>
test   %rax,%rax
jne    131a <main+0x17a>
test   %rbp,%rbp
je     1290 <main+0xf0>
mov    $0x1000,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
cmp    $0x1000,%eax
je     1290 <main+0xf0>
cmp    $0xffffffff,%eax
je     1323 <main+0x183>
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1290 <main+0xf0>
lea    0xd19(%rip),%rdi
jmp    132a <main+0x18a>
lea    0xd24(%rip),%rdi
jmp    132a <main+0x18a>
lea    0xd27(%rip),%rdi
jmp    132a <main+0x18a>
lea    0xd33(%rip),%rdi
call   10c0 <perror@plt>
jmp    126c <main+0xcc>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Expected 1 argument: the file to read.\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [20 x i8] c"failed to open file\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"mmap failed\00"
@stdin = global i32 0
@.str.3 = [21 x i8] c"failed to read stdin\00"
@.str.4 = [16 x i8] c"failed to write\00"
@.str.5 = [34 x i8] c"Failed to write exactly %lu bytes\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %87
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_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = or i32 %23, 16384
%25 = call i32 @open(i8* %20, i32 %24, i32 420)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %87
30: ; preds = %17
store i64 4096, i64* %7
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %31, %32
%34 = load i32, i32* @MAP_ANONYMOUS
%35 = load i32, i32* @MAP_PRIVATE
%36 = or i32 %34, %35
%37 = call i8* @mmap(i32* null, i64 4096, i32 %33, i32 %36, i32 0, i32 0)
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %43
41: ; preds = %30
%42 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %87
43: ; preds = %30
store i64 0, i64* %9
br label %44
44: ; preds = %43, %63, %83
%45 = load i32, i32* @stdin
%46 = call i64 @feof(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
br label %84
49: ; preds = %44
%50 = load i8*, i8** %8
%51 = call i32 @memset(i8* %50, i32 0, i64 4096)
%52 = load i8*, i8** %8
%53 = load i32, i32* @stdin
%54 = call i64 @fread(i8* %52, i32 1, i64 4096, i32 %53)
store i64 %54, i64* %10
%55 = load i32, i32* @stdin
%56 = call i64 @ferror(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %49
%59 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %87
60: ; preds = %49
%61 = load i64, i64* %10
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %60
br label %44
64: ; preds = %60
%65 = load i64, i64* %10
%66 = load i64, i64* %9
%67 = add i64 %66, %65
store i64 %67, i64* %9
%68 = load i32, i32* %6
%69 = load i8*, i8** %8
%70 = call i32 @write(i32 %68, i8* %69, i64 4096)
store i32 %70, i32* %11
%71 = load i32, i32* %11
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %64
%74 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %87
75: ; preds = %64
%76 = load i32, i32* %11
%77 = sext i32 %76 to i64
%78 = icmp ne i64 %77, 4096
br i1 %78, label %79, label %83
79: ; preds = %75
%80 = load i32, i32* @stderr
%81 = load i64, i64* %10
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %81)
br label %83
83: ; preds = %79, %75
br label %44
84: ; preds = %48
%85 = load i8*, i8** %8
%86 = call i32 @munmap(i8* %85, i64 4096)
store i32 0, i32* %3
br label %87
87: ; preds = %84, %73, %58, %41, %28, %14
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Expected 1 argument: the file to read.\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [20 x i8] c"failed to open file\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"mmap failed\00"
@stdin = global i32 0
@.str.3 = [21 x i8] c"failed to read stdin\00"
@.str.4 = [16 x i8] c"failed to write\00"
@.str.5 = [34 x i8] c"Failed to write exactly %lu bytes\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %58
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = or i32 %12, 16384
%14 = tail call i32 @open(i8* %9, i32 %13, i32 420)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %58
18: ; preds = %7
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_ANONYMOUS
%23 = load i32, i32* @MAP_PRIVATE
%24 = or i32 %23, %22
%25 = tail call i8* @mmap(i32* null, i64 4096, i32 %21, i32 %24, i32 0, i32 0)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %32, label %28
28: ; preds = %18
%29 = load i32, i32* @stdin
%30 = tail call i64 @feof(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %56
32: ; preds = %18
%33 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %58
34: ; preds = %28, %52
%35 = tail call i32 @memset(i8* %25, i32 0, i64 4096)
%36 = load i32, i32* @stdin
%37 = tail call i64 @fread(i8* %25, i32 1, i64 4096, i32 %36)
%38 = load i32, i32* @stdin
%39 = tail call i64 @ferror(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %34
%42 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %58
43: ; preds = %34
%44 = icmp eq i64 %37, 0
br i1 %44, label %52, label %45
45: ; preds = %43
%46 = tail call i32 @write(i32 %14, i8* %25, i64 4096)
switch i32 %46, label %49 [
i32 -1, label %47
i32 4096, label %52
]
47: ; preds = %45
%48 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %58
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i64 %37)
br label %52
52: ; preds = %45, %49, %43
%53 = load i32, i32* @stdin
%54 = tail call i64 @feof(i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %34, label %56
56: ; preds = %52, %28
%57 = tail call i32 @munmap(i8* %25, i64 4096)
br label %58
58: ; preds = %47, %41, %16, %56, %32, %4
%59 = phi i32 [ 1, %4 ], [ 1, %16 ], [ 1, %32 ], [ 0, %56 ], [ 1, %41 ], [ 1, %47 ]
ret i32 %59
}