#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 pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ remove(char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
scalar_t__ write(int, char *, scalar_t__);
void mp_example3(void) {
  pid_t pid;
  pid_t tid;
  int ret;
  int pfd;
  printf("fifo\n");
  ret = mkfifo("./myfifo", 0666);
  if (ret) {
    perror("1 open fifo fail ");
    if (errno == EEXIST) {
      remove("./myfifo");
    }
    ret = mkfifo("./myfifo", 0666);
    if (ret) {
      perror("2 open fifo fail ");
      exit(-1);
    }
  }
  tid = fork();
  if (tid == 0) {
    printf("i'm child %d\n", getpid());
    int len;
    char buf[128];
    pfd = open("./myfifo", O_RDONLY);
    if (pfd < 0) {
      perror("open fifo fail ");
      exit(-1);
    }
    len = read(pfd, buf, sizeof(buf));
    buf[len] = '\0';
    printf("c recv %s\n", buf);
    exit(0);
  } else if (tid == -1) {
    perror("fork fail ");
    exit(-1);
  } else {
    pid = getpid();
    pfd = open("./myfifo", O_WRONLY);
    if (pfd < 0) {
      perror("open fifo fail ");
      exit(-2);
    }
    printf("i'm father %d\n", pid);
    if (write(pfd, "hello child\n", strlen("hello child\n")) <
        strlen("hello child\n"))
      perror("not all data wrote ");
    wait(NULL);
  }
  close(pfd);
}


