#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*/ FatalError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int setpgid(int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void BecomeOrphan() {
  pid_t child_id;
  chdir("/");
  child_id = fork();
  switch (child_id) {
  case 0:
    break;
  case -1:
    FatalError("daemon fork failed, %s\n", strerror(errno));
    break;
  default: {
    int stat;
    stat = setpgid(child_id, child_id);
    if (stat != 0)
      FatalError("setting process group for daemon failed: %s\n",
                 strerror(errno));
  }
    exit(0);
  }
}


x86 assembly (clang, -O0):
<BecomeOrphan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <chdir@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11a7 <BecomeOrphan+0x47>
jmp    1192 <BecomeOrphan+0x32>
mov    -0xc(%rbp),%eax
test   %eax,%eax
jne    11c8 <BecomeOrphan+0x68>
jmp    11a2 <BecomeOrphan+0x42>
jmp    1203 <BecomeOrphan+0xa3>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe44(%rip),%rdi
call   1040 <FatalError@plt>
jmp    1203 <BecomeOrphan+0xa3>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1050 <setpgid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11fc <BecomeOrphan+0x9c>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe23(%rip),%rdi
call   1040 <FatalError@plt>
xor    %edi,%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BecomeOrphan>:
push   %rax
lea    0xe98(%rip),%rdi
call   1030 <chdir@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     119a <BecomeOrphan+0x3a>
cmp    $0xffffffff,%eax
jne    119c <BecomeOrphan+0x3c>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe70(%rip),%rdi
mov    %eax,%esi
pop    %rax
jmp    1040 <FatalError@plt>
pop    %rax
ret
mov    %eax,%edi
mov    %eax,%esi
call   1050 <setpgid@plt>
test   %eax,%eax
je     11c5 <BecomeOrphan+0x65>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
call   1040 <FatalError@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [24 x i8] c"daemon fork failed, %s\0A\00"
@errno = global i32 0
@.str.2 = [45 x i8] c"setting process group for daemon failed: %s\0A\00"
define void @BecomeOrphan() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @fork()
store i32 %4, i32* %1
%5 = load i32, i32* %1
switch i32 %5, label %11 [
i32 0, label %6
i32 -1, label %7
]
6: ; preds = %0
br label %23
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = call i32 @strerror(i32 %8)
%10 = call i32 @FatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %23
11: ; preds = %0
%12 = load i32, i32* %1
%13 = load i32, i32* %1
%14 = call i32 @setpgid(i32 %12, i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @FatalError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %11
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %7, %6
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [24 x i8] c"daemon fork failed, %s\0A\00"
@errno = global i32 0
@.str.2 = [45 x i8] c"setting process group for daemon failed: %s\0A\00"
define void @BecomeOrphan() {
%1 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %7 [
i32 0, label %16
i32 -1, label %3
]
3: ; preds = %0
%4 = load i32, i32* @errno
%5 = tail call i32 @strerror(i32 %4)
%6 = tail call i32 @FatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %16
7: ; preds = %0
%8 = tail call i32 @setpgid(i32 %2, i32 %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @FatalError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10, %7
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %3, %0
ret void
}