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

/* Variables and functions */
int daemon(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int go_daemon(const char *pid_file) {
  FILE *f;
  if (daemon(0, 0) == -1) {
    perror("daemon");
    return -1;
  }
  if (!pid_file)
    return 0;
  f = fopen(pid_file, "w");
  if (!f) {
    perror("fopen");
    return -1;
  }
  fprintf(f, "%d", getpid());
  if (fclose(f) != 0) {
    perror("fclose");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<go_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1060 <daemon@plt>
cmp    $0xffffffff,%eax
jne    1196 <go_daemon+0x36>
lea    0xe7b(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <go_daemon+0xd7>
cmpq   $0x0,-0x10(%rbp)
jne    11ad <go_daemon+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    1237 <go_daemon+0xd7>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e4 <go_daemon+0x84>
lea    0xe36(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <go_daemon+0xd7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe0f(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1230 <go_daemon+0xd0>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <go_daemon+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<go_daemon>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %edi,%edi
xor    %esi,%esi
call   1060 <daemon@plt>
cmp    $0xffffffff,%eax
je     11c2 <go_daemon+0x62>
test   %rbx,%rbx
je     11b9 <go_daemon+0x59>
lea    0xe85(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11cb <go_daemon+0x6b>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    11d4 <go_daemon+0x74>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe37(%rip),%rdi
jmp    11db <go_daemon+0x7b>
lea    0xe37(%rip),%rdi
jmp    11db <go_daemon+0x7b>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11b9 <go_daemon+0x59>

LLVM IR (-O0):
@.str = [7 x i8] c"daemon\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [7 x i8] c"fclose\00"
define i32 @go_daemon(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = call i32 @daemon(i32 0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %9
store i32 0, i32* %2
br label %30
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
20: ; preds = %13
%21 = load i32*, i32** %4
%22 = call i32 (...) @getpid()
%23 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32*, i32** %4
%25 = call i64 @fclose(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
29: ; preds = %20
store i32 0, i32* %2
br label %30
30: ; preds = %29, %27, %18, %12, %7
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [7 x i8] c"daemon\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [7 x i8] c"fclose\00"
define i32 @go_daemon(i8* %0) {
%2 = tail call i32 @daemon(i32 0, i32 0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = icmp eq i8* %0, null
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = tail call i64 @fclose(i32* %7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9, %6, %1
%15 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %9 ]
%16 = tail call i32 @perror(i8* %15)
br label %17
17: ; preds = %14, %9, %4
%18 = phi i32 [ 0, %4 ], [ 0, %9 ], [ -1, %14 ]
ret i32 %18
}