#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 */
scalar_t__ EIO;
int /*<<< orphan*/ HDIO_DRIVE_CMD;
unsigned char WIN_CHECKPOWERMODE1;
unsigned char WIN_CHECKPOWERMODE2;
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char **);
int check_powermode(int fd) {
  unsigned char args[4] = {WIN_CHECKPOWERMODE1, 0, 0, 0};
  int state;
  if (ioctl(fd, HDIO_DRIVE_CMD, &args) && (args[0] = WIN_CHECKPOWERMODE2) &&
      ioctl(fd, HDIO_DRIVE_CMD, &args)) {
    if (errno != EIO || args[0] != 0 || args[1] != 0) {
      state = -1;
    } else {
      state = 0;
    }
  } else {
    state = (args[2] == 255) ? 1 : 0;
  };
  return state;
}


x86 assembly (clang, -O0):
<check_powermode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ebe(%rip),%rax
mov    (%rax),%al
mov    %al,-0x8(%rbp)
movb   $0x0,-0x7(%rbp)
movb   $0x0,-0x6(%rbp)
movb   $0x0,-0x5(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11d5 <check_powermode+0xc5>
mov    0x2e5f(%rip),%rax
mov    (%rax),%al
mov    %al,-0x8(%rbp)
movzbl %al,%eax
cmp    $0x0,%eax
je     11d5 <check_powermode+0xc5>
mov    -0x4(%rbp),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11d5 <check_powermode+0xc5>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jne    11bd <check_powermode+0xad>
movzbl -0x8(%rbp),%eax
cmp    $0x0,%eax
jne    11bd <check_powermode+0xad>
movzbl -0x7(%rbp),%eax
cmp    $0x0,%eax
je     11c9 <check_powermode+0xb9>
movl   $0xffffffff,-0xc(%rbp)
jmp    11d0 <check_powermode+0xc0>
movl   $0x0,-0xc(%rbp)
jmp    11ec <check_powermode+0xdc>
movzbl -0x6(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0xff,%edx
cmove  %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_powermode>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ec3(%rip),%rax
mov    (%rax),%al
mov    %al,0x4(%rsp)
movw   $0x0,0x5(%rsp)
movb   $0x0,0x7(%rsp)
mov    0x2eb2(%rip),%r14
mov    (%r14),%esi
lea    0x4(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     119f <check_powermode+0x8f>
mov    0x2e69(%rip),%rax
mov    (%rax),%al
mov    %al,0x4(%rsp)
test   %al,%al
je     119f <check_powermode+0x8f>
mov    (%r14),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     119f <check_powermode+0x8f>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
cmpb   $0x0,0x4(%rsp)
setne  %cl
cmpb   $0x0,0x5(%rsp)
setne  %dl
or     %cl,%dl
or     %al,%dl
movzbl %dl,%eax
neg    %eax
jmp    11a9 <check_powermode+0x99>
xor    %eax,%eax
cmpb   $0xff,0x6(%rsp)
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@WIN_CHECKPOWERMODE1 = global i8 0
@HDIO_DRIVE_CMD = global i32 0
@WIN_CHECKPOWERMODE2 = global i8 0
@errno = global i64 0
@EIO = global i64 0
define i32 @check_powermode(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%6 = load i8, i8* @WIN_CHECKPOWERMODE1
store i8 %6, i8* %5
%7 = getelementptr inbounds i8, i8* %5, i64 1
store i8 0, i8* %7
%8 = getelementptr inbounds i8, i8* %7, i64 1
store i8 0, i8* %8
%9 = getelementptr inbounds i8, i8* %8, i64 1
store i8 0, i8* %9
%10 = load i32, i32* %2
%11 = load i32, i32* @HDIO_DRIVE_CMD
%12 = bitcast [4 x i8]* %3 to i8**
%13 = call i64 @ioctl(i32 %10, i32 %11, i8** %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %1
%16 = load i8, i8* @WIN_CHECKPOWERMODE2
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
store i8 %16, i8* %17
%18 = zext i8 %16 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %15
%21 = load i32, i32* %2
%22 = load i32, i32* @HDIO_DRIVE_CMD
%23 = bitcast [4 x i8]* %3 to i8**
%24 = call i64 @ioctl(i32 %21, i32 %22, i8** %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %43
26: ; preds = %20
%27 = load i64, i64* @errno
%28 = load i64, i64* @EIO
%29 = icmp ne i64 %27, %28
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35, %30, %26
store i32 -1, i32* %4
br label %42
41: ; preds = %35
store i32 0, i32* %4
br label %42
42: ; preds = %41, %40
br label %50
43: ; preds = %20, %15, %1
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = icmp eq i32 %46, 255
%48 = zext i1 %47 to i64
%49 = select i1 %47, i32 1, i32 0
store i32 %49, i32* %4
br label %50
50: ; preds = %43, %42
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@WIN_CHECKPOWERMODE1 = global i8 0
@HDIO_DRIVE_CMD = global i32 0
@WIN_CHECKPOWERMODE2 = global i8 0
@errno = global i64 0
@EIO = global i64 0
define i32 @check_powermode(i32 %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%4 = load i8, i8* @WIN_CHECKPOWERMODE1
store i8 %4, i8* %3
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
store i8 0, i8* %5
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 2
store i8 0, i8* %6
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 3
store i8 0, i8* %7
%8 = load i32, i32* @HDIO_DRIVE_CMD
%9 = bitcast [4 x i8]* %2 to i8**
%10 = call i64 @ioctl(i32 %0, i32 %8, i8** %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %1
%13 = load i8, i8* @WIN_CHECKPOWERMODE2
store i8 %13, i8* %3
%14 = icmp eq i8 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %12
%16 = load i32, i32* @HDIO_DRIVE_CMD
%17 = call i64 @ioctl(i32 %0, i32 %16, i8** %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EIO
%22 = icmp ne i64 %20, %21
%23 = load i8, i8* %3
%24 = icmp ne i8 %23, 0
%25 = select i1 %22, i1 true, i1 %24
%26 = load i8, i8* %5
%27 = icmp ne i8 %26, 0
%28 = select i1 %25, i1 true, i1 %27
%29 = sext i1 %28 to i32
br label %34
30: ; preds = %15, %12, %1
%31 = load i8, i8* %6
%32 = icmp eq i8 %31, -1
%33 = zext i1 %32 to i32
br label %34
34: ; preds = %19, %30
%35 = phi i32 [ %33, %30 ], [ %29, %19 ]
ret i32 %35
}