#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*/ do_watcher();
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ writepidfile(scalar_t__);
void do_spawn() {
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Error: Fork failed: %m\n");
    exit(1);
  }
  if (pid == 0) {
    do_watcher();
    exit(0);
  } else {
    writepidfile(pid);
    fprintf(stdout, "[%d]\n", pid);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<do_spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    118f <do_spawn+0x3f>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11a8 <do_spawn+0x58>
mov    $0x0,%al
call   1030 <do_watcher@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1050 <writepidfile@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<do_spawn>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jns    117e <do_spawn+0x2e>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jne    118e <do_spawn+0x3e>
xor    %eax,%eax
call   1030 <do_watcher@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <writepidfile@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Error: Fork failed: %m\0A\00"
@stdout = global i32 0
@.str.1 = [6 x i8] c"[%d]\0A\00"
define void @do_spawn() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 (...) @do_watcher()
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %9
%16 = load i64, i64* %1
%17 = call i32 @writepidfile(i64 %16)
%18 = load i32, i32* @stdout
%19 = load i64, i64* %1
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Error: Fork failed: %m\0A\00"
@stdout = global i32 0
@.str.1 = [6 x i8] c"[%d]\0A\00"
define void @do_spawn() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = icmp eq i64 %1, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 (...) @do_watcher()
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %7
%13 = tail call i32 @writepidfile(i64 %1)
%14 = load i32, i32* @stdout
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%16 = tail call i32 @exit(i32 0)
unreachable
}