#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);
void 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
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11a5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe76(%rip),%rdi
call   1050 <printf@plt>
lea    0xe80(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1213 <main+0xb3>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    0x2def(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     11fd <main+0x9d>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
jmp    1209 <main+0xa9>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    1218 <main+0xb8>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    127d <main+0x11d>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    0x2d85(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     1267 <main+0x107>
lea    0xe0e(%rip),%rdi
call   1040 <puts@plt>
jmp    1273 <main+0x113>
lea    0xe28(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    12d0 <main+0x170>
movl   $0x0,-0x1024(%rbp)
movslq -0x1024(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12cb <main+0x16b>
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    1292 <main+0x132>
jmp    12d0 <main+0x170>
jmp    12d5 <main+0x175>
cmpq   $0x0,-0x20(%rbp)
ja     1218 <main+0xb8>
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    1217 <main+0xb7>
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     122f <main+0xcf>
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     11e2 <main+0x82>
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>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    0x2dde(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
lea    0xe99(%rip),%rax
lea    0xe67(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    (%rsi),%rsi
lea    0xddf(%rip),%rdi
call   1050 <printf@plt>
lea    0xde9(%rip),%rdi
jmp    1208 <main+0xa8>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    0x2d91(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
lea    0xe15(%rip),%rax
lea    0xdfd(%rip),%rdi
jmp    1204 <main+0xa4>

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 void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i64
%7 = alloca [4096 x i8]
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @puts(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %21, i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %18
%29 = load i32, i32* @EAGAIN
store i32 %29, i32* @errno
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %35
33: ; preds = %28
%34 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %18
br label %38
38: ; preds = %73, %37
%39 = load i32, i32* %5
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%41 = call i64 @read(i32 %39, i8* %40, i32 4096)
store i64 %41, i64* %6
%42 = load i64, i64* %6
%43 = icmp eq i64 %42, -1
br i1 %43, label %44, label %53
44: ; preds = %38
%45 = load i32, i32* @EAGAIN
store i32 %45, i32* @errno
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %51
49: ; preds = %44
%50 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %47
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %38
%54 = load i64, i64* %6
%55 = icmp ugt i64 %54, 0
br i1 %55, label %56, label %72
56: ; preds = %53
store i32 0, i32* %8
br label %57
57: ; preds = %68, %56
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = load i64, i64* %6
%61 = icmp ult i64 %59, %60
br i1 %61, label %62, label %71
62: ; preds = %57
%63 = load i32, i32* %8
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %64
%66 = load i8, i8* %65
%67 = call i32 @putchar(i8 signext %66)
br label %68
68: ; preds = %62
%69 = load i32, i32* %8
%70 = add nsw i32 %69, 1
store i32 %70, i32* %8
br label %57
71: ; preds = %57
br label %72
72: ; preds = %71, %53
br label %73
73: ; preds = %72
%74 = load i64, i64* %6
%75 = icmp ugt i64 %74, 0
br i1 %75, label %38, label %76
76: ; preds = %73
ret void
}

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 void @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 void
}