#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 */
scalar_t__ EAGAIN;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ ioctl(int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  char buf[20 + 1] = {0};
  int ret, fd, mode = O_RDONLY;
  if ((argc > 1) && (strcmp(argv[1], "nonblock") == 0)) {
    mode |= O_NONBLOCK;
    printf("+ nonblock read\n");
  } else {
    printf("+ block read\n");
  }
  fd = open("/dev/globalfifo", mode);
  if (fd != -1) {
    if (ioctl(fd, 0x1, 0) < 0)
      printf("ioctl command failed\n");
    printf("+ before read\n");
    ret = read(fd, buf, 20);
    printf("+ after read\n");
    if (ret < 0) {
      if (errno == EAGAIN)
        printf("+ no data can be read\n");
      else
        printf("read failure\n");
    } else if (ret == 0) {
      printf("no data\n");
    } else {
      buf[ret] = '\0';
      printf("+ read %d(bytes): %s\n", ret, buf);
    }
    close(fd);
  } else {
    printf("device open failure\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x15,%edx
call   1040 <memset@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ec <main+0x7c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe45(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    11ec <main+0x7c>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fa <main+0x8a>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x3c(%rbp),%esi
lea    0xe24(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
je     1304 <main+0x194>
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    123d <main+0xcd>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %eax,-0x34(%rbp)
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jge    12b7 <main+0x147>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    0x2d38(%rip),%rcx
cmp    (%rcx),%rax
jne    12a4 <main+0x134>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b2 <main+0x142>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f7 <main+0x187>
cmpl   $0x0,-0x34(%rbp)
jne    12d4 <main+0x164>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f2 <main+0x182>
movslq -0x34(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
mov    -0x34(%rbp),%esi
lea    -0x30(%rbp),%rdx
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f7 <main+0x187>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
jmp    1312 <main+0x1a2>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0xd(%rsp)
mov    0x2e53(%rip),%rax
mov    (%rax),%ebp
lea    0xe99(%rip),%rbx
cmp    $0x2,%edi
jl     11c0 <main+0x50>
mov    0x8(%rsi),%rdi
lea    0xe5a(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11c0 <main+0x50>
mov    0x2e31(%rip),%rax
or     (%rax),%ebp
lea    0xeeb(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xe3a(%rip),%rdi
mov    %ebp,%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1246 <main+0xd6>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    11fc <main+0x8c>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %eax,%ebp
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
js     1254 <main+0xe4>
je     1273 <main+0x103>
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xde1(%rip),%rdi
mov    %rsp,%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1288 <main+0x118>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
jmp    128f <main+0x11f>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <main+0x10c>
lea    0xe0f(%rip),%rdi
jmp    1283 <main+0x113>
lea    0xdf1(%rip),%rdi
jmp    1283 <main+0x113>
lea    0xdf0(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"nonblock\00"
@O_NONBLOCK = global i32 0
@.str.1 = [17 x i8] c"+ nonblock read\0A\00"
@.str.2 = [14 x i8] c"+ block read\0A\00"
@.str.3 = [16 x i8] c"/dev/globalfifo\00"
@.str.4 = [22 x i8] c"ioctl command failed\0A\00"
@.str.5 = [15 x i8] c"+ before read\0A\00"
@.str.6 = [14 x i8] c"+ after read\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.7 = [23 x i8] c"+ no data can be read\0A\00"
@.str.8 = [14 x i8] c"read failure\0A\00"
@.str.9 = [9 x i8] c"no data\0A\00"
@.str.10 = [22 x i8] c"+ read %d(bytes): %s\0A\00"
@.str.11 = [21 x i8] c"device open failure\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [21 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [21 x i8]* %6 to i8*
%11 = load i32, i32* @O_RDONLY
store i32 %11, i32* %9
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %25
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32, i32* @O_NONBLOCK
%22 = load i32, i32* %9
%23 = or i32 %22, %21
store i32 %23, i32* %9
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %27
25: ; preds = %14, %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32, i32* %9
%29 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %71
32: ; preds = %27
%33 = load i32, i32* %8
%34 = call i64 @ioctl(i32 %33, i32 1, i32 0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* %8
%41 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 20)
store i32 %42, i32* %7
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %38
%47 = load i64, i64* @errno
%48 = load i64, i64* @EAGAIN
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %54
52: ; preds = %46
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
br label %54
54: ; preds = %52, %50
br label %68
55: ; preds = %38
%56 = load i32, i32* %7
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %67
60: ; preds = %55
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %7
%65 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i32 %64, i8* %65)
br label %67
67: ; preds = %60, %58
br label %68
68: ; preds = %67, %54
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
br label %73
71: ; preds = %27
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0))
br label %73
73: ; preds = %71, %68
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"nonblock\00"
@O_NONBLOCK = global i32 0
@.str.3 = [16 x i8] c"/dev/globalfifo\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.10 = [22 x i8] c"+ read %d(bytes): %s\0A\00"
@str = [13 x i8] c"+ block read\00"
@str.12 = [20 x i8] c"device open failure\00"
@str.13 = [14 x i8] c"+ before read\00"
@str.14 = [13 x i8] c"+ after read\00"
@str.15 = [8 x i8] c"no data\00"
@str.16 = [13 x i8] c"read failure\00"
@str.17 = [22 x i8] c"+ no data can be read\00"
@str.18 = [21 x i8] c"ioctl command failed\00"
@str.19 = [16 x i8] c"+ nonblock read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [21 x i8]
%4 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %5
br label %15
15: ; preds = %2, %7, %12
%16 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.19, i64 0, i64 0), %12 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0), %7 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0), %2 ]
%17 = phi i32 [ %14, %12 ], [ %5, %7 ], [ %5, %2 ]
%18 = tail call i32 @puts(i8* dereferenceable(1) %16)
%19 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%20 = icmp eq i32 %19, -1
br i1 %20, label %49, label %21
21: ; preds = %15
%22 = tail call i64 @ioctl(i32 %19, i32 1, i32 0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.18, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
%28 = call i32 @read(i32 %19, i8* %4, i32 20)
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.14, i64 0, i64 0))
%30 = icmp slt i32 %28, 0
br i1 %30, label %31, label %39
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.17, i64 0, i64 0))
br label %47
37: ; preds = %31
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.16, i64 0, i64 0))
br label %47
39: ; preds = %26
%40 = icmp eq i32 %28, 0
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.15, i64 0, i64 0))
br label %47
43: ; preds = %39
%44 = zext i32 %28 to i64
%45 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 %44
store i8 0, i8* %45
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i32 %28, i8* %4)
br label %47
47: ; preds = %41, %43, %35, %37
%48 = call i32 @close(i32 %19)
br label %51
49: ; preds = %15
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
br label %51
51: ; preds = %49, %47
ret i32 0
}