#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 */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t f = fork();
  if (f > 0) {
    exit(0);
  } else if (f == 0) {
    pid_t m = fork();
    if (m > 0) {
    } else {
      sleep(1000);
      exit(0);
    }
  } else {
    printf("fork failed\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    116c <main+0x2c>
xor    %edi,%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11a8 <main+0x68>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1192 <main+0x52>
jmp    11a3 <main+0x63>
mov    $0x3e8,%edi
call   1050 <sleep@plt>
xor    %edi,%edi
call   1040 <exit@plt>
jmp    11c0 <main+0x80>
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <main+0x85>
jmp    11c5 <main+0x85>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jg     1174 <main+0x34>
je     1162 <main+0x22>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1170 <main+0x30>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    117b <main+0x3b>
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1040 <exit@plt>
mov    $0x3e8,%edi
call   1050 <sleep@plt>
xor    %edi,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"fork failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @exit(i32 0)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = call i64 (...) @fork()
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %20
17: ; preds = %12
%18 = call i32 @sleep(i32 1000)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %16
br label %23
21: ; preds = %9
%22 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %24
23: ; preds = %20
br label %24
24: ; preds = %21, %23
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@str = [12 x i8] c"fork failed\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp sgt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %13
7: ; preds = %5
%8 = tail call i64 (...) @fork()
%9 = icmp sgt i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = tail call i32 @sleep(i32 1000)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %7, %13
%16 = phi i32 [ -1, %13 ], [ 0, %7 ]
ret i32 %16
}