#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  char buf[4096];
  int ret;
  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_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open: %s\n", strerror(errno));
    return -errno;
  }
  while (1) {
    ret = read(fd, buf, sizeof(buf));
    if (ret < 0) {
      fprintf(stderr, "Failed to read: %s\n", strerror(errno));
      ret = -errno;
      goto end;
    } else if (ret == 0) {
      break;
    }
    ret = write(STDOUT_FILENO, buf, ret);
    if (ret < 0) {
      fprintf(stderr, "Failed to write: %s\n", strerror(errno));
      ret = -errno;
      goto end;
    }
  }
  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    1366 <main+0x206>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    123b <main+0xdb>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x101c(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x101c(%rbp),%edi
mov    %rax,%rdx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d97(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1366 <main+0x206>
jmp    1240 <main+0xe0>
mov    -0x1018(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    12b4 <main+0x154>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1020(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1020(%rbp),%edi
mov    %rax,%rdx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d21(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1014(%rbp)
jmp    1352 <main+0x1f2>
cmpl   $0x0,-0x1014(%rbp)
jne    12c6 <main+0x166>
jmp    1348 <main+0x1e8>
jmp    12cb <main+0x16b>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    1343 <main+0x1e3>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1024(%rbp),%edi
mov    %rax,%rdx
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2c92(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1014(%rbp)
jmp    1352 <main+0x1f2>
jmp    1240 <main+0xe0>
movl   $0x0,-0x1014(%rbp)
mov    -0x1018(%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    1210 <main+0xb0>
mov    0x8(%rbx),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1249 <main+0xe9>
mov    %eax,%ebx
mov    %rsp,%rbp
lea    0xe94(%rip),%r14
mov    0x2e48(%rip),%r12
lea    0xe9a(%rip),%r15
nop
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11de <main+0x7e>
je     127b <main+0x11b>
mov    (%r12),%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11b0 <main+0x50>
mov    %r15,%r14
mov    0x2deb(%rip),%rax
mov    (%rax),%r15d
mov    0x2dd9(%rip),%r12
mov    (%r12),%edi
call   1080 <strerror@plt>
xor    %ebp,%ebp
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
sub    (%r12),%ebp
jmp    127d <main+0x11d>
mov    0x2db9(%rip),%r14
mov    (%r14),%edi
lea    0xddf(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
mov    (%rbx),%rdx
lea    0xdd8(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d7b(%rip),%rax
sub    (%rax),%ebp
jmp    1284 <main+0x124>
mov    0x2d80(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6f(%rip),%r14
mov    (%r14),%edi
call   1080 <strerror@plt>
lea    0xdb9(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
sub    (%r14),%ebp
jmp    1284 <main+0x124>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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_RDONLY = global i32 0
@.str.2 = [20 x i8] c"Failed to open: %s\0A\00"
@errno = global i32 0
@.str.3 = [20 x i8] c"Failed to read: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %75
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %3
br label %75
36: ; preds = %21
br label %37
37: ; preds = %36, %69
%38 = load i32, i32* %8
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 4096)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
store i32 %49, i32* %7
br label %71
50: ; preds = %37
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
br label %70
54: ; preds = %50
br label %55
55: ; preds = %54
%56 = load i32, i32* @STDOUT_FILENO
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%58 = load i32, i32* %7
%59 = call i32 @write(i32 %56, i8* %57, i32 %58)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %55
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @errno
%68 = sub nsw i32 0, %67
store i32 %68, i32* %7
br label %71
69: ; preds = %55
br label %37
70: ; preds = %53
store i32 0, i32* %7
br label %71
71: ; preds = %70, %62, %43
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %7
store i32 %74, i32* %3
br label %75
75: ; preds = %71, %29, %11
%76 = load i32, i32* %3
ret i32 %76
}

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_RDONLY = global i32 0
@.str.2 = [20 x i8] c"Failed to open: %s\0A\00"
@errno = global i32 0
@.str.3 = [20 x i8] c"Failed to read: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [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 %47
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
br label %47
27: ; preds = %14, %32
%28 = call i32 @read(i32 %18, i8* %4, i32 4096)
%29 = icmp slt i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = icmp eq i32 %28, 0
br i1 %31, label %44, label %32
32: ; preds = %30
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @write(i32 %33, i8* %4, i32 %28)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %27
36: ; preds = %32, %27
%37 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %27 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), %32 ]
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* %37, i8* %40)
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
br label %44
44: ; preds = %30, %36
%45 = phi i32 [ %43, %36 ], [ 0, %30 ]
%46 = tail call i32 @close(i32 %18)
br label %47
47: ; preds = %44, %20, %6
%48 = phi i32 [ %13, %6 ], [ %26, %20 ], [ %45, %44 ]
ret i32 %48
}