#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ F_OK;
int O_WRONLY;
int /*<<< orphan*/ PIPE_BUF;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int mkfifo(char *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stderr;
int write(int, char *, int /*<<< orphan*/);
int main() {
  int pipe_fd;
  int res;
  int open_mode = O_WRONLY;
  int bytes_sent = 0;
  char buffer[PIPE_BUF + 1];
  if (access("/tmp/my_fifo", F_OK) == -1) {
    res = mkfifo("/tmp/my_fifo", 0777);
    if (res != 0) {
      fprintf(stderr, "Could not create fifo %s\n", "/tmp/my_fifo");
      exit(EXIT_FAILURE);
    }
  }
  printf("Process %d opening FIFO O_WRONLY\n", getpid());
  pipe_fd = open("/tmp/my_fifo", open_mode);
  printf("Process %d result %d\n", getpid(), pipe_fd);
  if (pipe_fd != -1) {
    while (bytes_sent < (1024 * 1024 * 10)) {
      res = write(pipe_fd, buffer, PIPE_BUF);
      if (res == -1) {
        fprintf(stderr, "Write error on pipe\n");
        exit(EXIT_FAILURE);
      }
      bytes_sent += res;
    }
    (void)close(pipe_fd);
  } else {
    exit(EXIT_FAILURE);
  }
  printf("Process %d finished\n", getpid());
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%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,-0x28(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
jne    124e <main+0xbe>
lea    0xdfa(%rip),%rdi
mov    $0x1ff,%esi
call   1080 <mkfifo@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1249 <main+0xb9>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
lea    0xdcc(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    124e <main+0xbe>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xd91(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     130f <main+0x17f>
jmp    12a0 <main+0x110>
cmpl   $0xa00000,-0x14(%rbp)
jge    1302 <main+0x172>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12f4 <main+0x164>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    12a0 <main+0x110>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    131d <main+0x18d>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e16(%rip),%rax
mov    (%rax),%r14d
mov    0x2e1c(%rip),%r12
mov    (%r12),%eax
add    $0x1,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xe2c(%rip),%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
jne    11f7 <main+0x67>
lea    0xe1b(%rip),%rdi
mov    $0x1ff,%esi
call   1080 <mkfifo@plt>
test   %eax,%eax
jne    129b <main+0x10b>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe20(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xde9(%rip),%rdi
mov    %r14d,%esi
call   1090 <open@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe19(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xffffffff,%r14d
je     12d2 <main+0x142>
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %r14d,%edi
mov    %r15,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12bb <main+0x12b>
add    %eax,%ebx
cmp    $0x9fffff,%ebx
jle    1250 <main+0xc0>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
lea    0xd4e(%rip),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12d2 <main+0x142>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@PIPE_BUF = global i32 0
@.str = [13 x i8] c"/tmp/my_fifo\00"
@F_OK = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Could not create fifo %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [34 x i8] c"Process %d opening FIFO O_WRONLY\0A\00"
@.str.3 = [22 x i8] c"Process %d result %d\0A\00"
@.str.4 = [21 x i8] c"Write error on pipe\0A\00"
@.str.5 = [21 x i8] c"Process %d finished\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 0, i32* %1
%8 = load i32, i32* @O_WRONLY
store i32 %8, i32* %4
store i32 0, i32* %5
%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** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* @F_OK
%15 = call i32 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %27
17: ; preds = %0
%18 = call i32 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 511)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %17
br label %27
27: ; preds = %26, %0
%28 = call i32 (...) @getpid()
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %4
%31 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 %31, i32* %2
%32 = call i32 (...) @getpid()
%33 = load i32, i32* %2
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32, i32* %2
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %59
37: ; preds = %27
br label %38
38: ; preds = %52, %37
%39 = load i32, i32* %5
%40 = icmp slt i32 %39, 10485760
br i1 %40, label %41, label %56
41: ; preds = %38
%42 = load i32, i32* %2
%43 = load i32, i32* @PIPE_BUF
%44 = call i32 @write(i32 %42, i8* %13, i32 %43)
store i32 %44, i32* %3
%45 = load i32, i32* %3
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %41
%53 = load i32, i32* %3
%54 = load i32, i32* %5
%55 = add nsw i32 %54, %53
store i32 %55, i32* %5
br label %38
56: ; preds = %38
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
br label %62
59: ; preds = %27
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %56
%63 = call i32 (...) @getpid()
%64 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %63)
%65 = load i32, i32* @EXIT_SUCCESS
%66 = call i32 @exit(i32 %65)
unreachable
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@PIPE_BUF = global i32 0
@.str = [13 x i8] c"/tmp/my_fifo\00"
@F_OK = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Could not create fifo %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [34 x i8] c"Process %d opening FIFO O_WRONLY\0A\00"
@.str.3 = [22 x i8] c"Process %d result %d\0A\00"
@.str.4 = [21 x i8] c"Write error on pipe\0A\00"
@.str.5 = [21 x i8] c"Process %d finished\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @PIPE_BUF
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @F_OK
%7 = tail call i32 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %0
%10 = tail call i32 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 511)
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9, %0
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %20)
%23 = icmp eq i32 %20, -1
br i1 %23, label %43, label %26
24: ; preds = %26
%25 = icmp slt i32 %31, 10485760
br i1 %25, label %26, label %37
26: ; preds = %17, %24
%27 = phi i32 [ %31, %24 ], [ 0, %17 ]
%28 = load i32, i32* @PIPE_BUF
%29 = call i32 @write(i32 %20, i8* %5, i32 %28)
%30 = icmp eq i32 %29, -1
%31 = add nsw i32 %29, %27
br i1 %30, label %32, label %24
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %24
%38 = tail call i32 @close(i32 %20)
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = load i32, i32* @EXIT_SUCCESS
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %17
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
}