#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 qname[50];
  pid_t pid;
  int rval;
  int to_parent[2];
  int to_child[2];
  struct sigaction sa;
  sprintf(qname,
          "/"
          "mq_close"
          "_"
          "2-1"
          "_%d",
          getpid());
  rval = pipe(to_parent);
  if (rval == -1) {
    perror("unexpected error: "
           "mq_close"
           " "
           "2-1"
           ": "
           "pipe (1)");
    return 2;
  }
  rval = pipe(to_child);
  if (rval == -1) {
    perror("unexpected error: "
           "mq_close"
           " "
           "2-1"
           ": "
           "pipe (2)");
    return 2;
  }
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);
  sigaction(SIGCHLD, &sa, NULL);
  pid = fork();
  if (pid == -1) {
    perror("unexpected error: "
           "mq_close"
           " "
           "2-1"
           ": "
           "fork");
    return 2;
  }
  if (pid == 0) {
    close(to_parent[0]);
    close(to_child[1]);
    return child_process(qname, to_child[0], to_parent[1]);
  } else {
    close(to_parent[1]);
    close(to_child[0]);
    return parent_process(qname, to_parent[0], to_child[1], pid);
  }
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xe32(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    1201 <main+0x61>
lea    0xe21(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e4 <main+0x144>
lea    -0x58(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    122f <main+0x8f>
lea    0xe1c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e4 <main+0x144>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x70(%rbp),%rdi
call   1090 <sigemptyset@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    128e <main+0xee>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e4 <main+0x144>
cmpl   $0x0,-0x44(%rbp)
jne    12bf <main+0x11f>
mov    -0x50(%rbp),%edi
call   1070 <close@plt>
mov    -0x54(%rbp),%edi
call   1070 <close@plt>
lea    -0x40(%rbp),%rdi
mov    -0x58(%rbp),%esi
mov    -0x4c(%rbp),%edx
call   1060 <child_process@plt>
mov    %eax,-0x4(%rbp)
jmp    12e4 <main+0x144>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    -0x58(%rbp),%edi
call   1070 <close@plt>
lea    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%esi
mov    -0x54(%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
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe4d(%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     127b <main+0xdb>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     1284 <main+0xe4>
mov    0x2df4(%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    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1255 <main+0xb5>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     128d <main+0xed>
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    129e <main+0xfe>
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    129e <main+0xfe>
lea    0xd8f(%rip),%rdi
jmp    1294 <main+0xf4>
lea    0xdaf(%rip),%rdi
jmp    1294 <main+0xf4>
lea    0xdcf(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%eax
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@.str = [17 x i8] c"/mq_close_2-1_%d\00"
@.str.1 = [41 x i8] c"unexpected error: mq_close 2-1: pipe (1)\00"
@.str.2 = [41 x i8] c"unexpected error: mq_close 2-1: pipe (2)\00"
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@.str.3 = [37 x i8] c"unexpected error: mq_close 2-1: fork\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = alloca %struct.sigaction
store i32 0, i32* %1
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%9 = call i32 (...) @getpid()
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %67
17: ; preds = %0
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%19 = call i32 @pipe(i32* %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %67
24: ; preds = %17
%25 = load i32, i32* @SIG_IGN
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%29 = call i32 @sigemptyset(i32* %28)
%30 = load i32, i32* @SIGCHLD
%31 = call i32 @sigaction(i32 %30, %struct.sigaction* %7, i32* null)
%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 = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %67
37: ; preds = %24
%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]* %5, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, 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]* %6, i64 0, i64 0
%49 = load i32, i32* %48
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %5, 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]* %5, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, 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]* %5, i64 0, i64 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %6, 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, %22, %15
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@.str = [17 x i8] c"/mq_close_2-1_%d\00"
@.str.1 = [41 x i8] c"unexpected error: mq_close 2-1: pipe (1)\00"
@.str.2 = [41 x i8] c"unexpected error: mq_close 2-1: pipe (2)\00"
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@.str.3 = [37 x i8] c"unexpected error: mq_close 2-1: 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 = tail call i32 (...) @getpid()
%10 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %53
16: ; preds = %0
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %53
22: ; preds = %16
%23 = load i32, i32* @SIG_IGN
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%27 = call i32 @sigemptyset(i32* %26)
%28 = load i32, i32* @SIGCHLD
%29 = call i32 @sigaction(i32 %28, %struct.sigaction* %4, i32* null)
%30 = call i32 (...) @fork()
switch i32 %30, label %43 [
i32 -1, label %31
i32 0, label %33
]
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %53
33: ; preds = %22
%34 = load i32, i32* %11
%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* %17
%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 = %22
%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* %17
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %11
%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, %20, %14
%54 = phi i32 [ 2, %14 ], [ 2, %20 ], [ 2, %31 ], [ %42, %33 ], [ %52, %43 ]
ret i32 %54
}