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

/* Variables and functions */
int NOFILE;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
int getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon(void) {
  int pid;
  int i;
  char *id;
  FILE *fp;
  pid_t p;
  pid = fork();
  if (pid > 0)
    exit(0);
  else if (pid < 0)
    exit(1);
  setsid();
  pid = fork();
  if (pid > 0)
    exit(0);
  else if (pid < 0)
    exit(1);
  for (i = 0; i < NOFILE; ++i)
    close(i);
  chdir("/tmp");
  umask(0);
  p = getpid();
  id = (char *)malloc(16);
  sprintf(id, "%d", p);
  fp = fopen("/tmp/weather_home.pid", "w+");
  fwrite(id, 1, strlen(id), fp);
  fclose(fp);
  free(id);
  return;
}


x86 assembly (clang, -O0):
<init_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1203 <init_daemon+0x23>
xor    %edi,%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1217 <init_daemon+0x37>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    121c <init_daemon+0x3c>
mov    $0x0,%al
call   1090 <setsid@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    123e <init_daemon+0x5e>
xor    %edi,%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1252 <init_daemon+0x72>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    1257 <init_daemon+0x77>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%eax
jge    1286 <init_daemon+0xa6>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    125e <init_daemon+0x7e>
lea    0xd73(%rip),%rdi
call   1070 <chdir@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd46(%rip),%rsi
call   10d0 <sprintf@plt>
lea    0xd3d(%rip),%rdi
lea    0xd4c(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jg     12bf <init_daemon+0xdf>
js     12c6 <init_daemon+0xe6>
xor    %eax,%eax
call   1090 <setsid@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jg     12bf <init_daemon+0xdf>
js     12c6 <init_daemon+0xe6>
mov    0x2dbd(%rip),%rbp
cmpl   $0x0,0x0(%rbp)
jle    123f <init_daemon+0x5f>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1080 <close@plt>
add    $0x1,%ebx
cmp    0x0(%rbp),%ebx
jl     1230 <init_daemon+0x50>
lea    0xdba(%rip),%rdi
call   1070 <chdir@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
lea    0xd96(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   10d0 <sprintf@plt>
lea    0xd88(%rip),%rdi
lea    0xd97(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbp,%rcx
call   10f0 <fwrite@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <free@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@NOFILE = global i32 0
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [22 x i8] c"/tmp/weather_home.pid\00"
@.str.3 = [3 x i8] c"w+\00"
define void @init_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = call i32 (...) @fork()
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = load i32, i32* %1
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %11
br label %17
17: ; preds = %16
%18 = call i32 (...) @setsid()
%19 = call i32 (...) @fork()
store i32 %19, i32* %1
%20 = load i32, i32* %1
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %17
%25 = load i32, i32* %1
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %24
br label %30
30: ; preds = %29
store i32 0, i32* %2
br label %31
31: ; preds = %38, %30
%32 = load i32, i32* %2
%33 = load i32, i32* @NOFILE
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35
%39 = load i32, i32* %2
%40 = add nsw i32 %39, 1
store i32 %40, i32* %2
br label %31
41: ; preds = %31
%42 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%43 = call i32 @umask(i32 0)
%44 = call i32 (...) @getpid()
store i32 %44, i32* %5
%45 = call i64 @malloc(i32 16)
%46 = inttoptr i64 %45 to i8*
store i8* %46, i8** %3
%47 = load i8*, i8** %3
%48 = load i32, i32* %5
%49 = call i32 @sprintf(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %50, i32** %4
%51 = load i8*, i8** %3
%52 = load i8*, i8** %3
%53 = call i32 @strlen(i8* %52)
%54 = load i32*, i32** %4
%55 = call i32 @fwrite(i8* %51, i32 1, i32 %53, i32* %54)
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %3
%59 = call i32 @free(i8* %58)
ret void
}

LLVM IR (-O2):
@NOFILE = global i32 0
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [22 x i8] c"/tmp/weather_home.pid\00"
@.str.3 = [3 x i8] c"w+\00"
define void @init_daemon() {
%1 = tail call i32 (...) @fork()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = icmp slt i32 %1, 0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %5
%10 = tail call i32 (...) @setsid()
%11 = tail call i32 (...) @fork()
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %9
%16 = icmp slt i32 %11, 0
br i1 %16, label %20, label %17
17: ; preds = %15
%18 = load i32, i32* @NOFILE
%19 = icmp sgt i32 %18, 0
br i1 %19, label %22, label %28
20: ; preds = %15
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %17, %22
%23 = phi i32 [ %25, %22 ], [ 0, %17 ]
%24 = tail call i32 @close(i32 %23)
%25 = add nuw nsw i32 %23, 1
%26 = load i32, i32* @NOFILE
%27 = icmp slt i32 %25, %26
br i1 %27, label %22, label %28
28: ; preds = %22, %17
%29 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%30 = tail call i32 @umask(i32 0)
%31 = tail call i32 (...) @getpid()
%32 = tail call i64 @malloc(i32 16)
%33 = inttoptr i64 %32 to i8*
%34 = tail call i32 @sprintf(i8* dereferenceable(1) %33, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%35 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @strlen(i8* %33)
%37 = tail call i32 @fwrite(i8* %33, i32 1, i32 %36, i32* %35)
%38 = tail call i32 @fclose(i32* %35)
%39 = tail call i32 @free(i8* %33)
ret void
}