#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__ fork();
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  pid_t pid = fork();
  if (pid == 0) {
    setsid();
    pid_t pid = fork();
    if (pid == 0) {
      setsid();
      sleep(1000);
      return 0;
    }
    if (pid > 0) {
      sleep(10);
      return 0;
    }
    printf("error double fork %d\n", pid);
    return 0;
  }
  if (pid > 0) {
    sleep(20);
    return 0;
  }
  printf("error fork %d\n", pid);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <main+0xa3>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a6 <main+0x66>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    $0x3e8,%edi
call   1050 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <main+0xd4>
cmpq   $0x0,-0x20(%rbp)
jle    11c7 <main+0x87>
mov    $0xa,%edi
call   1050 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <main+0xd4>
mov    -0x20(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <main+0xd4>
cmpq   $0x0,-0x18(%rbp)
jle    1204 <main+0xc4>
mov    $0x14,%edi
call   1050 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <main+0xd4>
mov    -0x18(%rbp),%rsi
lea    0xe07(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     115b <main+0x1b>
jle    117c <main+0x3c>
mov    $0x14,%edi
call   1050 <sleep@plt>
jmp    11a7 <main+0x67>
xor    %eax,%eax
call   1040 <setsid@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     1185 <main+0x45>
jle    1198 <main+0x58>
mov    $0xa,%edi
call   1050 <sleep@plt>
jmp    11a7 <main+0x67>
lea    0xe93(%rip),%rdi
jmp    119f <main+0x5f>
xor    %eax,%eax
call   1040 <setsid@plt>
mov    $0x3e8,%edi
call   1050 <sleep@plt>
jmp    11a7 <main+0x67>
lea    0xe61(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

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

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