#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 */
struct stat {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ geteuid();
scalar_t__ kill(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (geteuid() != 0) {
    fprintf(stderr, "%s: you must be root\n", argv[0]);
    return 1;
  };
  printf("Running the shutdown script...\n");
  struct stat st;
  if (stat("/etc/init/shutdown.sh", &st) == 0) {
    pid_t pid = fork();
    if (pid == 0) {
      execl("/bin/sh", "sh", "/etc/init/shutdown.sh", NULL);
      return 0;
    } else {
      waitpid(pid, NULL, 0);
    };
  };
  printf("Telling init to halt the system...\n");
  int fd = open("/run/down-action", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (fd == -1) {
    perror("open /run/down-action");
  } else {
    write(fd, argv[0], strlen(argv[0]));
    close(fd);
  };
  if (kill(1, SIGTERM) != 0) {
    perror("kill");
    return 1;
  };
  pause();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
je     122f <main+0x4f>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde2(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x195>
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf4(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    12a9 <main+0xc9>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1295 <main+0xb5>
lea    0xdda(%rip),%rdi
lea    0xdd0(%rip),%rsi
lea    0xdb6(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10f0 <execl@plt>
movl   $0x0,-0x4(%rbp)
jmp    1375 <main+0x195>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <waitpid@plt>
jmp    12a9 <main+0xc9>
lea    0xda4(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    0x2cf3(%rip),%rax
or     (%rax),%esi
mov    0x2d22(%rip),%rax
or     (%rax),%esi
lea    0xda6(%rip),%rdi
mov    $0x1a4,%edx
call   10d0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12ff <main+0x11f>
lea    0xd83(%rip),%rdi
call   10e0 <perror@plt>
jmp    1332 <main+0x152>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
call   10a0 <kill@plt>
cmp    $0x0,%rax
je     1367 <main+0x187>
lea    0xd38(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x195>
mov    $0x0,%al
call   10b0 <pause@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
je     1215 <main+0x35>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdfa(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12d6 <main+0xf6>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%rdi
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %rax,%rax
jne    1253 <main+0x73>
xor    %ebx,%ebx
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
je     12e0 <main+0x100>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <waitpid@plt>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d92(%rip),%rax
mov    0x2d4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d78(%rip),%rax
or     (%rax),%esi
lea    0xdb8(%rip),%rdi
mov    $0x1a4,%edx
call   10d0 <open@plt>
cmp    $0xffffffff,%eax
je     12fe <main+0x11e>
mov    %eax,%ebx
mov    (%r14),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%ebx
mov    $0x1,%edi
call   10a0 <kill@plt>
test   %rax,%rax
jne    130c <main+0x12c>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <pause@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd45(%rip),%rdi
lea    0xd3b(%rip),%rsi
lea    0xd21(%rip),%rdx
xor    %ecx,%ecx
call   10f0 <execl@plt>
jmp    12d6 <main+0xf6>
lea    0xd2f(%rip),%rdi
call   10e0 <perror@plt>
jmp    12b0 <main+0xd0>
lea    0xd37(%rip),%rdi
call   10e0 <perror@plt>
jmp    12d6 <main+0xf6>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"%s: you must be root\0A\00"
@.str.1 = [32 x i8] c"Running the shutdown script...\0A\00"
@.str.2 = [22 x i8] c"/etc/init/shutdown.sh\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [36 x i8] c"Telling init to halt the system...\0A\00"
@.str.6 = [17 x i8] c"/run/down-action\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.7 = [22 x i8] c"open /run/down-action\00"
@SIGTERM = global i32 0
@.str.8 = [5 x i8] c"kill\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @geteuid()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %63
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%19 = call i64 @stat(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %6)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = call i64 (...) @fork()
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32* null)
store i32 0, i32* %3
br label %63
27: ; preds = %21
%28 = load i64, i64* %7
%29 = call i32 @waitpid(i64 %28, i32* null, i32 0)
br label %30
30: ; preds = %27
br label %31
31: ; preds = %30, %17
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @O_CREAT
%34 = load i32, i32* @O_WRONLY
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %37, i32 420)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %55
43: ; preds = %31
%44 = load i32, i32* %8
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 0
%47 = load i8*, i8** %46
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %44, i8* %47, i32 %51)
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %43, %41
%56 = load i32, i32* @SIGTERM
%57 = call i64 @kill(i32 1, i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %63
61: ; preds = %55
%62 = call i32 (...) @pause()
store i32 0, i32* %3
br label %63
63: ; preds = %61, %59, %25, %11
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"%s: you must be root\0A\00"
@.str.2 = [22 x i8] c"/etc/init/shutdown.sh\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.6 = [17 x i8] c"/run/down-action\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.7 = [22 x i8] c"open /run/down-action\00"
@SIGTERM = global i32 0
@.str.8 = [5 x i8] c"kill\00"
@str = [31 x i8] c"Running the shutdown script...\00"
@str.9 = [35 x i8] c"Telling init to halt the system...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = tail call i64 (...) @geteuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %48
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%12 = bitcast %struct.stat* %3 to i8*
%13 = call i64 @stat(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %10
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %46
20: ; preds = %15
%21 = call i32 @waitpid(i64 %16, i32* null, i32 0)
br label %22
22: ; preds = %20, %10
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.9, i64 0, i64 0))
%24 = load i32, i32* @O_CREAT
%25 = load i32, i32* @O_WRONLY
%26 = or i32 %25, %24
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %28, i32 420)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %38
33: ; preds = %22
%34 = load i8*, i8** %1
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @write(i32 %29, i8* %34, i32 %35)
%37 = call i32 @close(i32 %29)
br label %38
38: ; preds = %33, %31
%39 = load i32, i32* @SIGTERM
%40 = call i64 @kill(i32 1, i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %46
44: ; preds = %38
%45 = call i32 (...) @pause()
br label %46
46: ; preds = %18, %42, %44
%47 = phi i32 [ 1, %42 ], [ 0, %44 ], [ 0, %18 ]
br label %48
48: ; preds = %46, %6
%49 = phi i32 [ 1, %6 ], [ %47, %46 ]
ret i32 %49
}