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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_RDONLY;
int /*<<< orphan*/ PIPE_BUF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int /*<<< orphan*/);
int main() {
  int pipe_fd;
  int res;
  int open_mode = O_RDONLY;
  char buffer[PIPE_BUF + 1];
  int bytes_read = 0;
  memset(buffer, '\0', sizeof(buffer));
  printf("Process %d opening FIFO O_RDONLY\n", getpid());
  pipe_fd = open("/tmp/my_fifo", open_mode);
  printf("Process %d result %d\n", getpid(), pipe_fd);
  if (pipe_fd != -1) {
    do {
      res = read(pipe_fd, buffer, PIPE_BUF);
      bytes_read += res;
    } while (res > 0);
    (void)close(pipe_fd);
  } else {
    exit(EXIT_FAILURE);
  }
  printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);
  exit(EXIT_SUCCESS);
}


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    %eax,-0x10(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe37(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     1254 <main+0xe4>
jmp    121c <main+0xac>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     121c <main+0xac>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    1262 <main+0xf2>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x24(%rbp),%edx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e66(%rip),%rax
mov    (%rax),%r15d
mov    0x2e3c(%rip),%r12
mov    (%r12),%edx
add    $0x1,%edx
mov    %rsp,%r14
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe54(%rip),%rdi
mov    %r15d,%esi
call   1080 <open@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe48(%rip),%rdi
mov    %eax,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%r15d
jne    1210 <main+0xa0>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %r15d,%edi
mov    %r14,%rsi
call   1070 <read@plt>
add    %eax,%ebx
test   %eax,%eax
jg     1210 <main+0xa0>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe0a(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@PIPE_BUF = global i32 0
@.str = [34 x i8] c"Process %d opening FIFO O_RDONLY\0A\00"
@.str.1 = [13 x i8] c"/tmp/my_fifo\00"
@.str.2 = [22 x i8] c"Process %d result %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [36 x i8] c"Process %d finished, %d bytes read\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
store i32 %8, i32* %4
%9 = load i32, i32* @PIPE_BUF
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
store i32 0, i32* %7
%14 = trunc i64 %11 to i32
%15 = call i32 @memset(i8* %13, i8 signext 0, i32 %14)
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %4
%19 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %2
%20 = call i32 (...) @getpid()
%21 = load i32, i32* %2
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32, i32* %2
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %39
25: ; preds = %0
br label %26
26: ; preds = %33, %25
%27 = load i32, i32* %2
%28 = load i32, i32* @PIPE_BUF
%29 = call i32 @read(i32 %27, i8* %13, i32 %28)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = load i32, i32* %7
%32 = add nsw i32 %31, %30
store i32 %32, i32* %7
br label %33
33: ; preds = %26
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %26, label %36
36: ; preds = %33
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
br label %42
39: ; preds = %0
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %36
%43 = call i32 (...) @getpid()
%44 = load i32, i32* %7
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@PIPE_BUF = global i32 0
@.str = [34 x i8] c"Process %d opening FIFO O_RDONLY\0A\00"
@.str.1 = [13 x i8] c"/tmp/my_fifo\00"
@.str.2 = [22 x i8] c"Process %d result %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [36 x i8] c"Process %d finished, %d bytes read\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = load i32, i32* @PIPE_BUF
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @memset(i8* %5, i8 signext 0, i32 %3)
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%10 = call i32 (...) @getpid()
%11 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %9)
%12 = icmp eq i32 %9, -1
br i1 %12, label %25, label %13
13: ; preds = %0, %13
%14 = phi i32 [ %17, %13 ], [ 0, %0 ]
%15 = load i32, i32* @PIPE_BUF
%16 = call i32 @read(i32 %9, i8* %5, i32 %15)
%17 = add nsw i32 %16, %14
%18 = icmp sgt i32 %16, 0
br i1 %18, label %13, label %19
19: ; preds = %13
%20 = call i32 @close(i32 %9)
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %17)
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %0
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
}