#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 EXIT_FAILURE;
int RB_AUTOBOOT;
int RB_HALT_SYSTEM;
int RB_POWER_OFF;
int errno;
int getpid();
int move_mounts();
int nbd_disconnect();
int /*<<< orphan*/ printf(char *, int, char *, ...);
int /*<<< orphan*/ reboot(int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcmp(char *, char *);
char *strerror(int);
int /*<<< orphan*/ sync();
int umount(char *);
int main(int argc, char *argv[]) {
  int flag = RB_POWER_OFF;
  int ret;
  if (argc > 1) {
    if (!strcmp(argv[1], "reboot"))
      flag = RB_AUTOBOOT;
    else if (!strcmp(argv[1], "halt"))
      flag = RB_HALT_SYSTEM;
  }
  ret = move_mounts();
  if (ret < 0)
    goto fail;
  printf("shutdown[%d]: "
         "Unmounting %s\n",
         getpid(), "/oldroot");
  ret = umount("/oldroot");
  if (ret < 0) {
    printf("shutdown[%d]: "
           "error unmounting '%s': %s\n",
           getpid(), "/oldroot", strerror(errno));
    goto fail;
  }
  printf("shutdown[%d]: "
         "Disconnecting %s\n",
         getpid(), "/dev/nbd0");
  ret = nbd_disconnect();
  if (ret < 0) {
    printf("shutdown[%d]: "
           "error disconnecting '%s': %s\n",
           getpid(), "/dev/nbd0", strerror(-ret));
    goto fail;
  }
  (void)reboot(flag);
fail:
  sleep(10);
  sync();
  (void)reboot(flag);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    122d <main+0x8d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe25(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    11fa <main+0x5a>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1228 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1223 <main+0x83>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1228 <main+0x88>
jmp    122d <main+0x8d>
mov    $0x0,%al
call   1040 <move_mounts@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1246 <main+0xa6>
jmp    1323 <main+0x183>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xdb6(%rip),%rdi
lea    0xdcc(%rip),%rdx
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdbe(%rip),%rdi
call   1030 <umount@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12b5 <main+0x115>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %rax,%rcx
lea    0xd90(%rip),%rdi
lea    0xd80(%rip),%rdx
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1323 <main+0x183>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xd96(%rip),%rdi
lea    0xdaf(%rip),%rdx
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <nbd_disconnect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    131b <main+0x17b>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
sub    -0x18(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%esi
mov    %rax,%rcx
lea    0xd7d(%rip),%rdi
lea    0xd6c(%rip),%rdx
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1323 <main+0x183>
mov    -0x14(%rbp),%edi
call   10a0 <reboot@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   1090 <sync@plt>
mov    -0x14(%rbp),%edi
call   10a0 <reboot@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e45(%rip),%rax
mov    (%rax),%r14d
cmp    $0x2,%edi
jl     11f0 <main+0x50>
mov    0x8(%rsi),%rbp
lea    0xe42(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
je     11e6 <main+0x46>
lea    0xe36(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    11f0 <main+0x50>
mov    0x2dec(%rip),%rax
jmp    11ed <main+0x4d>
mov    0x2dd3(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1040 <move_mounts@plt>
test   %eax,%eax
js     12b1 <main+0x111>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdff(%rip),%rdi
lea    0xe15(%rip),%rbp
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1030 <umount@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebp
test   %ebx,%ebx
js     1263 <main+0xc3>
lea    0xe1d(%rip),%rdi
lea    0xe36(%rip),%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <nbd_disconnect@plt>
test   %eax,%eax
js     1283 <main+0xe3>
mov    %r14d,%edi
call   10a0 <reboot@plt>
jmp    12b1 <main+0x111>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdba(%rip),%rdi
lea    0xdaa(%rip),%rdx
mov    %ebp,%esi
jmp    12a7 <main+0x107>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
neg    %ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xde7(%rip),%rdi
lea    0xdd6(%rip),%rdx
mov    %ebx,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1090 <sync@plt>
mov    %r14d,%edi
call   10a0 <reboot@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@RB_POWER_OFF = global i32 0
@.str = [7 x i8] c"reboot\00"
@RB_AUTOBOOT = global i32 0
@.str.1 = [5 x i8] c"halt\00"
@RB_HALT_SYSTEM = global i32 0
@.str.2 = [29 x i8] c"shutdown[%d]: Unmounting %s\0A\00"
@.str.3 = [9 x i8] c"/oldroot\00"
@.str.4 = [41 x i8] c"shutdown[%d]: error unmounting '%s': %s\0A\00"
@errno = global i32 0
@.str.5 = [32 x i8] c"shutdown[%d]: Disconnecting %s\0A\00"
@.str.6 = [10 x i8] c"/dev/nbd0\00"
@.str.7 = [44 x i8] c"shutdown[%d]: error disconnecting '%s': %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @RB_POWER_OFF
store i32 %8, i32* %6
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %29
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @strcmp(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = load i32, i32* @RB_AUTOBOOT
store i32 %18, i32* %6
br label %28
19: ; preds = %11
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = load i32, i32* @RB_HALT_SYSTEM
store i32 %26, i32* %6
br label %27
27: ; preds = %25, %19
br label %28
28: ; preds = %27, %17
br label %29
29: ; preds = %28, %2
%30 = call i32 (...) @move_mounts()
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
br label %60
34: ; preds = %29
%35 = call i32 (...) @getpid()
%36 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @umount(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %34
%41 = call i32 (...) @getpid()
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %41, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %60
45: ; preds = %34
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %46, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%48 = call i32 (...) @nbd_disconnect()
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %45
%52 = call i32 (...) @getpid()
%53 = load i32, i32* %7
%54 = sub nsw i32 0, %53
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %52, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %55)
br label %60
57: ; preds = %45
%58 = load i32, i32* %6
%59 = call i32 @reboot(i32 %58)
br label %60
60: ; preds = %57, %51, %40, %33
%61 = call i32 @sleep(i32 10)
%62 = call i32 (...) @sync()
%63 = load i32, i32* %6
%64 = call i32 @reboot(i32 %63)
%65 = load i32, i32* @EXIT_FAILURE
ret i32 %65
}

LLVM IR (-O2):
@RB_POWER_OFF = global i32 0
@.str = [7 x i8] c"reboot\00"
@RB_AUTOBOOT = global i32 0
@.str.1 = [5 x i8] c"halt\00"
@RB_HALT_SYSTEM = global i32 0
@.str.2 = [29 x i8] c"shutdown[%d]: Unmounting %s\0A\00"
@.str.3 = [9 x i8] c"/oldroot\00"
@.str.4 = [41 x i8] c"shutdown[%d]: error unmounting '%s': %s\0A\00"
@errno = global i32 0
@.str.5 = [32 x i8] c"shutdown[%d]: Disconnecting %s\0A\00"
@.str.6 = [10 x i8] c"/dev/nbd0\00"
@.str.7 = [44 x i8] c"shutdown[%d]: error disconnecting '%s': %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @RB_POWER_OFF
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %7, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = load i32, i32* @RB_AUTOBOOT
br label %17
12: ; preds = %5
%13 = tail call i32 @strcmp(i8* dereferenceable(1) %7, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* @RB_HALT_SYSTEM
%16 = select i1 %14, i32 %15, i32 %3
br label %17
17: ; preds = %12, %10, %2
%18 = phi i32 [ %11, %10 ], [ %3, %2 ], [ %16, %12 ]
%19 = tail call i32 (...) @move_mounts()
%20 = icmp slt i32 %19, 0
br i1 %20, label %42, label %21
21: ; preds = %17
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @umount(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp slt i32 %24, 0
%26 = tail call i32 (...) @getpid()
br i1 %25, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %42
31: ; preds = %21
%32 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%33 = tail call i32 (...) @nbd_disconnect()
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = tail call i32 (...) @getpid()
%37 = sub nsw i32 0, %33
%38 = tail call i8* @strerror(i32 %37)
%39 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %36, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %38)
br label %42
40: ; preds = %31
%41 = tail call i32 @reboot(i32 %18)
br label %42
42: ; preds = %17, %40, %35, %27
%43 = tail call i32 @sleep(i32 10)
%44 = tail call i32 (...) @sync()
%45 = tail call i32 @reboot(i32 %18)
%46 = load i32, i32* @EXIT_FAILURE
ret i32 %46
}