#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*/ assert(int);
int execv(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ log1(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send_int(int, int);
int setsid();
int /*<<< orphan*/ shim_log(char *, ...);
pid_t spawn(int fd, char *params[]) {
  assert(params != 0);
  assert(*params != 0);
  log1("Spawning!");
  pid_t child = fork();
  if (child)
    return 0;
  log1("Forked!");
  if (setsid() == -1) {
    perror("setsid");
    exit(1);
  }
  child = fork();
  if (child) {
    send_int(fd, child);
    shim_log("Sent pid %d", child);
    exit(0);
    return -1;
  }
  log1("Reforked!");
  shim_log("Running %s", *params);
  if (execv(*params, params) == -1) {
    perror("execv");
    exit(1);
  }
  return -1;
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xe33(%rip),%rdi
call   1070 <log1@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f2 <spawn+0x62>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <spawn+0x122>
lea    0xe11(%rip),%rdi
call   1070 <log1@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1224 <spawn+0x94>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     125b <spawn+0xcb>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <send_int@plt>
mov    -0x14(%rbp),%esi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <shim_log@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdc3(%rip),%rdi
call   1070 <log1@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <shim_log@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execv@plt>
cmp    $0xffffffff,%eax
jne    12ab <spawn+0x11b>
lea    0xd9e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%rbx)
setne  %dil
call   1050 <assert@plt>
lea    0xe42(%rip),%rdi
call   1070 <log1@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1225 <spawn+0x95>
lea    0xe33(%rip),%rdi
call   1070 <log1@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
je     122c <spawn+0x9c>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1235 <spawn+0xa5>
lea    0xe2b(%rip),%rdi
call   1070 <log1@plt>
mov    (%rbx),%rsi
lea    0xe26(%rip),%rdi
xor    %eax,%eax
call   1060 <shim_log@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10a0 <execv@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1258 <spawn+0xc8>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddf(%rip),%rdi
jmp    125f <spawn+0xcf>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %eax,%esi
call   1030 <send_int@plt>
lea    0xdd1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <shim_log@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xddb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"Spawning!\00"
@.str.1 = [8 x i8] c"Forked!\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [12 x i8] c"Sent pid %d\00"
@.str.4 = [10 x i8] c"Reforked!\00"
@.str.5 = [11 x i8] c"Running %s\00"
@.str.6 = [6 x i8] c"execv\00"
define i32 @spawn(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %5
%8 = icmp ne i8** %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8**, i8*** %5
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = call i32 @log1(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%17 = call i32 (...) @fork()
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 0, i32* %3
br label %53
21: ; preds = %2
%22 = call i32 @log1(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (...) @setsid()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = call i32 (...) @fork()
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = load i32, i32* %4
%34 = load i32, i32* %6
%35 = call i32 @send_int(i32 %33, i32 %34)
%36 = load i32, i32* %6
%37 = call i32 (i8*, ...) @shim_log(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %28
%40 = call i32 @log1(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) @shim_log(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %42)
%44 = load i8**, i8*** %5
%45 = load i8*, i8** %44
%46 = load i8**, i8*** %5
%47 = call i32 @execv(i8* %45, i8** %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %39
%50 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %39
store i32 -1, i32* %3
br label %53
53: ; preds = %52, %20
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [10 x i8] c"Spawning!\00"
@.str.1 = [8 x i8] c"Forked!\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [12 x i8] c"Sent pid %d\00"
@.str.4 = [10 x i8] c"Reforked!\00"
@.str.5 = [11 x i8] c"Running %s\00"
@.str.6 = [6 x i8] c"execv\00"
define i32 @spawn(i32 %0, i8** %1) {
%3 = icmp ne i8** %1, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i8*, i8** %1
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 @log1(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %2
%14 = tail call i32 @log1(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 (...) @setsid()
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = tail call i32 (...) @fork()
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = tail call i32 @send_int(i32 %0, i32 %21)
%25 = tail call i32 (i8*, ...) @shim_log(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %20
%28 = tail call i32 @log1(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%29 = load i8*, i8** %1
%30 = tail call i32 (i8*, ...) @shim_log(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %1
%32 = tail call i32 @execv(i8* %31, i8** %1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %27, %2
%38 = phi i32 [ 0, %2 ], [ -1, %27 ]
ret i32 %38
}