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

/* Variables and functions */
int /*<<< orphan*/ PR_SET_NAME;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ fork();
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ prctl(int /*<<< orphan*/, char *, int /*<<< orphan*/,
                         int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ wait(int *);
int main(void) {
  int status;
  pid_t p;
  if (p = fork()) {
    wait(&status);
  } else {
    if (p = fork()) {
      wait(&status);
    } else {
      setsid();
      prctl(PR_SET_NAME, (char *)"test-daemon", 0, 0, 0);
      kill(getppid(), SIGTERM);
      pause();
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     11a0 <main+0x30>
lea    -0x8(%rbp),%rdi
call   1080 <wait@plt>
jmp    1209 <main+0x99>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     11c1 <main+0x51>
lea    -0x8(%rbp),%rdi
call   1080 <wait@plt>
jmp    1204 <main+0x94>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1040 <prctl@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    $0x0,%al
call   1060 <pause@plt>
jmp    1209 <main+0x99>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1187 <main+0x17>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1195 <main+0x25>
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <setsid@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <prctl@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
mov    0x2e22(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1050 <kill@plt>
xor    %eax,%eax
call   1060 <pause@plt>
jmp    1191 <main+0x21>

LLVM IR (-O0):
@PR_SET_NAME = global i32 0
@.str = [12 x i8] c"test-daemon\00"
@SIGTERM = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @wait(i32* %2)
br label %22
8: ; preds = %0
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @wait(i32* %2)
br label %21
13: ; preds = %8
%14 = call i32 (...) @setsid()
%15 = load i32, i32* @PR_SET_NAME
%16 = call i32 @prctl(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 0, i32 0, i32 0)
%17 = call i32 (...) @getppid()
%18 = load i32, i32* @SIGTERM
%19 = call i32 @kill(i32 %17, i32 %18)
%20 = call i32 (...) @pause()
br label %21
21: ; preds = %13, %11
br label %22
22: ; preds = %21, %6
ret i32 0
}

LLVM IR (-O2):
@PR_SET_NAME = global i32 0
@.str = [12 x i8] c"test-daemon\00"
@SIGTERM = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @wait(i32* %1)
br label %20
7: ; preds = %0
%8 = tail call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = call i32 @wait(i32* %1)
br label %20
12: ; preds = %7
%13 = tail call i32 (...) @setsid()
%14 = load i32, i32* @PR_SET_NAME
%15 = tail call i32 @prctl(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 0, i32 0, i32 0)
%16 = tail call i32 (...) @getppid()
%17 = load i32, i32* @SIGTERM
%18 = tail call i32 @kill(i32 %16, i32 %17)
%19 = tail call i32 (...) @pause()
br label %20
20: ; preds = %10, %12, %5
ret i32 0
}