#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*/ PR_SET_PDEATHSIG;
int /*<<< orphan*/ SIGHUP;
char *curdir();
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ prctl(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int spawn_daemon(void) {
  char statd_path[4096];
  char *dir;
  pid_t childpid;
  if ((childpid = fork()) == -1) {
    perror("fork");
    return -1;
  }
  if (childpid == 0) {
    if (prctl(PR_SET_PDEATHSIG, SIGHUP) < 0) {
      perror("setting up daemon deathsig");
    }
    dir = curdir();
    snprintf(statd_path, 4096, "%s/statd", dir);
    free(dir);
    execlp(statd_path, statd_path, NULL);
    exit(0);
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<spawn_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x101c(%rbp)
cmp    $0xffffffff,%eax
jne    11b9 <spawn_daemon+0x39>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <spawn_daemon+0xd1>
cmpl   $0x0,-0x101c(%rbp)
jne    124a <spawn_daemon+0xca>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1060 <prctl@plt>
cmp    $0x0,%rax
jge    11f3 <spawn_daemon+0x73>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <curdir@plt>
mov    %rax,-0x1018(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe05(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x1018(%rbp),%rdi
call   1040 <free@plt>
lea    -0x1010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_daemon>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11a9 <spawn_daemon+0x29>
test   %eax,%eax
je     11bc <spawn_daemon+0x3c>
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    119c <spawn_daemon+0x1c>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1060 <prctl@plt>
test   %rax,%rax
jns    11e4 <spawn_daemon+0x64>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1030 <curdir@plt>
mov    %rax,%rbx
lea    0xe2b(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@PR_SET_PDEATHSIG = global i32 0
@SIGHUP = global i32 0
@.str.1 = [27 x i8] c"setting up daemon deathsig\00"
@.str.2 = [9 x i8] c"%s/statd\00"
define i32 @spawn_daemon() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = call i32 (...) @fork()
store i32 %5, i32* %4
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %31
9: ; preds = %0
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = load i32, i32* @PR_SET_PDEATHSIG
%14 = load i32, i32* @SIGHUP
%15 = call i64 @prctl(i32 %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i8* (...) @curdir()
store i8* %20, i8** %3
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%22 = load i8*, i8** %3
%23 = call i32 @snprintf(i8* %21, i32 4096, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %3
%25 = call i32 @free(i8* %24)
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%28 = call i32 @execlp(i8* %26, i8* %27, i32* null)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %9
store i32 0, i32* %1
br label %31
31: ; preds = %30, %7
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@PR_SET_PDEATHSIG = global i32 0
@SIGHUP = global i32 0
@.str.1 = [27 x i8] c"setting up daemon deathsig\00"
@.str.2 = [9 x i8] c"%s/statd\00"
define i32 @spawn_daemon() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @fork()
switch i32 %3, label %19 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
6: ; preds = %0
%7 = load i32, i32* @PR_SET_PDEATHSIG
%8 = load i32, i32* @SIGHUP
%9 = tail call i64 @prctl(i32 %7, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %6
%14 = tail call i8* (...) @curdir()
%15 = call i32 @snprintf(i8* %2, i32 4096, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = call i32 @free(i8* %14)
%17 = call i32 @execlp(i8* %2, i8* %2, i32* null)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %0, %4
%20 = phi i32 [ -1, %4 ], [ 0, %0 ]
ret i32 %20
}