#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void fork_background() {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0)
    err(EXIT_FAILURE, "Failed to fork daemon to background");
  else if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  sid = setsid();
  if (sid < 0)
    exit(EXIT_FAILURE);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  freopen("mcmdd.log", "a", stdout);
  freopen("mcmdd.err", "a", stderr);
  fchmod(0, S_IRUSR | S_IWUSR);
  fchmod(1, S_IRUSR | S_IWUSR);
  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
  FILE *pidfile = fopen("mcmdd.pid", "w");
  if (!pidfile)
    err(1, "Failed to write pid file");
  fprintf(pidfile, "%d\n", sid);
  fclose(pidfile);
  chmod("mcmdd.pid", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}


x86 assembly (clang, -O0):
<fork_background>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1208 <fork_background+0x38>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   10a0 <err@plt>
jmp    1226 <fork_background+0x56>
cmpq   $0x0,-0x8(%rbp)
jle    1221 <fork_background+0x51>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    1226 <fork_background+0x56>
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1251 <fork_background+0x81>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
lea    0xd99(%rip),%rdi
lea    0xd9c(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
lea    0xd89(%rip),%rdi
lea    0xd80(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    0x2d1d(%rip),%rax
or     (%rax),%esi
xor    %edi,%edi
call   10b0 <fchmod@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%esi
mov    0x2d04(%rip),%rax
or     (%rax),%esi
mov    $0x1,%edi
call   10b0 <fchmod@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
lea    0xd27(%rip),%rdi
lea    0xd2a(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    133f <fork_background+0x16f>
mov    $0x1,%edi
lea    0xd0c(%rip),%rsi
call   10a0 <err@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd11(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2c45(%rip),%rax
mov    (%rax),%esi
mov    0x2c74(%rip),%rax
or     (%rax),%esi
mov    0x2c4b(%rip),%rax
or     (%rax),%esi
mov    0x2c3a(%rip),%rax
or     (%rax),%esi
lea    0xcb3(%rip),%rdi
call   10c0 <chmod@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_background>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     11f7 <fork_background+0x27>
je     120c <fork_background+0x3c>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   10a0 <err@plt>
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1345 <fork_background+0x175>
mov    %rax,%r14
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d49(%rip),%r13
mov    0x0(%r13),%edx
lea    0xdc2(%rip),%rdi
lea    0xdc5(%rip),%rbx
mov    %rbx,%rsi
call   1050 <freopen@plt>
mov    0x2d58(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xdae(%rip),%rdi
mov    %rbx,%rsi
call   1050 <freopen@plt>
mov    0x2d17(%rip),%r15
mov    0x2d48(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
xor    %edi,%edi
call   10b0 <fchmod@plt>
mov    (%r12),%esi
or     (%r15),%esi
mov    $0x1,%edi
call   10b0 <fchmod@plt>
mov    0x0(%r13),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    0x0(%rbp),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0xd67(%rip),%rdi
lea    0xd6a(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12f8 <fork_background+0x128>
lea    0xd58(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
lea    0xd60(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%r12),%esi
or     (%r15),%esi
mov    0x2ca0(%rip),%rax
or     (%rax),%esi
mov    0x2c8f(%rip),%rax
or     (%rax),%esi
lea    0xd08(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <chmod@plt>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [36 x i8] c"Failed to fork daemon to background\00"
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [10 x i8] c"mcmdd.log\00"
@.str.2 = [2 x i8] c"a\00"
@stdout = global i32 0
@.str.3 = [10 x i8] c"mcmdd.err\00"
@stderr = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [10 x i8] c"mcmdd.pid\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [25 x i8] c"Failed to write pid file\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define void @fork_background() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32*
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @EXIT_FAILURE
%9 = call i32 @err(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %17
10: ; preds = %0
%11 = load i64, i64* %1
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EXIT_SUCCESS
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %10
br label %17
17: ; preds = %16, %7
%18 = call i32 @umask(i32 0)
%19 = call i64 (...) @setsid()
store i64 %19, i64* %2
%20 = load i64, i64* %2
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %17
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* @STDERR_FILENO
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @stdout
%33 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @stderr
%35 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = call i32 @fchmod(i32 0, i32 %38)
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %40, %41
%43 = call i32 @fchmod(i32 1, i32 %42)
%44 = load i32, i32* @stdout
%45 = call i32 @setbuf(i32 %44, i32* null)
%46 = load i32, i32* @stderr
%47 = call i32 @setbuf(i32 %46, i32* null)
%48 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %48, i32** %3
%49 = load i32*, i32** %3
%50 = icmp ne i32* %49, null
br i1 %50, label %53, label %51
51: ; preds = %25
%52 = call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %51, %25
%54 = load i32*, i32** %3
%55 = load i64, i64* %2
%56 = call i32 @fprintf(i32* %54, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i64 %55)
%57 = load i32*, i32** %3
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* @S_IRUSR
%60 = load i32, i32* @S_IWUSR
%61 = or i32 %59, %60
%62 = load i32, i32* @S_IRGRP
%63 = or i32 %61, %62
%64 = load i32, i32* @S_IROTH
%65 = or i32 %63, %64
%66 = call i32 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %65)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [36 x i8] c"Failed to fork daemon to background\00"
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [10 x i8] c"mcmdd.log\00"
@.str.2 = [2 x i8] c"a\00"
@stdout = global i32 0
@.str.3 = [10 x i8] c"mcmdd.err\00"
@stderr = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [10 x i8] c"mcmdd.pid\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [25 x i8] c"Failed to write pid file\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define void @fork_background() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @err(i32 %4, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %11
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6, %3
%12 = tail call i32 @umask(i32 0)
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %11
%19 = load i32, i32* @STDIN_FILENO
%20 = tail call i32 @close(i32 %19)
%21 = load i32, i32* @STDOUT_FILENO
%22 = tail call i32 @close(i32 %21)
%23 = load i32, i32* @STDERR_FILENO
%24 = tail call i32 @close(i32 %23)
%25 = load i32, i32* @stdout
%26 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @stderr
%28 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %30, %29
%32 = tail call i32 @fchmod(i32 0, i32 %31)
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %34, %33
%36 = tail call i32 @fchmod(i32 1, i32 %35)
%37 = load i32, i32* @stdout
%38 = tail call i32 @setbuf(i32 %37, i32* null)
%39 = load i32, i32* @stderr
%40 = tail call i32 @setbuf(i32 %39, i32* null)
%41 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %18
%44 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %43, %18
%46 = tail call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i64 %13)
%47 = tail call i32 @fclose(i32* %41)
%48 = load i32, i32* @S_IRUSR
%49 = load i32, i32* @S_IWUSR
%50 = or i32 %49, %48
%51 = load i32, i32* @S_IRGRP
%52 = or i32 %50, %51
%53 = load i32, i32* @S_IROTH
%54 = or i32 %52, %53
%55 = tail call i32 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %54)
ret void
}