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

/* Variables and functions */
int /*<<< orphan*/ BLKFLSBUF;
int /*<<< orphan*/ HDIO_DRIVE_CMD;
int /*<<< orphan*/ NAME_MAX;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, ...);
int open(char *, int);
int snprintf(char *, int, char *, char *);
int do_standby_disk(char *device, unsigned int flags) {
  unsigned char flush1[4] = {0xEA, 0, 0, 0};
  unsigned char flush2[4] = {0xE7, 0, 0, 0};
  unsigned char stdby1[4] = {0xE0, 0, 0, 0};
  unsigned char stdby2[4] = {0x94, 0, 0, 0};
  char buf[NAME_MAX + 1];
  int fd, ret;
  ret = snprintf(buf, sizeof(buf),
                 "/dev"
                 "/%s",
                 device);
  if ((ret >= (int)sizeof(buf)) || (ret < 0))
    return -1;
  if ((fd = open(buf, O_RDWR | O_NONBLOCK)) < 0)
    return -1;
  switch (flags & 0x00000004) {
  case 0x00000004:
    if ((ret = ioctl(fd, HDIO_DRIVE_CMD, &flush1)) == 0)
      break;
  default:
    ret = ioctl(fd, HDIO_DRIVE_CMD, &flush2) && ioctl(fd, BLKFLSBUF);
    break;
  }
  if ((flags & 0x00000020) == 0x0) {
    ret = ioctl(fd, HDIO_DRIVE_CMD, &stdby1) &&
          ioctl(fd, HDIO_DRIVE_CMD, &stdby2);
  }
  close(fd);
  if (ret)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<do_standby_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xea,-0x18(%rbp)
movl   $0xe7,-0x1c(%rbp)
movl   $0xe0,-0x20(%rbp)
movl   $0x94,-0x24(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe65(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x50(%rbp),%rcx
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    %ecx,%eax
jge    11cc <do_standby_disk+0x8c>
cmpl   $0x0,-0x40(%rbp)
jge    11df <do_standby_disk+0x9f>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1338 <do_standby_disk+0x1f8>
mov    -0x58(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    1219 <do_standby_disk+0xd9>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1338 <do_standby_disk+0x1f8>
mov    -0x14(%rbp),%al
test   $0x4,%al
je     1256 <do_standby_disk+0x116>
jmp    1229 <do_standby_disk+0xe9>
mov    -0x3c(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jne    1251 <do_standby_disk+0x111>
jmp    12a4 <do_standby_disk+0x164>
jmp    1256 <do_standby_disk+0x116>
mov    -0x3c(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x59(%rbp)
je     1299 <do_standby_disk+0x159>
mov    -0x3c(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <ioctl@plt>
cmp    $0x0,%eax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
jne    1305 <do_standby_disk+0x1c5>
mov    -0x3c(%rbp),%edi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x5a(%rbp)
je     12fa <do_standby_disk+0x1ba>
mov    -0x3c(%rbp),%edi
mov    0x2cec(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
cmp    $0x0,%eax
setne  %al
mov    %al,-0x5a(%rbp)
mov    -0x5a(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x40(%rbp)
je     132a <do_standby_disk+0x1ea>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1338 <do_standby_disk+0x1f8>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_standby_disk>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rcx
mov    %rsp,%r12
movl   $0xea,-0x38(%rbp)
movl   $0xe7,-0x34(%rbp)
movl   $0xe0,-0x30(%rbp)
movl   $0x94,-0x2c(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%r13d
lea    0x1(%r13),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe64(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
mov    $0xffffffff,%r15d
cmp    %r13d,%eax
jg     128b <do_standby_disk+0x14b>
test   %eax,%eax
js     128b <do_standby_disk+0x14b>
mov    0x2e26(%rip),%rax
mov    0x2e17(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     128b <do_standby_disk+0x14b>
mov    %eax,%r13d
test   $0x4,%r14b
je     1203 <do_standby_disk+0xc3>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
lea    -0x38(%rbp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %eax,%eax
je     123a <do_standby_disk+0xfa>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
lea    -0x34(%rbp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %eax,%eax
je     123a <do_standby_disk+0xfa>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
xor    %ebx,%ebx
test   %eax,%eax
setne  %bl
test   $0x20,%r14b
jne    1272 <do_standby_disk+0x132>
mov    0x2d89(%rip),%rbx
mov    (%rbx),%esi
lea    -0x30(%rbp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %eax,%eax
je     1280 <do_standby_disk+0x140>
mov    (%rbx),%esi
lea    -0x2c(%rbp),%rdx
mov    %r13d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
xor    %ebx,%ebx
test   %eax,%eax
setne  %bl
mov    %r13d,%edi
call   1050 <close@plt>
test   %ebx,%ebx
jne    128b <do_standby_disk+0x14b>
jmp    1288 <do_standby_disk+0x148>
mov    %r13d,%edi
call   1050 <close@plt>
xor    %r15d,%r15d
mov    %r12,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.do_standby_disk.flush1 = [4 x i8] c"\EA\00\00\00"
@__const.do_standby_disk.flush2 = [4 x i8] c"\E7\00\00\00"
@__const.do_standby_disk.stdby1 = [4 x i8] c"\E0\00\00\00"
@__const.do_standby_disk.stdby2 = [4 x i8] c"\94\00\00\00"
@NAME_MAX = global i32 0
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@HDIO_DRIVE_CMD = global i32 0
@BLKFLSBUF = global i32 0
define i32 @do_standby_disk(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca [4 x i8]
%8 = alloca [4 x i8]
%9 = alloca [4 x i8]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = bitcast [4 x i8]* %6 to i8*
%16 = bitcast [4 x i8]* %7 to i8*
%17 = bitcast [4 x i8]* %8 to i8*
%18 = bitcast [4 x i8]* %9 to i8*
%19 = load i32, i32* @NAME_MAX
%20 = add nsw i32 %19, 1
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %10
%23 = alloca i8, i64 %21
store i64 %21, i64* %11
%24 = trunc i64 %21 to i32
%25 = load i8*, i8** %4
%26 = call i32 @snprintf(i8* %23, i32 %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 %26, i32* %13
%27 = load i32, i32* %13
%28 = trunc i64 %21 to i32
%29 = icmp sge i32 %27, %28
br i1 %29, label %33, label %30
30: ; preds = %2
%31 = load i32, i32* %13
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30, %2
store i32 -1, i32* %3
store i32 1, i32* %14
br label %88
34: ; preds = %30
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %23, i32 %37)
store i32 %38, i32* %12
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 -1, i32* %3
store i32 1, i32* %14
br label %88
41: ; preds = %34
%42 = load i32, i32* %5
%43 = and i32 %42, 4
switch i32 %43, label %51 [
i32 4, label %44
]
44: ; preds = %41
%45 = load i32, i32* %12
%46 = load i32, i32* @HDIO_DRIVE_CMD
%47 = call i32 (i32, i32, ...) @ioctl(i32 %45, i32 %46, [4 x i8]* %6)
store i32 %47, i32* %13
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %64
50: ; preds = %44
br label %51
51: ; preds = %41, %50
%52 = load i32, i32* %12
%53 = load i32, i32* @HDIO_DRIVE_CMD
%54 = call i32 (i32, i32, ...) @ioctl(i32 %52, i32 %53, [4 x i8]* %7)
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i32, i32* %12
%58 = load i32, i32* @BLKFLSBUF
%59 = call i32 (i32, i32, ...) @ioctl(i32 %57, i32 %58)
%60 = icmp ne i32 %59, 0
br label %61
61: ; preds = %56, %51
%62 = phi i1 [ false, %51 ], [ %60, %56 ]
%63 = zext i1 %62 to i32
store i32 %63, i32* %13
br label %64
64: ; preds = %61, %49
%65 = load i32, i32* %5
%66 = and i32 %65, 32
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %81
68: ; preds = %64
%69 = load i32, i32* %12
%70 = load i32, i32* @HDIO_DRIVE_CMD
%71 = call i32 (i32, i32, ...) @ioctl(i32 %69, i32 %70, [4 x i8]* %8)
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %68
%74 = load i32, i32* %12
%75 = load i32, i32* @HDIO_DRIVE_CMD
%76 = call i32 (i32, i32, ...) @ioctl(i32 %74, i32 %75, [4 x i8]* %9)
%77 = icmp ne i32 %76, 0
br label %78
78: ; preds = %73, %68
%79 = phi i1 [ false, %68 ], [ %77, %73 ]
%80 = zext i1 %79 to i32
store i32 %80, i32* %13
br label %81
81: ; preds = %78, %64
%82 = load i32, i32* %12
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %13
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %81
store i32 -1, i32* %3
store i32 1, i32* %14
br label %88
87: ; preds = %81
store i32 0, i32* %3
store i32 1, i32* %14
br label %88
88: ; preds = %87, %86, %40, %33
%89 = load i8*, i8** %10
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@HDIO_DRIVE_CMD = global i32 0
@BLKFLSBUF = global i32 0
define i32 @do_standby_disk(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
store i32 234, i32* %3
%8 = bitcast i32* %4 to i8*
store i32 231, i32* %4
%9 = bitcast i32* %5 to i8*
store i32 224, i32* %5
%10 = bitcast i32* %6 to i8*
store i32 148, i32* %6
%11 = load i32, i32* @NAME_MAX
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = tail call i8* @llvm.stacksave()
%15 = alloca i8, i64 %13
%16 = call i32 @snprintf(i8* %15, i32 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = icmp sgt i32 %16, %11
%18 = icmp slt i32 %16, 0
%19 = or i1 %17, %18
br i1 %19, label %62, label %20
20: ; preds = %2
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %22, %21
%24 = call i32 @open(i8* %15, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %62, label %26
26: ; preds = %20
%27 = and i32 %1, 4
%28 = icmp eq i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = load i32, i32* @HDIO_DRIVE_CMD
%31 = call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %30, i32* %3)
%32 = icmp eq i32 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %29, %26
%34 = load i32, i32* @HDIO_DRIVE_CMD
%35 = call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %34, i32* %4)
%36 = icmp eq i32 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %33
%38 = load i32, i32* @BLKFLSBUF
%39 = call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %38)
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i32
br label %42
42: ; preds = %33, %37, %29
%43 = phi i32 [ 0, %29 ], [ 0, %33 ], [ %41, %37 ]
%44 = and i32 %1, 32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %42
%47 = load i32, i32* @HDIO_DRIVE_CMD
%48 = call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %47, i32* %5)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @close(i32 %24)
br label %61
52: ; preds = %46
%53 = load i32, i32* @HDIO_DRIVE_CMD
%54 = call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %53, i32* %6)
%55 = icmp ne i32 %54, 0
%56 = zext i1 %55 to i32
br label %57
57: ; preds = %52, %42
%58 = phi i32 [ %43, %42 ], [ %56, %52 ]
%59 = call i32 @close(i32 %24)
%60 = icmp eq i32 %58, 0
br i1 %60, label %61, label %62
61: ; preds = %50, %57
br label %62
62: ; preds = %61, %57, %20, %2
%63 = phi i32 [ -1, %2 ], [ -1, %20 ], [ 0, %61 ], [ -1, %57 ]
ret i32 %63
}