#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 */
struct stat {
  size_t st_size;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, size_t);
scalar_t__ read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  struct stat statbuf;
  char buf[1024];
  size_t bytes_read = 0;
  ssize_t rdlen;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <filename>\n", argv[0]);
    exit(9);
  }
  if (stat(argv[1], &statbuf) < 0) {
    fprintf(stderr, "error: stat: %s\n", strerror(errno));
    exit(9);
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    fprintf(stderr, "error: open: %s\n", strerror(errno));
    exit(9);
  }
  do {
    if ((rdlen = read(fd, buf, sizeof(buf))) < 0) {
      fprintf(stderr, "error: read: %s\n", strerror(errno));
      exit(9);
    }
    bytes_read += rdlen;
  } while (rdlen > 0);
  if (statbuf.st_size != bytes_read) {
    fprintf(stderr, "error: stat length and file length differ\n");
    exit(9);
  }
  if (close(fd) < 0) {
    fprintf(stderr, "error: close: %s\n", strerror(errno));
    exit(9);
  }
  printf("read %zu bytes\n", bytes_read);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x428(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d6 <main+0x56>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    122f <main+0xaf>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x438(%rbp),%edi
mov    %rax,%rdx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
jge    1292 <main+0x112>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x43c(%rbp),%edi
mov    %rax,%rdx
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
jmp    1297 <main+0x117>
mov    -0x434(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
jge    12fd <main+0x17d>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x440(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x440(%rbp),%edi
mov    %rax,%rdx
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    -0x430(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x430(%rbp)
jg     1297 <main+0x117>
mov    -0x18(%rbp),%rax
cmp    -0x428(%rbp),%rax
je     1352 <main+0x1d2>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
lea    0xd08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    -0x434(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    13a5 <main+0x225>
mov    0x2c6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
mov    0x2c53(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    %rax,%rdx
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    -0x428(%rbp),%rsi
lea    0xcd3(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1268 <main+0xe8>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     127d <main+0xfd>
mov    0x8(%rbx),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     129d <main+0x11d>
mov    %eax,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1234 <main+0xb4>
add    %rax,%rbx
test   %rax,%rax
jne    11e0 <main+0x60>
cmp    %rbx,0x8(%rsp)
jne    12bd <main+0x13d>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
js     12de <main+0x15e>
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d9d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd85(%rip),%rsi
jmp    1257 <main+0xd7>
mov    0x2d54(%rip),%rax
mov    (%rax),%ebx
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd7b(%rip),%rsi
jmp    1252 <main+0xd2>
mov    0x2d34(%rip),%rax
mov    (%rax),%ebx
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd6c(%rip),%rsi
jmp    1252 <main+0xd2>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x9,%edi
call   1090 <exit@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd78(%rip),%rsi
jmp    1252 <main+0xd2>

LLVM IR (-O0):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@.str.1 = [17 x i8] c"error: stat: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c"error: open: %s\0A\00"
@.str.3 = [17 x i8] c"error: read: %s\0A\00"
@.str.4 = [43 x i8] c"error: stat length and file length differ\0A\00"
@.str.5 = [18 x i8] c"error: close: %s\0A\00"
@.str.6 = [16 x i8] c"read %zu bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca [1024 x i8]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %8
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%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 ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 9)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @stat(i8* %23, %struct.stat* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 9)
unreachable
32: ; preds = %20
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %10
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 9)
unreachable
45: ; preds = %32
br label %46
46: ; preds = %61, %45
%47 = load i32, i32* %10
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = call i64 @read(i32 %47, i8* %48, i32 1024)
store i64 %49, i64* %9
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %54)
%56 = call i32 @exit(i32 9)
unreachable
57: ; preds = %46
%58 = load i64, i64* %9
%59 = load i64, i64* %8
%60 = add i64 %59, %58
store i64 %60, i64* %8
br label %61
61: ; preds = %57
%62 = load i64, i64* %9
%63 = icmp sgt i64 %62, 0
br i1 %63, label %46, label %64
64: ; preds = %61
%65 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%66 = load i64, i64* %65
%67 = load i64, i64* %8
%68 = icmp ne i64 %66, %67
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%72 = call i32 @exit(i32 9)
unreachable
73: ; preds = %64
%74 = load i32, i32* %10
%75 = call i64 @close(i32 %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %83
77: ; preds = %73
%78 = load i32, i32* @stderr
%79 = load i32, i32* @errno
%80 = call i8* @strerror(i32 %79)
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %80)
%82 = call i32 @exit(i32 9)
unreachable
83: ; preds = %73
%84 = load i64, i64* %8
%85 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i64 %84)
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@.str.1 = [17 x i8] c"error: stat: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c"error: open: %s\0A\00"
@.str.3 = [17 x i8] c"error: read: %s\0A\00"
@.str.4 = [43 x i8] c"error: stat length and file length differ\0A\00"
@.str.5 = [18 x i8] c"error: close: %s\0A\00"
@.str.6 = [16 x i8] c"read %zu bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 9)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @stat(i8* %15, %struct.stat* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 9)
unreachable
24: ; preds = %13
%25 = load i8*, i8** %14
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %24
%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 ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 9)
unreachable
35: ; preds = %24, %45
%36 = phi i64 [ %46, %45 ], [ 0, %24 ]
%37 = call i64 @read(i32 %27, i8* %6, i32 1024)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 9)
unreachable
45: ; preds = %35
%46 = add i64 %37, %36
%47 = icmp eq i64 %37, 0
br i1 %47, label %48, label %35
48: ; preds = %45
%49 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%50 = load i64, i64* %49
%51 = icmp eq i64 %50, %46
br i1 %51, label %56, label %52
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 9)
unreachable
56: ; preds = %48
%57 = call i64 @close(i32 %27)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %62)
%64 = call i32 @exit(i32 9)
unreachable
65: ; preds = %56
%66 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i64 %46)
ret i32 0
}