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

/* Variables and functions */
int check_pid(char const *);
scalar_t__ daemon(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int write_pid(char const *);
int daemon_init(const char *pidfile) {
  int pid = check_pid(pidfile);
  if (pid) {
    fprintf(stderr, "Skynet is already running, pid = %d.\n", pid);
    return 1;
  }
  if (daemon(1, 0)) {
    fprintf(stderr, "Can't daemonize.\n");
    return 1;
  }
  pid = write_pid(pidfile);
  if (pid == 0) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <check_pid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1188 <daemon_init+0x48>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe8b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ea <daemon_init+0xaa>
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <daemon@plt>
cmp    $0x0,%rax
je     11c1 <daemon_init+0x81>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ea <daemon_init+0xaa>
mov    -0x10(%rbp),%rdi
call   1050 <write_pid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11e3 <daemon_init+0xa3>
movl   $0x1,-0x4(%rbp)
jmp    11ea <daemon_init+0xaa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <check_pid@plt>
test   %eax,%eax
je     116f <daemon_init+0x2f>
mov    0x2e8a(%rip),%rcx
mov    (%rcx),%edi
lea    0xea1(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%ebp
jmp    11ae <daemon_init+0x6e>
mov    $0x1,%ebp
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <daemon@plt>
test   %rax,%rax
je     119e <daemon_init+0x5e>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11ae <daemon_init+0x6e>
mov    %rbx,%rdi
call   1050 <write_pid@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Skynet is already running, pid = %d.\0A\00"
@.str.1 = [18 x i8] c"Can't daemonize.\0A\00"
define i32 @daemon_init(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @check_pid(i8* %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* %4
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 1, i32* %2
br label %26
13: ; preds = %1
%14 = call i64 @daemon(i32 1, i32 0)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %26
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = call i32 @write_pid(i8* %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 1, i32* %2
br label %26
25: ; preds = %19
store i32 0, i32* %2
br label %26
26: ; preds = %25, %24, %16, %9
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Skynet is already running, pid = %d.\0A\00"
@.str.1 = [18 x i8] c"Can't daemonize.\0A\00"
define i32 @daemon_init(i8* %0) {
%2 = tail call i32 @check_pid(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %17
7: ; preds = %1
%8 = tail call i64 @daemon(i32 1, i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %17
13: ; preds = %7
%14 = tail call i32 @write_pid(i8* %0)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
br label %17
17: ; preds = %13, %10, %4
%18 = phi i32 [ 1, %4 ], [ 1, %10 ], [ %16, %13 ]
ret i32 %18
}