#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*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ stderr;
void yta_daemonize() {
  pid_t pid = fork();
  switch (pid) {
  case 0: {
    break;
  }
  case -1: {
    fprintf(stderr, "failed daemonizing, exiting");
    exit(1);
  }
  default: {
    exit(0);
  }
  }
  if (setsid() == -1) {
    perror("failed setsid in daemon, exiting daemon");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<yta_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
sub    $0xffffffff,%eax
je     118b <yta_daemonize+0x3b>
jmp    1176 <yta_daemonize+0x26>
mov    -0x8(%rbp),%eax
test   %eax,%eax
jne    11aa <yta_daemonize+0x5a>
jmp    1186 <yta_daemonize+0x36>
jmp    11b1 <yta_daemonize+0x61>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1030 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11d7 <yta_daemonize+0x87>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<yta_daemonize>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jne    116a <yta_daemonize+0x1a>
xor    %eax,%eax
call   1030 <setsid@plt>
cmp    $0xffffffff,%eax
je     118e <yta_daemonize+0x3e>
pop    %rax
ret
cmp    $0xffffffff,%eax
jne    11a4 <yta_daemonize+0x54>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"failed daemonizing, exiting\00"
@.str.1 = [40 x i8] c"failed setsid in daemon, exiting daemon\00"
define void @yta_daemonize() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
switch i32 %3, label %9 [
i32 0, label %4
i32 -1, label %5
]
4: ; preds = %0
br label %11
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %4
%12 = call i32 (...) @setsid()
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %11
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"failed daemonizing, exiting\00"
@.str.1 = [40 x i8] c"failed setsid in daemon, exiting daemon\00"
define void @yta_daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %6 [
i32 0, label %8
i32 -1, label %2
]
2: ; preds = %0
%3 = load i32, i32* @stderr
%4 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = tail call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
ret void
}