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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ _exit(scalar_t__);
scalar_t__ errno;
int fork();
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int fork2(void) {
  pid_t pid;
  int status;
  if (!(pid = fork())) {
    switch (fork()) {
    case 0:
      return 0;
    case -1:
      _exit(errno);
    default:
      _exit(0);
    }
  }
  if (pid < 0 || waitpid(pid, &status, 0) < 0)
    return -1;
  if (WIFEXITED(status))
    if (WEXITSTATUS(status) == 0)
      return 1;
    else
      errno = WEXITSTATUS(status);
  else
    errno = EINTR;
  return -1;
}


x86 assembly (clang, -O0):
<fork2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
cltq
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11bd <fork2+0x6d>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11a5 <fork2+0x55>
jmp    1189 <fork2+0x39>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    11b4 <fork2+0x64>
jmp    1199 <fork2+0x49>
movl   $0x0,-0x4(%rbp)
jmp    1252 <fork2+0x102>
mov    0x2e34(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x10(%rbp)
jl     11e1 <fork2+0x91>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    11ed <fork2+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <fork2+0x102>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1237 <fork2+0xe7>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    121d <fork2+0xcd>
movl   $0x1,-0x4(%rbp)
jmp    1252 <fork2+0x102>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %rax,%rcx
mov    0x2db1(%rip),%rax
mov    %rcx,(%rax)
jmp    124b <fork2+0xfb>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rcx
mov    0x2d98(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork2>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11a2 <fork2+0x52>
mov    $0xffffffff,%ebx
js     11de <fork2+0x8e>
movslq %eax,%rdi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
js     11de <fork2+0x8e>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11c3 <fork2+0x73>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %rax,%rax
je     11d9 <fork2+0x89>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
jmp    11cd <fork2+0x7d>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11de <fork2+0x8e>
cmp    $0xffffffff,%ebx
jne    11e6 <fork2+0x96>
mov    0x2e25(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
mov    %rax,(%rcx)
jmp    11de <fork2+0x8e>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @fork2() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = call i32 (...) @fork()
%5 = sext i32 %4 to i64
store i64 %5, i64* %2
%6 = icmp ne i64 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %0
%8 = call i32 (...) @fork()
switch i32 %8, label %13 [
i32 0, label %9
i32 -1, label %10
]
9: ; preds = %7
store i32 0, i32* %1
br label %39
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = call i32 @_exit(i64 %11)
unreachable
13: ; preds = %7
%14 = call i32 @_exit(i64 0)
unreachable
15: ; preds = %0
%16 = load i64, i64* %2
%17 = icmp slt i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i64, i64* %2
%20 = call i64 @waitpid(i64 %19, i32* %3, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18, %15
store i32 -1, i32* %1
br label %39
23: ; preds = %18
%24 = load i32, i32* %3
%25 = call i64 @WIFEXITED(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* %3
%29 = call i64 @WEXITSTATUS(i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 1, i32* %1
br label %39
32: ; preds = %27
%33 = load i32, i32* %3
%34 = call i64 @WEXITSTATUS(i32 %33)
store i64 %34, i64* @errno
br label %35
35: ; preds = %32
br label %38
36: ; preds = %23
%37 = load i64, i64* @EINTR
store i64 %37, i64* @errno
br label %38
38: ; preds = %36, %35
store i32 -1, i32* %1
br label %39
39: ; preds = %38, %31, %22, %9
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @fork2() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = sext i32 %3 to i64
%5 = icmp eq i32 %3, 0
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = tail call i32 (...) @fork()
switch i32 %7, label %11 [
i32 0, label %33
i32 -1, label %8
]
8: ; preds = %6
%9 = load i64, i64* @errno
%10 = tail call i32 @_exit(i64 %9)
unreachable
11: ; preds = %6
%12 = tail call i32 @_exit(i64 0)
unreachable
13: ; preds = %0
%14 = icmp slt i32 %3, 0
br i1 %14, label %33, label %15
15: ; preds = %13
%16 = call i64 @waitpid(i64 %4, i32* %1, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %33, label %18
18: ; preds = %15
%19 = load i32, i32* %1
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %18
%23 = load i32, i32* %1
%24 = call i64 @WEXITSTATUS(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %22
%27 = load i32, i32* %1
%28 = call i64 @WEXITSTATUS(i32 %27)
br label %31
29: ; preds = %18
%30 = load i64, i64* @EINTR
br label %31
31: ; preds = %29, %26
%32 = phi i64 [ %30, %29 ], [ %28, %26 ]
store i64 %32, i64* @errno
br label %33
33: ; preds = %22, %13, %15, %6, %31
%34 = phi i32 [ -1, %31 ], [ %7, %6 ], [ -1, %15 ], [ -1, %13 ], [ 1, %22 ]
ret i32 %34
}