#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *, char *);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ stat_file(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int mk_reboot_script() {
  char buf[512];
  char *rc;
  int fd;
  unlink("/reboot");
  rc = NULL;
  if (stat_file("/etc/rc.d/rc6.d"))
    rc = "/etc/rc.d/rc6.d";
  if (stat_file("/etc/rc6.d"))
    rc = "/etc/rc6.d";
  if (rc == NULL)
    return 1;
  sprintf(buf,
          "%s/"
          "S00vzreboot",
          rc);
  if ((fd = open(buf, O_CREAT | O_WRONLY | O_TRUNC, 0755)) < 0) {
    logger(-1, errno, "Unable to create %s", buf);
    return 1;
  }
  write(fd,
        "#!/bin/bash\n>"
        "/reboot",
        sizeof("#!/bin/bash\n>"
               "/reboot") -
            1);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<mk_reboot_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
lea    0xec9(%rip),%rdi
call   1030 <unlink@plt>
movq   $0x0,-0x218(%rbp)
lea    0xe67(%rip),%rdi
call   1060 <stat_file@plt>
cmp    $0x0,%rax
je     11b6 <mk_reboot_script+0x46>
lea    0xe51(%rip),%rax
mov    %rax,-0x218(%rbp)
lea    0xe53(%rip),%rdi
call   1060 <stat_file@plt>
cmp    $0x0,%rax
je     11da <mk_reboot_script+0x6a>
lea    0xe3d(%rip),%rax
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11f4 <mk_reboot_script+0x84>
movl   $0x1,-0x4(%rbp)
jmp    129f <mk_reboot_script+0x12f>
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rdx
lea    0xe12(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x210(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   1080 <open@plt>
mov    %eax,-0x21c(%rbp)
cmp    $0x0,%eax
jge    1276 <mk_reboot_script+0x106>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
lea    -0x210(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xdc5(%rip),%rdx
call   1070 <logger@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <mk_reboot_script+0x12f>
mov    -0x21c(%rbp),%edi
lea    0xdbb(%rip),%rsi
mov    $0x14,%edx
call   1040 <write@plt>
mov    -0x21c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_reboot_script>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
lea    0xec7(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe70(%rip),%r15
mov    %r15,%rdi
call   1060 <stat_file@plt>
mov    %rax,%rbx
lea    0xe6e(%rip),%rbp
mov    %rbp,%rdi
call   1060 <stat_file@plt>
mov    $0x1,%r14d
mov    %rax,%rcx
or     %rbx,%rcx
je     1249 <mk_reboot_script+0xd9>
test   %rbx,%rbx
cmove  %rbx,%r15
test   %rax,%rax
cmove  %r15,%rbp
lea    0xe4a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1090 <sprintf@plt>
mov    0x2e12(%rip),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ed,%edx
call   1080 <open@plt>
test   %eax,%eax
js     122c <mk_reboot_script+0xbc>
mov    %eax,%ebx
lea    0xe2a(%rip),%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %r14d,%r14d
jmp    1249 <mk_reboot_script+0xd9>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
lea    0xdee(%rip),%rdx
mov    %rsp,%rcx
mov    $0xffffffff,%edi
call   1070 <logger@plt>
mov    %r14d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/reboot\00"
@.str.1 = [16 x i8] c"/etc/rc.d/rc6.d\00"
@.str.2 = [11 x i8] c"/etc/rc6.d\00"
@.str.3 = [15 x i8] c"%s/S00vzreboot\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str.4 = [20 x i8] c"Unable to create %s\00"
@.str.5 = [21 x i8] c"#!/bin/bash\0A>/reboot\00"
define i32 @mk_reboot_script() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = call i32 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
%6 = call i64 @stat_file(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %9
9: ; preds = %8, %0
%10 = call i64 @stat_file(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %13
13: ; preds = %12, %9
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 1, i32* %1
br label %38
17: ; preds = %13
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %22, %23
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %21, i32 %26, i32 493)
store i32 %27, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %17
%30 = load i32, i32* @errno
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%32 = call i32 @logger(i32 -1, i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %31)
store i32 1, i32* %1
br label %38
33: ; preds = %17
%34 = load i32, i32* %4
%35 = call i32 @write(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 20)
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
store i32 0, i32* %1
br label %38
38: ; preds = %33, %29, %16
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [8 x i8] c"/reboot\00"
@.str.1 = [16 x i8] c"/etc/rc.d/rc6.d\00"
@.str.2 = [11 x i8] c"/etc/rc6.d\00"
@.str.3 = [15 x i8] c"%s/S00vzreboot\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str.4 = [20 x i8] c"Unable to create %s\00"
@.str.5 = [21 x i8] c"#!/bin/bash\0A>/reboot\00"
define i32 @mk_reboot_script() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = tail call i64 @stat_file(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
%6 = tail call i64 @stat_file(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
%8 = select i1 %7, i1 %5, i1 false
br i1 %8, label %26, label %9
9: ; preds = %0
%10 = select i1 %5, i8* null, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0)
%11 = select i1 %7, i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0)
%12 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %2, i32 %17, i32 493)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %9
%21 = load i32, i32* @errno
%22 = call i32 @logger(i32 -1, i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %26
23: ; preds = %9
%24 = tail call i32 @write(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 20)
%25 = tail call i32 @close(i32 %18)
br label %26
26: ; preds = %0, %23, %20
%27 = phi i32 [ 1, %20 ], [ 0, %23 ], [ 1, %0 ]
ret i32 %27
}