#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;

/* Variables and functions */
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
pid_t parent_pause_fork() {
  int filedes[2];
  if (!pipe(filedes)) {
    int pid = fork();
    if (pid == -1) {
      close(filedes[0]);
      close(filedes[1]);
    } else if (pid > 0) {
      char success = 1;
      close(filedes[1]);
      read(filedes[0], &success, 1);
      close(filedes[0]);
      if (success) {
        return (pid);
      }
      waitpid(pid, 0, 0);
    } else {
      close(filedes[0]);
      fcntl(filedes[1], F_SETFD, FD_CLOEXEC);
      return (-filedes[1]);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<parent_pause_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%eax
jne    1231 <parent_pause_fork+0xd1>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a3 <parent_pause_fork+0x43>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    122c <parent_pause_fork+0xcc>
cmpl   $0x0,-0x10(%rbp)
jle    11f8 <parent_pause_fork+0x98>
movb   $0x1,-0x11(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmpb   $0x0,-0x11(%rbp)
je     11e7 <parent_pause_fork+0x87>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <parent_pause_fork+0xd8>
mov    -0x10(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <waitpid@plt>
jmp    1227 <parent_pause_fork+0xc7>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
xor    %eax,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <parent_pause_fork+0xd8>
jmp    122c <parent_pause_fork+0xcc>
jmp    1231 <parent_pause_fork+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_pause_fork>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     117f <parent_pause_fork+0x1f>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11c8 <parent_pause_fork+0x68>
mov    %eax,%ebp
test   %eax,%eax
jle    11dc <parent_pause_fork+0x7c>
movb   $0x1,0xf(%rsp)
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
cmpb   $0x0,0xf(%rsp)
je     120b <parent_pause_fork+0xab>
mov    %ebp,%ebx
jmp    1176 <parent_pause_fork+0x16>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
jmp    1176 <parent_pause_fork+0x16>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
xor    %ebx,%ebx
sub    0x14(%rsp),%ebx
jmp    1176 <parent_pause_fork+0x16>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1176 <parent_pause_fork+0x16>

LLVM IR (-O0):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @parent_pause_fork() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i8
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp ne i32 %6, 0
br i1 %7, label %53, label %8
8: ; preds = %0
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
br label %52
19: ; preds = %8
%20 = load i32, i32* %3
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %39
22: ; preds = %19
store i8 1, i8* %4
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @read(i32 %27, i8* %4, i32 1)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i8, i8* %4
%33 = icmp ne i8 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = load i32, i32* %3
store i32 %35, i32* %1
br label %54
36: ; preds = %22
%37 = load i32, i32* %3
%38 = call i32 @waitpid(i32 %37, i32 0, i32 0)
br label %51
39: ; preds = %19
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @F_SETFD
%46 = load i32, i32* @FD_CLOEXEC
%47 = call i32 @fcntl(i32 %44, i32 %45, i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%49 = load i32, i32* %48
%50 = sub nsw i32 0, %49
store i32 %50, i32* %1
br label %54
51: ; preds = %36
br label %52
52: ; preds = %51, %12
br label %53
53: ; preds = %52, %0
store i32 0, i32* %1
br label %54
54: ; preds = %53, %39, %34
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @parent_pause_fork() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %41
7: ; preds = %0
%8 = call i32 (...) @fork()
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = load i32, i32* %4
%12 = call i32 @close(i32 %11)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
br label %41
16: ; preds = %7
%17 = icmp sgt i32 %8, 0
br i1 %17, label %18, label %30
18: ; preds = %16
store i8 1, i8* %2
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %4
%23 = call i32 @read(i32 %22, i8* %2, i32 1)
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = load i8, i8* %2
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %18
%29 = call i32 @waitpid(i32 %8, i32 0, i32 0)
br label %41
30: ; preds = %16
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i32, i32* @F_SETFD
%36 = load i32, i32* @FD_CLOEXEC
%37 = call i32 @fcntl(i32 %34, i32 %35, i32 %36)
%38 = load i32, i32* %33
%39 = sub nsw i32 0, %38
br label %41
40: ; preds = %18
br label %41
41: ; preds = %10, %28, %0, %30, %40
%42 = phi i32 [ %39, %30 ], [ %8, %40 ], [ 0, %0 ], [ 0, %28 ], [ 0, %10 ]
ret i32 %42
}