#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 /*<<< orphan*/ _exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_pid_file();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  (void)argc;
  (void)argv;
  printf("This is a testing daemon pid %i, that will fork and leave pid in "
         "/run/test.pid"
         ". \n",
         getpid());
  if (fork() == 0) {
    printf("This is fork, pid %i\n", getpid());
    printf("Setting pid now!\n");
    set_pid_file();
    sleep(20);
    printf("Child will now die!\n");
    unlink("/run/test.pid");
    _exit(1);
  }
  sleep(1);
  printf("Fork launched!\n");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    1208 <main+0x98>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <set_pid_file@plt>
mov    $0x14,%edi
call   1080 <sleep@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe96(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11be <main+0x3e>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xec1(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xea9(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1080 <set_pid_file@plt>
mov    $0x14,%edi
call   1090 <sleep@plt>
lea    0xe9d(%rip),%rdi
call   1050 <puts@plt>
lea    0xe63(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@.str = [82 x i8] c"This is a testing daemon pid %i, that will fork and leave pid in /run/test.pid. \0A\00"
@.str.1 = [22 x i8] c"This is fork, pid %i\0A\00"
@.str.2 = [18 x i8] c"Setting pid now!\0A\00"
@.str.3 = [21 x i8] c"Child will now die!\0A\00"
@.str.4 = [14 x i8] c"/run/test.pid\00"
@.str.5 = [16 x i8] c"Fork launched!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = load i8**, i8*** %5
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 (...) @set_pid_file()
%17 = call i32 @sleep(i32 20)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @_exit(i32 1)
unreachable
21: ; preds = %2
%22 = call i32 @sleep(i32 1)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [82 x i8] c"This is a testing daemon pid %i, that will fork and leave pid in /run/test.pid. \0A\00"
@.str.1 = [22 x i8] c"This is fork, pid %i\0A\00"
@.str.4 = [14 x i8] c"/run/test.pid\00"
@str = [15 x i8] c"Fork launched!\00"
@str.6 = [17 x i8] c"Setting pid now!\00"
@str.7 = [20 x i8] c"Child will now die!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
%11 = tail call i32 (...) @set_pid_file()
%12 = tail call i32 @sleep(i32 20)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
%14 = tail call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @_exit(i32 1)
unreachable
16: ; preds = %2
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
ret i32 0
}