#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 */
struct sigaction {
  int /*<<< orphan*/ sa_mask;
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_handler;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int child_process(char *, int, int);
int /*<<< orphan*/ close(int);
int fork();
int getpid();
int parent_process(char *, int, int, int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  char mqname[50];
  pid_t pid;
  int to_parent[2];
  int to_child[2];
  int rval;
  struct sigaction sa;
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);
  sigaction(SIGCHLD, &sa, NULL);
  sprintf(mqname,
          "/"
          "mq_unlink"
          "_"
          "2-2"
          "_%d",
          getpid());
  rval = pipe(to_parent);
  if (rval == -1) {
    perror("unexpected error: "
           "mq_unlink"
           " "
           "2-2"
           ": "
           "fd[0]");
    return 2;
  }
  rval = pipe(to_child);
  if (rval == -1) {
    perror("unexpected error: "
           "mq_unlink"
           " "
           "2-2"
           ": "
           "fd[1]");
    return 2;
  }
  pid = fork();
  if (pid == -1) {
    perror("unexpected error: "
           "mq_unlink"
           " "
           "2-2"
           ": "
           "fork");
    return 2;
  }
  if (pid == 0) {
    close(to_parent[0]);
    close(to_child[1]);
    return child_process(mqname, to_child[0], to_parent[1]);
  } else {
    close(to_parent[1]);
    close(to_child[0]);
    return parent_process(mqname, to_parent[0], to_child[1], pid);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x70(%rbp),%rdi
call   1090 <sigemptyset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xdff(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x4c(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    1234 <main+0x94>
lea    0xdef(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e4 <main+0x144>
lea    -0x54(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    1262 <main+0xc2>
lea    0xde8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e4 <main+0x144>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    128e <main+0xee>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e4 <main+0x144>
cmpl   $0x0,-0x44(%rbp)
jne    12bf <main+0x11f>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    -0x50(%rbp),%edi
call   1070 <close@plt>
lea    -0x40(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    -0x48(%rbp),%edx
call   1060 <child_process@plt>
mov    %eax,-0x4(%rbp)
jmp    12e4 <main+0x144>
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
mov    -0x54(%rbp),%edi
call   1070 <close@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x50(%rbp),%edx
mov    -0x44(%rbp),%ecx
call   1040 <parent_process@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigemptyset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe17(%rip),%rsi
lea    0x30(%rsp),%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     1273 <main+0xd3>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xdc>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     124d <main+0xad>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1285 <main+0xe5>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0x30(%rsp),%rdi
mov    %ebx,%ecx
call   1040 <parent_process@plt>
jmp    1296 <main+0xf6>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%esi
mov    0x14(%rsp),%edx
lea    0x30(%rsp),%rdi
call   1060 <child_process@plt>
jmp    1296 <main+0xf6>
lea    0xd98(%rip),%rdi
jmp    128c <main+0xec>
lea    0xdb6(%rip),%rdi
jmp    128c <main+0xec>
lea    0xdd4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%eax
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@.str = [18 x i8] c"/mq_unlink_2-2_%d\00"
@.str.1 = [39 x i8] c"unexpected error: mq_unlink 2-2: fd[0]\00"
@.str.2 = [39 x i8] c"unexpected error: mq_unlink 2-2: fd[1]\00"
@.str.3 = [38 x i8] c"unexpected error: mq_unlink 2-2: fork\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca %struct.sigaction
store i32 0, i32* %1
%8 = load i32, i32* @SIG_IGN
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%12 = call i32 @sigemptyset(i32* %11)
%13 = load i32, i32* @SIGCHLD
%14 = call i32 @sigaction(i32 %13, %struct.sigaction* %7, i32* null)
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%16 = call i32 (...) @getpid()
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%19 = call i32 @pipe(i32* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %67
24: ; preds = %0
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%26 = call i32 @pipe(i32* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %67
31: ; preds = %24
%32 = call i32 (...) @fork()
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %67
37: ; preds = %31
%38 = load i32, i32* %3
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %37
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%49 = load i32, i32* %48
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @child_process(i8* %47, i32 %49, i32 %51)
store i32 %52, i32* %1
br label %67
53: ; preds = %37
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%64 = load i32, i32* %63
%65 = load i32, i32* %3
%66 = call i32 @parent_process(i8* %60, i32 %62, i32 %64, i32 %65)
store i32 %66, i32* %1
br label %67
67: ; preds = %53, %40, %35, %29, %22
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@.str = [18 x i8] c"/mq_unlink_2-2_%d\00"
@.str.1 = [39 x i8] c"unexpected error: mq_unlink 2-2: fd[0]\00"
@.str.2 = [39 x i8] c"unexpected error: mq_unlink 2-2: fd[1]\00"
@.str.3 = [38 x i8] c"unexpected error: mq_unlink 2-2: fork\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca %struct.sigaction
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%6 = bitcast [2 x i32]* %2 to i8*
%7 = bitcast [2 x i32]* %3 to i8*
%8 = bitcast %struct.sigaction* %4 to i8*
%9 = load i32, i32* @SIG_IGN
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%13 = call i32 @sigemptyset(i32* %12)
%14 = load i32, i32* @SIGCHLD
%15 = call i32 @sigaction(i32 %14, %struct.sigaction* %4, i32* null)
%16 = call i32 (...) @getpid()
%17 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%19 = call i32 @pipe(i32* %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %53
23: ; preds = %0
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%25 = call i32 @pipe(i32* %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %53
29: ; preds = %23
%30 = call i32 (...) @fork()
switch i32 %30, label %43 [
i32 -1, label %31
i32 0, label %33
]
31: ; preds = %29
%32 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %53
33: ; preds = %29
%34 = load i32, i32* %18
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %24
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @child_process(i8* %5, i32 %39, i32 %41)
br label %53
43: ; preds = %29
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %24
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %18
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @parent_process(i8* %5, i32 %49, i32 %51, i32 %30)
br label %53
53: ; preds = %43, %33, %31, %27, %21
%54 = phi i32 [ 2, %21 ], [ 2, %27 ], [ 2, %31 ], [ %42, %33 ], [ %52, %43 ]
ret i32 %54
}