#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PIPE_BUF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int main(void) {
  int fd;
  int len;
  char buf[PIPE_BUF];
  mode_t mode = 0666;
  if ((fd = open("fifo1", O_RDONLY)) < 0) {
    perror("open");
    exit(1);
  }
  while ((len = read(fd, buf, PIPE_BUF)) > 0)
    printf("read_fifo read: %s", buf);
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x1b6,-0x24(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d9 <main+0x79>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11de <main+0x7e>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1214 <main+0xb4>
mov    -0x30(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1030 <printf@plt>
jmp    11de <main+0x7e>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e66(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    11b6 <main+0x56>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r14d
mov    (%r12),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
test   %eax,%eax
jle    11fe <main+0x9e>
lea    0xe39(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    (%r12),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
test   %eax,%eax
jg     11e0 <main+0x80>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PIPE_BUF = global i32 0
@.str = [6 x i8] c"fifo1\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [19 x i8] c"read_fifo read: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PIPE_BUF
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
store i32 438, i32* %6
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
br label %18
18: ; preds = %23, %17
%19 = load i32, i32* %2
%20 = load i32, i32* @PIPE_BUF
%21 = call i32 @read(i32 %19, i8* %10, i32 %20)
store i32 %21, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %18
25: ; preds = %18
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
@.str = [6 x i8] c"fifo1\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [19 x i8] c"read_fifo read: %s\00"
define i32 @main() {
%1 = load i32, i32* @PIPE_BUF
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i32, i32* @PIPE_BUF
%9 = call i32 @read(i32 %5, i8* %3, i32 %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %14, label %19
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7, %14
%15 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%16 = load i32, i32* @PIPE_BUF
%17 = call i32 @read(i32 %5, i8* %3, i32 %16)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %14, label %19
19: ; preds = %14, %7
%20 = call i32 @close(i32 %5)
%21 = call i32 @exit(i32 0)
unreachable
}