#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int close(int /*<<< orphan*/);
int /*<<< orphan*/ close_log();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ log_error(char *, char *, ...);
int setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_child(pid_t child_id, char *working_dir) {
  umask(0);
  if (setsid() == -1) {
    log_error("Unable to create new session: %s\n", strerror(errno));
    goto error_out;
  }
  if (chdir(working_dir) == -1) {
    log_error("Unable to change to directory %s: %s\n", working_dir,
              strerror(errno));
    goto error_out;
  }
  if (close(STDIN_FILENO) == -1) {
    log_error("Unable to close stdin: %s\n", strerror(errno));
    goto error_out;
  }
  if (close(STDOUT_FILENO) == -1) {
    log_error("Unable to close stdout: %s\n", strerror(errno));
    goto error_out;
  }
  if (close(STDERR_FILENO) == -1) {
    log_error("Unable to close stderr: %s\n", strerror(errno));
    goto error_out;
  }
  return;
error_out:
  close_log();
  free(working_dir);
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<init_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11da <init_child+0x4a>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    12d2 <init_child+0x142>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    121b <init_child+0x8b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    12d2 <init_child+0x142>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    1256 <init_child+0xc6>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    12d2 <init_child+0x142>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    1291 <init_child+0x101>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    12d2 <init_child+0x142>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    12cc <init_child+0x13c>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    12d2 <init_child+0x142>
add    $0x20,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1060 <close_log@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<init_child>:
push   %rbx
mov    %rsi,%rbx
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
cmp    $0xffffffff,%eax
je     11ef <init_child+0x5f>
mov    %rbx,%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     1209 <init_child+0x79>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     122c <init_child+0x9c>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     1246 <init_child+0xb6>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     1260 <init_child+0xd0>
pop    %rbx
ret
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xdf9(%rip),%rdi
jmp    1278 <init_child+0xe8>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe04(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <log_error@plt>
jmp    127f <init_child+0xef>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xe04(%rip),%rdi
jmp    1278 <init_child+0xe8>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xe05(%rip),%rdi
jmp    1278 <init_child+0xe8>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xe07(%rip),%rdi
xor    %eax,%eax
call   1040 <log_error@plt>
xor    %eax,%eax
call   1060 <close_log@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"Unable to create new session: %s\0A\00"
@errno = global i32 0
@.str.1 = [38 x i8] c"Unable to change to directory %s: %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [27 x i8] c"Unable to close stdin: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [28 x i8] c"Unable to close stdout: %s\0A\00"
@STDERR_FILENO = global i32 0
@.str.4 = [28 x i8] c"Unable to close stderr: %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @init_child(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = call i32 @umask(i32 0)
%6 = call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
%13 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %54
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @chdir(i8* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %21)
br label %54
23: ; preds = %14
%24 = load i32, i32* @STDIN_FILENO
%25 = call i32 @close(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %54
33: ; preds = %23
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @close(i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = sext i32 %39 to i64
%41 = inttoptr i64 %40 to i8*
%42 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %54
43: ; preds = %33
%44 = load i32, i32* @STDERR_FILENO
%45 = call i32 @close(i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = sext i32 %49 to i64
%51 = inttoptr i64 %50 to i8*
%52 = call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %51)
br label %54
53: ; preds = %43
ret void
54: ; preds = %47, %37, %27, %18, %8
%55 = call i32 (...) @close_log()
%56 = load i8*, i8** %4
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
}

LLVM IR (-O2):
@.str = [34 x i8] c"Unable to create new session: %s\0A\00"
@errno = global i32 0
@.str.1 = [38 x i8] c"Unable to change to directory %s: %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [27 x i8] c"Unable to close stdin: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [28 x i8] c"Unable to close stdout: %s\0A\00"
@STDERR_FILENO = global i32 0
@.str.4 = [28 x i8] c"Unable to close stderr: %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @init_child(i32 %0, i8* %1) {
%3 = tail call i32 @umask(i32 0)
%4 = tail call i32 (...) @setsid()
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %50
12: ; preds = %2
%13 = tail call i32 @chdir(i8* %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %17)
br label %50
19: ; preds = %12
%20 = load i32, i32* @STDIN_FILENO
%21 = tail call i32 @close(i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* @errno
%25 = tail call i32 @strerror(i32 %24)
%26 = sext i32 %25 to i64
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %50
29: ; preds = %19
%30 = load i32, i32* @STDOUT_FILENO
%31 = tail call i32 @close(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = tail call i32 @strerror(i32 %34)
%36 = sext i32 %35 to i64
%37 = inttoptr i64 %36 to i8*
%38 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %50
39: ; preds = %29
%40 = load i32, i32* @STDERR_FILENO
%41 = tail call i32 @close(i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i32, i32* @errno
%45 = tail call i32 @strerror(i32 %44)
%46 = sext i32 %45 to i64
%47 = inttoptr i64 %46 to i8*
%48 = tail call i32 (i8*, i8*, ...) @log_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %47)
br label %50
49: ; preds = %39
ret void
50: ; preds = %43, %33, %23, %15, %6
%51 = tail call i32 (...) @close_log()
%52 = tail call i32 @free(i8* %1)
%53 = load i32, i32* @EXIT_FAILURE
%54 = tail call i32 @exit(i32 %53)
unreachable
}