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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
void readfifo() {
  char buf[100];
  memset(buf, 0, sizeof(buf));
  int fd = open("/home/lennon/share/20150214/fifo1", O_RDONLY | O_NONBLOCK);
  printf("hello....");
  if (fd == -1) {
    printf("%m\n");
    exit(-1);
  }
  int len = 0;
  while ((len = read(fd, buf, sizeof(buf))) > 0) {
    printf("%s\n", buf);
    memset(buf, 0, sizeof(buf));
  }
  close(fd);
}


x86 assembly (clang, -O0):
<readfifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x74(%rbp)
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xffffffff,-0x74(%rbp)
jne    11cc <readfifo+0x6c>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
movl   $0x0,-0x78(%rbp)
mov    -0x74(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x78(%rbp)
cmp    $0x0,%eax
jle    1217 <readfifo+0xb7>
lea    -0x70(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
jmp    11d3 <readfifo+0x73>
mov    -0x74(%rbp),%edi
call   1050 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfifo>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    0x2e54(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%ebp
je     1208 <readfifo+0x98>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    11fa <readfifo+0x8a>
mov    %rsp,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     11d0 <readfifo+0x60>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"/home/lennon/share/20150214/fifo1\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [10 x i8] c"hello....\00"
@.str.2 = [4 x i8] c"%m\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define void @readfifo() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 100)
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
store i32 0, i32* %3
br label %17
17: ; preds = %22, %16
%18 = load i32, i32* %2
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 100)
store i32 %20, i32* %3
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 100)
br label %17
27: ; preds = %17
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"/home/lennon/share/20150214/fifo1\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [10 x i8] c"hello....\00"
@.str.2 = [4 x i8] c"%m\0A\00"
define void @readfifo() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 100)
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = call i32 @open(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32 %7, -1
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = call i32 @read(i32 %7, i8* %2, i32 100)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %16, label %21
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %10, %16
%17 = call i32 @puts(i8* %2)
%18 = call i32 @memset(i8* %2, i32 0, i32 100)
%19 = call i32 @read(i32 %7, i8* %2, i32 100)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %16, label %21
21: ; preds = %16, %10
%22 = call i32 @close(i32 %7)
ret void
}