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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int SIGKILL;
int SIGTERM;
int SIGUSR1;
int SIGUSR2;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_UNBLOCK;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ kill(int, int);
int /*<<< orphan*/ reboot(unsigned int);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sync();
scalar_t__ vfork();
void init_halt_reboot_poweroff(int sig) {
  char *p;
  sigset_t set;
  pid_t pid;
  unsigned rb;
  signal(SIGUSR1, SIG_DFL);
  signal(SIGTERM, SIG_DFL);
  signal(SIGUSR2, SIG_DFL);
  sigfillset(&set);
  sigprocmask(SIG_UNBLOCK, &set, NULL);
  kill(-1, SIGTERM);
  sync();
  sleep(1);
  kill(-1, SIGKILL);
  sync();
  sleep(1);
  p = "halt";
  rb = 0xcdef0123;
  if (sig == SIGTERM) {
    p = "reboot";
    rb = 0x01234567;
  } else if (sig == SIGUSR2) {
    p = "poweroff";
    rb = 0x4321fedc;
  };
  pid = vfork();
  if (pid == 0) {
    reboot(rb);
    _exit(EXIT_SUCCESS);
  }
  while (1)
    sleep(1);
}


x86 assembly (clang, -O0):
<init_halt_reboot_poweroff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
lea    -0x14(%rbp),%rdi
call   1060 <sigfillset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
call   1070 <kill@plt>
mov    $0x0,%al
call   1080 <sync@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
call   1070 <kill@plt>
mov    $0x0,%al
call   1080 <sync@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    0xdb2(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0xcdef0123,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
jne    1282 <init_halt_reboot_poweroff+0xf2>
lea    0xd93(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1234567,-0x24(%rbp)
jmp    12ab <init_halt_reboot_poweroff+0x11b>
mov    -0x4(%rbp),%eax
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%eax
jne    12a6 <init_halt_reboot_poweroff+0x116>
lea    0xd71(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x4321fedc,-0x24(%rbp)
jmp    12ab <init_halt_reboot_poweroff+0x11b>
mov    $0x0,%al
call   1090 <vfork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d7 <init_halt_reboot_poweroff+0x147>
mov    -0x24(%rbp),%edi
call   10a0 <reboot@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>
jmp    12dc <init_halt_reboot_poweroff+0x14c>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    12dc <init_halt_reboot_poweroff+0x14c>

x86 assembly (clang, -O2):
<init_halt_reboot_poweroff>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e16(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e20(%rip),%rbp
mov    0x0(%rbp),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2dff(%rip),%r15
mov    (%r15),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sigfillset@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x0(%rbp),%esi
mov    $0xffffffff,%edi
call   1070 <kill@plt>
xor    %eax,%eax
call   1080 <sync@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
call   1070 <kill@plt>
xor    %eax,%eax
call   1080 <sync@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x0(%rbp),%ebx
mov    (%r15),%ebp
xor    %eax,%eax
call   1090 <vfork@plt>
test   %rax,%rax
je     125c <init_halt_reboot_poweroff+0xcc>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1250 <init_halt_reboot_poweroff+0xc0>
cmp    %r14d,%ebp
mov    $0x4321fedc,%eax
mov    $0xcdef0123,%ecx
cmove  %eax,%ecx
cmp    %r14d,%ebx
mov    $0x1234567,%edi
cmovne %ecx,%edi
call   10a0 <reboot@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@SIG_DFL = global i32 0
@SIGTERM = global i32 0
@SIGUSR2 = global i32 0
@SIG_UNBLOCK = global i32 0
@SIGKILL = global i32 0
@.str = [5 x i8] c"halt\00"
@.str.1 = [7 x i8] c"reboot\00"
@.str.2 = [9 x i8] c"poweroff\00"
@EXIT_SUCCESS = global i32 0
define void @init_halt_reboot_poweroff(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* @SIGUSR1
%8 = load i32, i32* @SIG_DFL
%9 = call i32 @signal(i32 %7, i32 %8)
%10 = load i32, i32* @SIGTERM
%11 = load i32, i32* @SIG_DFL
%12 = call i32 @signal(i32 %10, i32 %11)
%13 = load i32, i32* @SIGUSR2
%14 = load i32, i32* @SIG_DFL
%15 = call i32 @signal(i32 %13, i32 %14)
%16 = call i32 @sigfillset(i32* %4)
%17 = load i32, i32* @SIG_UNBLOCK
%18 = call i32 @sigprocmask(i32 %17, i32* %4, i32* null)
%19 = load i32, i32* @SIGTERM
%20 = call i32 @kill(i32 -1, i32 %19)
%21 = call i32 (...) @sync()
%22 = call i32 @sleep(i32 1)
%23 = load i32, i32* @SIGKILL
%24 = call i32 @kill(i32 -1, i32 %23)
%25 = call i32 (...) @sync()
%26 = call i32 @sleep(i32 1)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %3
store i32 -839974621, i32* %6
%27 = load i32, i32* %2
%28 = load i32, i32* @SIGTERM
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 19088743, i32* %6
br label %37
31: ; preds = %1
%32 = load i32, i32* %2
%33 = load i32, i32* @SIGUSR2
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i32 1126301404, i32* %6
br label %36
36: ; preds = %35, %31
br label %37
37: ; preds = %36, %30
%38 = call i64 (...) @vfork()
store i64 %38, i64* %5
%39 = load i64, i64* %5
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* %6
%43 = call i32 @reboot(i32 %42)
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @_exit(i32 %44)
unreachable
46: ; preds = %37
br label %47
47: ; preds = %46, %47
%48 = call i32 @sleep(i32 1)
br label %47
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
@SIG_DFL = global i32 0
@SIGTERM = global i32 0
@SIGUSR2 = global i32 0
@SIG_UNBLOCK = global i32 0
@SIGKILL = global i32 0
@EXIT_SUCCESS = global i32 0
define void @init_halt_reboot_poweroff(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @SIGUSR1
%5 = load i32, i32* @SIG_DFL
%6 = tail call i32 @signal(i32 %4, i32 %5)
%7 = load i32, i32* @SIGTERM
%8 = load i32, i32* @SIG_DFL
%9 = tail call i32 @signal(i32 %7, i32 %8)
%10 = load i32, i32* @SIGUSR2
%11 = load i32, i32* @SIG_DFL
%12 = tail call i32 @signal(i32 %10, i32 %11)
%13 = call i32 @sigfillset(i32* %2)
%14 = load i32, i32* @SIG_UNBLOCK
%15 = call i32 @sigprocmask(i32 %14, i32* %2, i32* null)
%16 = load i32, i32* @SIGTERM
%17 = call i32 @kill(i32 -1, i32 %16)
%18 = call i32 (...) @sync()
%19 = call i32 @sleep(i32 1)
%20 = load i32, i32* @SIGKILL
%21 = call i32 @kill(i32 -1, i32 %20)
%22 = call i32 (...) @sync()
%23 = call i32 @sleep(i32 1)
%24 = load i32, i32* @SIGTERM
%25 = load i32, i32* @SIGUSR2
%26 = call i64 (...) @vfork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %1
%29 = icmp eq i32 %24, %0
%30 = icmp eq i32 %25, %0
%31 = select i1 %30, i32 1126301404, i32 -839974621
%32 = select i1 %29, i32 19088743, i32 %31
%33 = call i32 @reboot(i32 %32)
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @_exit(i32 %34)
unreachable
36: ; preds = %1, %36
%37 = call i32 @sleep(i32 1)
br label %36
}