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

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
size_t read(int, char *, int);
main(int argc, char *argv[]) {
  int fd;
  size_t bytes;
  char buffer[1024 * 4];
  if (argc != 2) {
    printf("Usage: %s <filename>\n", argv[0]);
    puts("Reads the content of a file, but doesn't wait for input");
    exit(-1);
  }
  fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (fd == -1) {
    if (errno = EAGAIN)
      puts("Open would block");
    else
      puts("Open failed");
    exit(-1);
  }
  do {
    int i;
    bytes = read(fd, buffer, 1024 * 4);
    if (bytes == -1) {
      if (errno = EAGAIN)
        puts("Normally I'd block, but you told me not to");
      else
        puts("Another read error");
      exit(-1);
    }
    if (bytes > 0) {
      for (i = 0; i < bytes; i++)
        putchar(buffer[i]);
    }
  } while (bytes > 0);
}


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     11ac <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121a <main+0xba>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2de8(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     1204 <main+0xa4>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
jmp    1210 <main+0xb0>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    121f <main+0xbf>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1284 <main+0x124>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    0x2d7e(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     126e <main+0x10e>
lea    0xe07(%rip),%rdi
call   1040 <puts@plt>
jmp    127a <main+0x11a>
lea    0xe21(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    12d7 <main+0x177>
movl   $0x0,-0x1024(%rbp)
movslq -0x1024(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12d2 <main+0x172>
movslq -0x1024(%rbp),%rax
movsbl -0x1020(%rbp,%rax,1),%edi
ff
call   1030 <putchar@plt>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    1299 <main+0x139>
jmp    12d7 <main+0x177>
jmp    12dc <main+0x17c>
cmpq   $0x0,-0x20(%rbp)
ja     121f <main+0xbf>
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
cmp    $0x2,%edi
jne    1219 <main+0xb9>
mov    0x8(%rsi),%rdi
mov    0x2e5f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1231 <main+0xd1>
mov    %eax,%r15d
mov    %rsp,%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %rax,%rax
je     11d4 <main+0x74>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     11e4 <main+0x84>
xor    %ebp,%ebp
movsbl (%rsp,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11c0 <main+0x60>
jmp    11a0 <main+0x40>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    0x2ddc(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
lea    0xe97(%rip),%rax
lea    0xe65(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    (%rsi),%rsi
lea    0xddd(%rip),%rdi
call   1050 <printf@plt>
lea    0xde7(%rip),%rdi
jmp    120a <main+0xaa>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    0x2d8f(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
lea    0xe13(%rip),%rax
lea    0xdfb(%rip),%rdi
jmp    1206 <main+0xa6>

LLVM IR (-O0):
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [56 x i8] c"Reads the content of a file, but doesn't wait for input\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@EAGAIN = global i32 0
@errno = global i32 0
@.str.2 = [17 x i8] c"Open would block\00"
@.str.3 = [12 x i8] c"Open failed\00"
@.str.4 = [43 x i8] c"Normally I'd block, but you told me not to\00"
@.str.5 = [19 x i8] c"Another read error\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 [4096 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @puts(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_NONBLOCK
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %22, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %38
29: ; preds = %19
%30 = load i32, i32* @EAGAIN
store i32 %30, i32* @errno
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %36
34: ; preds = %29
%35 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %19
br label %39
39: ; preds = %74, %38
%40 = load i32, i32* %6
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%42 = call i64 @read(i32 %40, i8* %41, i32 4096)
store i64 %42, i64* %7
%43 = load i64, i64* %7
%44 = icmp eq i64 %43, -1
br i1 %44, label %45, label %54
45: ; preds = %39
%46 = load i32, i32* @EAGAIN
store i32 %46, i32* @errno
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %52
50: ; preds = %45
%51 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %50, %48
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %39
%55 = load i64, i64* %7
%56 = icmp ugt i64 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %54
store i32 0, i32* %9
br label %58
58: ; preds = %69, %57
%59 = load i32, i32* %9
%60 = sext i32 %59 to i64
%61 = load i64, i64* %7
%62 = icmp ult i64 %60, %61
br i1 %62, label %63, label %72
63: ; preds = %58
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %65
%67 = load i8, i8* %66
%68 = call i32 @putchar(i8 signext %67)
br label %69
69: ; preds = %63
%70 = load i32, i32* %9
%71 = add nsw i32 %70, 1
store i32 %71, i32* %9
br label %58
72: ; preds = %58
br label %73
73: ; preds = %72, %54
br label %74
74: ; preds = %73
%75 = load i64, i64* %7
%76 = icmp ugt i64 %75, 0
br i1 %76, label %39, label %77
77: ; preds = %74
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [56 x i8] c"Reads the content of a file, but doesn't wait for input\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@EAGAIN = global i32 0
@errno = global i32 0
@.str.2 = [17 x i8] c"Open would block\00"
@.str.3 = [12 x i8] c"Open failed\00"
@.str.4 = [43 x i8] c"Normally I'd block, but you told me not to\00"
@.str.5 = [19 x i8] c"Another read error\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 %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %11
%20 = load i32, i32* @EAGAIN
store i32 %20, i32* @errno
%21 = icmp eq i32 %20, 0
%22 = select i1 %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0)
%23 = tail call i32 @puts(i8* dereferenceable(1) %22)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %33, %11
%26 = call i64 @read(i32 %17, i8* %4, i32 4096)
switch i64 %26, label %33 [
i64 -1, label %27
i64 0, label %40
]
27: ; preds = %25
%28 = load i32, i32* @EAGAIN
store i32 %28, i32* @errno
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0)
%31 = tail call i32 @puts(i8* dereferenceable(1) %30)
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %25, %33
%34 = phi i64 [ %38, %33 ], [ 0, %25 ]
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = tail call i32 @putchar(i8 signext %36)
%38 = add nuw i64 %34, 1
%39 = icmp eq i64 %38, %26
br i1 %39, label %25, label %33
40: ; preds = %25
ret i32 0
}