#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 */

/* Variables and functions */
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fork();
int setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int spawn_child(int filter, int *status) {
  int pid;
  pid = fork();
  switch (pid) {
  case -1:
    return (-1);
  case 0:
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    if (setsid() == -1) {
      syslog(LOG_NOTICE, "setsid(): %s", strerror(errno));
      return (-1);
    };
    break;
  default:
    return (pid);
  };
  *status = filter;
  return (0);
}


x86 assembly (clang, -O0):
<spawn_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     118d <spawn_child+0x3d>
jmp    117d <spawn_child+0x2d>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     1199 <spawn_child+0x49>
jmp    120f <spawn_child+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <spawn_child+0xda>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    120a <spawn_child+0xba>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe07(%rip),%rsi
call   1050 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <spawn_child+0xda>
jmp    121a <spawn_child+0xca>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <spawn_child+0xda>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_child>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11cf <spawn_child+0x7f>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
je     11a3 <spawn_child+0x53>
mov    %ebp,(%r14)
jmp    11cf <spawn_child+0x7f>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ebx
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe3f(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <syslog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@LOG_NOTICE = global i32 0
@.str = [13 x i8] c"setsid(): %s\00"
@errno = global i32 0
define i32 @spawn_child(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
switch i32 %8, label %25 [
i32 -1, label %9
i32 0, label %10
]
9: ; preds = %2
store i32 -1, i32* %3
br label %30
10: ; preds = %2
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @close(i32 %11)
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i32 @close(i32 %13)
%15 = load i32, i32* @STDERR_FILENO
%16 = call i32 @close(i32 %15)
%17 = call i32 (...) @setsid()
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %10
%20 = load i32, i32* @LOG_NOTICE
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 -1, i32* %3
br label %30
24: ; preds = %10
br label %27
25: ; preds = %2
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %30
27: ; preds = %24
%28 = load i32, i32* %4
%29 = load i32*, i32** %5
store i32 %28, i32* %29
store i32 0, i32* %3
br label %30
30: ; preds = %27, %25, %19, %9
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@LOG_NOTICE = global i32 0
@.str = [13 x i8] c"setsid(): %s\00"
@errno = global i32 0
define i32 @spawn_child(i32 %0, i32* nocapture writeonly %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = load i32, i32* @STDIN_FILENO
%7 = tail call i32 @close(i32 %6)
%8 = load i32, i32* @STDOUT_FILENO
%9 = tail call i32 @close(i32 %8)
%10 = load i32, i32* @STDERR_FILENO
%11 = tail call i32 @close(i32 %10)
%12 = tail call i32 (...) @setsid()
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %5
%15 = load i32, i32* @LOG_NOTICE
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @syslog(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %20
19: ; preds = %5
store i32 %0, i32* %1
br label %20
20: ; preds = %2, %19, %14
%21 = phi i32 [ -1, %14 ], [ 0, %19 ], [ %3, %2 ]
ret i32 %21
}