#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 */
struct passwd {
  char *pw_dir;
};
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 /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ free(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(int);
scalar_t__ setsid();
char *strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid, sid;
  char *home_dir, *log_path;
  struct passwd *user_info;
  FILE *log_file;
  int log_path_len;
  pid = fork();
  if (pid < 0)
    return EXIT_FAILURE;
  if (pid > 0)
    return EXIT_SUCCESS;
  umask(0);
  sid = setsid();
  if (sid < 0)
    return EXIT_FAILURE;
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  user_info = getpwuid(getuid());
  home_dir = user_info->pw_dir;
  log_path_len = strlen(home_dir) + strlen("/daemon-test-log") + 1;
  log_path = malloc(log_path_len * sizeof(char));
  log_path[0] = '\0';
  log_path = strcat(log_path, home_dir);
  log_path = strcat(log_path, "/daemon-test-log");
  log_file = fopen(log_path, "w+");
  if (!log_file) {
    free(log_path);
    return EXIT_FAILURE;
  }
  while (1) {
    fprintf(log_file, "I am the child process!\n");
  }
  fclose(log_file);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11fd <main+0x3d>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1345 <main+0x185>
cmpq   $0x0,-0x18(%rbp)
jle    1219 <main+0x59>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1345 <main+0x185>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1247 <main+0x87>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1345 <main+0x185>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x48(%rbp)
lea    0xd5f(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10d0 <strcat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xd17(%rip),%rsi
call   10d0 <strcat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xd14(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    132b <main+0x16b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2caf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1345 <main+0x185>
jmp    1330 <main+0x170>
mov    -0x40(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1330 <main+0x170>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   10e0 <fork@plt>
mov    0x2dfd(%rip),%r15
test   %rax,%rax
js     11e1 <main+0x21>
je     11ea <main+0x2a>
mov    0x2de7(%rip),%r15
mov    (%r15),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
js     11e1 <main+0x21>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
lea    0xdb6(%rip),%rdi
call   1050 <strlen@plt>
lea    (%r14,%rax,1),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %rbx,%rsi
call   10c0 <strcat@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
movups 0xd85(%rip),%xmm0
movups %xmm0,(%r14,%rax,1)
movb   $0x0,0x10(%r14,%rax,1)
lea    0xd84(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12c7 <main+0x107>
mov    %rax,%rbx
lea    0xd70(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x18,%esi
mov    $0x1,%edx
mov    %r14,%rdi
mov    %rbx,%rcx
call   10d0 <fwrite@plt>
jmp    12b0 <main+0xf0>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11e1 <main+0x21>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [17 x i8] c"/daemon-test-log\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [25 x i8] c"I am the child process!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.passwd*
%11 = alloca i32*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i64 (...) @fork()
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %3
br label %71
18: ; preds = %2
%19 = load i64, i64* %6
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32, i32* @EXIT_SUCCESS
store i32 %22, i32* %3
br label %71
23: ; preds = %18
%24 = call i32 @umask(i32 0)
%25 = call i64 (...) @setsid()
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i32, i32* @EXIT_FAILURE
store i32 %29, i32* %3
br label %71
30: ; preds = %23
%31 = load i32, i32* @STDIN_FILENO
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @close(i32 %35)
%37 = call i32 (...) @getuid()
%38 = call %struct.passwd* @getpwuid(i32 %37)
store %struct.passwd* %38, %struct.passwd** %10
%39 = load %struct.passwd*, %struct.passwd** %10
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %39, i32 0, i32 0
%41 = load i8*, i8** %40
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%45 = add nsw i32 %43, %44
%46 = add nsw i32 %45, 1
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = mul i64 %48, 1
%50 = trunc i64 %49 to i32
%51 = call i8* @malloc(i32 %50)
store i8* %51, i8** %9
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 0
store i8 0, i8* %53
%54 = load i8*, i8** %9
%55 = load i8*, i8** %8
%56 = call i8* @strcat(i8* %54, i8* %55)
store i8* %56, i8** %9
%57 = load i8*, i8** %9
%58 = call i8* @strcat(i8* %57, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* %58, i8** %9
%59 = load i8*, i8** %9
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %60, i32** %11
%61 = load i32*, i32** %11
%62 = icmp ne i32* %61, null
br i1 %62, label %67, label %63
63: ; preds = %30
%64 = load i8*, i8** %9
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
store i32 %66, i32* %3
br label %71
67: ; preds = %30
br label %68
68: ; preds = %67, %68
%69 = load i32*, i32** %11
%70 = call i32 @fprintf(i32* %69, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %68
71: ; preds = %63, %28, %21, %16
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [17 x i8] c"/daemon-test-log\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [25 x i8] c"I am the child process!\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %36, label %5
5: ; preds = %2
%6 = icmp eq i64 %3, 0
br i1 %6, label %7, label %36
7: ; preds = %5
%8 = tail call i32 @umask(i32 0)
%9 = tail call i64 (...) @setsid()
%10 = icmp slt i64 %9, 0
br i1 %10, label %36, label %11
11: ; preds = %7
%12 = load i32, i32* @STDIN_FILENO
%13 = tail call i32 @close(i32 %12)
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @close(i32 %14)
%16 = load i32, i32* @STDERR_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = tail call i32 (...) @getuid()
%19 = tail call %struct.passwd* @getpwuid(i32 %18)
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = tail call i32 @strlen(i8* %21)
%23 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%24 = add i32 %22, 1
%25 = add i32 %24, %23
%26 = tail call i8* @malloc(i32 %25)
store i8 0, i8* %26
%27 = tail call i8* @strcat(i8* %26, i8* dereferenceable(1) %21)
%28 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %26)
%29 = getelementptr i8, i8* %26, i64 %28
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(17) %29, i8* align 1 dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 17, i1 false)
%30 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %11
%33 = tail call i32 @free(i8* %26)
br label %36
34: ; preds = %11, %34
%35 = tail call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 24, i64 1, i32* %30)
br label %34
36: ; preds = %7, %5, %2, %32
%37 = phi i32* [ @EXIT_FAILURE, %32 ], [ @EXIT_FAILURE, %2 ], [ @EXIT_SUCCESS, %5 ], [ @EXIT_FAILURE, %7 ]
%38 = load i32, i32* %37
ret i32 %38
}