#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 sg_io_hdr {
  char interface_id;
  unsigned char *cmdp;
  int cmd_len;
  unsigned char *sbp;
  int mx_sb_len;
  int masked_status;
  scalar_t__ sb_len_wr;
  int /*<<< orphan*/ dxfer_direction;
};
typedef int /*<<< orphan*/ sense_data;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int CHECK_CONDITION;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ SG_DXFER_NONE;
int /*<<< orphan*/ SG_IO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, struct sg_io_hdr *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_hex(unsigned char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct sg_io_hdr transport = {};
  unsigned char cmd[] = {0, 0, 0, 0, 0, 0};
  unsigned char sense_data[32] = {};
  int fd, rc;
  if (argc < 2) {
    fprintf(stderr, "Usage: wait-ready DEVICE\n");
    return 1;
  }
  fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  transport.interface_id = 'S';
  transport.cmdp = cmd;
  transport.cmd_len = sizeof(cmd);
  transport.sbp = sense_data;
  transport.mx_sb_len = sizeof(sense_data);
  transport.dxfer_direction = SG_DXFER_NONE;
  rc = ioctl(fd, SG_IO, &transport);
  if (rc) {
    perror("ioctl(SG_IO)");
    return 1;
  }
  if ((transport.masked_status & CHECK_CONDITION) && transport.sb_len_wr) {
    fprintf(stderr, "* check sense data:\n");
    print_hex(sense_data, sizeof(sense_data));
    fprintf(stderr, "SK=0x%02x ASC=0x%02x ASCQ=0x%02x\n", sense_data[2],
            sense_data[12], sense_data[13]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1030 <memset@plt>
lea    -0x4e(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1030 <memset@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11d6 <main+0x76>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x19e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    121a <main+0xba>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x19e>
movb   $0x53,-0x48(%rbp)
lea    -0x4e(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x6,-0x38(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x20,-0x28(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x74(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
je     1282 <main+0x122>
lea    0xdae(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x19e>
mov    -0x24(%rbp),%eax
mov    0x2d44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12f7 <main+0x197>
cmpq   $0x0,-0x20(%rbp)
je     12f7 <main+0x197>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x70(%rbp),%rdi
mov    $0x20,%esi
call   1060 <print_hex@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
movzbl -0x6e(%rbp),%edx
movzbl -0x64(%rbp),%ecx
movzbl -0x63(%rbp),%r8d
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x19e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x40(%rsp)
movw   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
cmp    $0x1,%edi
jg     11aa <main+0x5a>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    127b <main+0x12b>
mov    0x8(%rsi),%rdi
mov    0x2e2b(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1286 <main+0x136>
movb   $0x53,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %rcx,0x18(%rsp)
movl   $0x6,0x20(%rsp)
lea    0x50(%rsp),%rcx
mov    %rcx,0x28(%rsp)
movl   $0x20,0x30(%rsp)
mov    0x2db3(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x40(%rsp)
mov    0x2db6(%rip),%rcx
mov    (%rcx),%esi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
jne    1294 <main+0x144>
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
test   %ecx,0x34(%rsp)
je     1280 <main+0x130>
cmpq   $0x0,0x38(%rsp)
je     1280 <main+0x130>
mov    0x2d8c(%rip),%rbx
mov    (%rbx),%edi
lea    0xde7(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0x50(%rsp),%rdi
mov    $0x20,%esi
call   1050 <print_hex@plt>
mov    (%rbx),%edi
movzbl 0x52(%rsp),%edx
movzbl 0x5c(%rsp),%ecx
movzbl 0x5d(%rsp),%r8d
lea    0xdcd(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x70,%rsp
pop    %rbx
ret
lea    0xd8d(%rip),%rdi
call   1070 <perror@plt>
jmp    127b <main+0x12b>
lea    0xd84(%rip),%rdi
call   1070 <perror@plt>
jmp    127b <main+0x12b>

LLVM IR (-O0):
%struct.sg_io_hdr = type { i8, i8*, i32, i8*, i32, i32, i64, i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: wait-ready DEVICE\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SG_DXFER_NONE = global i32 0
@SG_IO = global i32 0
@.str.2 = [13 x i8] c"ioctl(SG_IO)\00"
@CHECK_CONDITION = global i32 0
@.str.3 = [21 x i8] c"* check sense data:\0A\00"
@.str.4 = [34 x i8] c"SK=0x%02x ASC=0x%02x ASCQ=0x%02x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sg_io_hdr
%7 = alloca [6 x i8]
%8 = alloca [32 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.sg_io_hdr* %6 to i8*
%12 = bitcast [6 x i8]* %7 to i8*
%13 = bitcast [32 x i8]* %8 to i8*
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %75
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_NONBLOCK
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %22, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %75
31: ; preds = %19
%32 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 0
store i8 83, i8* %32
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 1
store i8* %33, i8** %34
%35 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 2
store i32 6, i32* %35
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%37 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 3
store i8* %36, i8** %37
%38 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 4
store i32 32, i32* %38
%39 = load i32, i32* @SG_DXFER_NONE
%40 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 7
store i32 %39, i32* %40
%41 = load i32, i32* %9
%42 = load i32, i32* @SG_IO
%43 = call i32 @ioctl(i32 %41, i32 %42, %struct.sg_io_hdr* %6)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %31
%47 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %75
48: ; preds = %31
%49 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 5
%50 = load i32, i32* %49
%51 = load i32, i32* @CHECK_CONDITION
%52 = and i32 %50, %51
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %74
54: ; preds = %48
%55 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 6
%56 = load i64, i64* %55
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %74
58: ; preds = %54
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%62 = call i32 @print_hex(i8* %61, i32 32)
%63 = load i32, i32* @stderr
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 2
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 12
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 13
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %66, i32 %69, i32 %72)
store i32 1, i32* %3
br label %75
74: ; preds = %54, %48
store i32 0, i32* %3
br label %75
75: ; preds = %74, %58, %46, %29, %16
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.sg_io_hdr = type { i8, i8*, i32, i8*, i32, i32, i64, i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Usage: wait-ready DEVICE\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SG_DXFER_NONE = global i32 0
@SG_IO = global i32 0
@.str.2 = [13 x i8] c"ioctl(SG_IO)\00"
@CHECK_CONDITION = global i32 0
@.str.3 = [21 x i8] c"* check sense data:\0A\00"
@.str.4 = [34 x i8] c"SK=0x%02x ASC=0x%02x ASCQ=0x%02x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sg_io_hdr
%4 = alloca [6 x i8]
%5 = alloca [32 x i8]
%6 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 0
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %60
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_NONBLOCK
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %60
23: ; preds = %13
store i8 83, i8* %6
%24 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 1
store i8* %7, i8** %24
%25 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 2
store i32 6, i32* %25
%26 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 3
store i8* %8, i8** %26
%27 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 4
store i32 32, i32* %27
%28 = load i32, i32* @SG_DXFER_NONE
%29 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 7
store i32 %28, i32* %29
%30 = load i32, i32* @SG_IO
%31 = call i32 @ioctl(i32 %19, i32 %30, %struct.sg_io_hdr* %3)
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %60
35: ; preds = %23
%36 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 5
%37 = load i32, i32* %36
%38 = load i32, i32* @CHECK_CONDITION
%39 = and i32 %38, %37
%40 = icmp ne i32 %39, 0
%41 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i64 0, i32 6
%42 = load i64, i64* %41
%43 = icmp ne i64 %42, 0
%44 = select i1 %40, i1 %43, i1 false
br i1 %44, label %45, label %60
45: ; preds = %35
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @print_hex(i8* %8, i32 32)
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 2
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 12
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 13
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %52, i32 %55, i32 %58)
br label %60
60: ; preds = %35, %45, %33, %21, %10
%61 = phi i32 [ 1, %10 ], [ 1, %21 ], [ 1, %33 ], [ 1, %45 ], [ 0, %35 ]
ret i32 %61
}