x86 assembly (clang, -O0):
<mp_example3>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe16(%rip),%rdi
mov    $0x1b6,%esi
call   10a0 <mkfifo@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1272 <mp_example3+0xa2>
lea    0xe01(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
jne    1239 <mp_example3+0x69>
lea    0xdd2(%rip),%rdi
call   1030 <remove@plt>
lea    0xdc6(%rip),%rdi
mov    $0x1b6,%esi
call   10a0 <mkfifo@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     126d <mp_example3+0x9d>
lea    0xdc3(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    1272 <mp_example3+0xa2>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1314 <mp_example3+0x144>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
lea    0xd59(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12d5 <mp_example3+0x105>
lea    0xd4b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1090 <read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
cmpl   $0xffffffff,-0x8(%rbp)
jne    1334 <mp_example3+0x164>
lea    0xd27(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    0x2c7b(%rip),%rax
mov    (%rax),%esi
lea    0xcb8(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1376 <mp_example3+0x1a6>
lea    0xcaa(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffffe,%edi
call   10d0 <exit@plt>
mov    -0x4(%rbp),%esi
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    0xccf(%rip),%rdi
call   1060 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    %rax,%rdx
lea    0xcba(%rip),%rsi
call   1040 <write@plt>
mov    %rax,-0xa8(%rbp)
lea    0xca7(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0xa8(%rbp),%rax
cmp    %rcx,%rax
jge    13e3 <mp_example3+0x213>
lea    0xc95(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
jmp    13f1 <mp_example3+0x221>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_example3>:
push   %rbp
push   %rbx
sub    $0x88,%rsp
lea    0xe24(%rip),%rdi
call   1040 <puts@plt>
lea    0xe14(%rip),%rdi
mov    $0x1b6,%esi
call   10a0 <mkfifo@plt>
test   %eax,%eax
jne    127e <mp_example3+0xae>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
je     12dc <mp_example3+0x10c>
test   %eax,%eax
je     12e5 <mp_example3+0x115>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%ebp
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0xdd1(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     132b <mp_example3+0x15b>
mov    %eax,%ebx
lea    0xe0c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe0b(%rip),%rsi
mov    $0xc,%edx
mov    %ebx,%edi
call   1050 <write@plt>
cmp    $0xb,%rax
jle    12ce <mp_example3+0xfe>
xor    %edi,%edi
call   10e0 <wait@plt>
mov    %ebx,%edi
add    $0x88,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <close@plt>
lea    0xd84(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%rax
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%rax
jne    12ac <mp_example3+0xdc>
lea    0xd59(%rip),%rdi
call   1030 <remove@plt>
lea    0xd4d(%rip),%rdi
mov    $0x1b6,%esi
call   10a0 <mkfifo@plt>
test   %eax,%eax
je     11fe <mp_example3+0x2e>
lea    0xd4f(%rip),%rdi
jmp    131c <mp_example3+0x14c>
lea    0xd98(%rip),%rdi
call   10c0 <perror@plt>
jmp    1267 <mp_example3+0x97>
lea    0xd63(%rip),%rdi
jmp    131c <mp_example3+0x14c>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd3a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
lea    0xcf4(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
jns    1341 <mp_example3+0x171>
lea    0xcef(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xcd9(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffffe,%edi
call   10d0 <exit@plt>
mov    %rsp,%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1090 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xcdb(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"fifo\0A\00"
@.str.1 = [9 x i8] c"./myfifo\00"
@.str.2 = [18 x i8] c"1 open fifo fail \00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [18 x i8] c"2 open fifo fail \00"
@.str.4 = [14 x i8] c"i'm child %d\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [16 x i8] c"open fifo fail \00"
@.str.6 = [11 x i8] c"c recv %s\0A\00"
@.str.7 = [11 x i8] c"fork fail \00"
@O_WRONLY = global i32 0
@.str.8 = [15 x i8] c"i'm father %d\0A\00"
@.str.9 = [13 x i8] c"hello child\0A\00"
@.str.10 = [20 x i8] c"not all data wrote \00"
define void @mp_example3() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 438)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%13 = load i64, i64* @errno
%14 = load i64, i64* @EEXIST
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @remove(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 438)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %18
br label %26
26: ; preds = %25, %0
%27 = call i32 (...) @fork()
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %50
30: ; preds = %26
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %33)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %30
%41 = load i32, i32* %4
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 128)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %45
store i8 0, i8* %46
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %26
%51 = load i32, i32* %2
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %50
%57 = call i32 (...) @getpid()
store i32 %57, i32* %1
%58 = load i32, i32* @O_WRONLY
%59 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %58)
store i32 %59, i32* %4
%60 = load i32, i32* %4
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%64 = call i32 @exit(i32 -2)
unreachable
65: ; preds = %56
%66 = load i32, i32* %1
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %4
%69 = call i64 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%70 = call i64 @write(i32 %68, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i64 %69)
%71 = call i64 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%72 = icmp slt i64 %70, %71
br i1 %72, label %73, label %75
73: ; preds = %65
%74 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0))
br label %75
75: ; preds = %73, %65
%76 = call i32 @wait(i32* null)
br label %77
77: ; preds = %75
br label %78
78: ; preds = %77
%79 = load i32, i32* %4
%80 = call i32 @close(i32 %79)
ret void
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"./myfifo\00"
@.str.2 = [18 x i8] c"1 open fifo fail \00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [18 x i8] c"2 open fifo fail \00"
@.str.4 = [14 x i8] c"i'm child %d\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [16 x i8] c"open fifo fail \00"
@.str.6 = [11 x i8] c"c recv %s\0A\00"
@.str.7 = [11 x i8] c"fork fail \00"
@O_WRONLY = global i32 0
@.str.8 = [15 x i8] c"i'm father %d\0A\00"
@.str.9 = [13 x i8] c"hello child\0A\00"
@.str.10 = [20 x i8] c"not all data wrote \00"
@str = [5 x i8] c"fifo\00"
define void @mp_example3() {
%1 = alloca [128 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 438)
%4 = icmp eq i32 %3, 0
br i1 %4, label %18, label %5
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%7 = load i64, i64* @errno
%8 = load i64, i64* @EEXIST
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @remove(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %5
%13 = tail call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 438)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12, %0
%19 = tail call i32 (...) @fork()
switch i32 %19, label %39 [
i32 0, label %20
i32 -1, label %36
]
20: ; preds = %18
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %20
%31 = call i32 @read(i32 %25, i8* %23, i32 128)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %23)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %18
%37 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%38 = tail call i32 @exit(i32 -1)
unreachable
39: ; preds = %18
%40 = tail call i32 (...) @getpid()
%41 = load i32, i32* @O_WRONLY
%42 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%46 = tail call i32 @exit(i32 -2)
unreachable
47: ; preds = %39
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i32 %40)
%49 = tail call i64 @write(i32 %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i64 12)
%50 = icmp slt i64 %49, 12
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = tail call i32 @wait(i32* null)
%55 = tail call i32 @close(i32 %42)
ret void
}