#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*/ val;
typedef int u_int32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
int main(int argc, char **argv) {
  u_int32_t val;
  int fd;
  int ret;
  if ((fd = open("/dev/fibo0", O_RDONLY)) < 0)
    err(1, "unable to open "
           "/dev/fibo0");
  while ((ret = read(fd, &val, sizeof(val))) == sizeof(val)) {
    printf("%u\n", val);
  }
  if (ret < 0)
    err(2, "read("
           "/dev/fibo0"
           ")");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1198 <main+0x48>
mov    $0x1,%edi
lea    0xe6d(%rip),%rsi
call   1060 <err@plt>
jmp    119d <main+0x4d>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cltq
cmp    $0x4,%rax
jne    11d1 <main+0x81>
mov    -0x14(%rbp),%esi
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
jmp    119d <main+0x4d>
cmpl   $0x0,-0x1c(%rbp)
jge    11ec <main+0x9c>
mov    $0x2,%edi
lea    0xe37(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xea6(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1185 <main+0x35>
lea    0xe85(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    11d0 <main+0x80>
lea    0xe78(%rip),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     11b0 <main+0x60>
test   %eax,%eax
jns    11e5 <main+0x95>
lea    0xe43(%rip),%rsi
mov    $0x2,%edi
call   1060 <err@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/fibo0\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"unable to open /dev/fibo0\00"
@.str.2 = [4 x i8] c"%u\0A\00"
@.str.3 = [17 x i8] c"read(/dev/fibo0)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%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* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
br label %15
15: ; preds = %20, %14
%16 = load i32, i32* %7
%17 = call i32 @read(i32 %16, i32* %6, i32 4)
store i32 %17, i32* %8
%18 = sext i32 %17 to i64
%19 = icmp eq i64 %18, 4
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %6
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %15
23: ; preds = %15
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @err(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/fibo0\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"unable to open /dev/fibo0\00"
@.str.2 = [4 x i8] c"%u\0A\00"
@.str.3 = [17 x i8] c"read(/dev/fibo0)\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = call i32 @read(i32 %6, i32* %3, i32 4)
%12 = icmp eq i32 %11, 4
br i1 %12, label %13, label %18
13: ; preds = %10, %13
%14 = load i32, i32* %3
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 @read(i32 %6, i32* %3, i32 4)
%17 = icmp eq i32 %16, 4
br i1 %17, label %13, label %18
18: ; preds = %13, %10
%19 = phi i32 [ %11, %10 ], [ %16, %13 ]
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = tail call i32 @close(i32 %6)
ret i32 0
}