#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  char interface_id;
  unsigned char *cmdp;
  unsigned char cmd_len;
  unsigned char *sbp;
  unsigned int mx_sb_len;
  int dxfer_direction;
  unsigned char *dxferp;
  unsigned int dxfer_len;
  int timeout;
  int info;
  unsigned int sb_len_wr;
  int masked_status;
  int status;
  int host_status;
  int driver_status;
};
typedef TYPE_1__ sg_io_hdr_t;

/* Variables and functions */
int SG_INFO_OK;
int SG_INFO_OK_MASK;
int /*<<< orphan*/ SG_IO;
scalar_t__ ioctl(int, int /*<<< orphan*/, TYPE_1__ *);
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int scsi_io(int fd, unsigned char *cdb, unsigned char cdb_size, int xfer_dir,
            unsigned char *data, unsigned int *data_size, unsigned char *sense,
            unsigned int *sense_len) {
  sg_io_hdr_t io_hdr;
  memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  io_hdr.interface_id = 'S';
  io_hdr.cmdp = cdb;
  io_hdr.cmd_len = cdb_size;
  io_hdr.sbp = sense;
  io_hdr.mx_sb_len = *sense_len;
  *sense_len = 0;
  io_hdr.dxfer_direction = xfer_dir;
  io_hdr.dxferp = data;
  io_hdr.dxfer_len = *data_size;
  io_hdr.timeout = 5000;
  if (ioctl(fd, SG_IO, &io_hdr) < 0) {
    perror("SG_IO ioctl failed");
    return -1;
  }
  if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
    if (io_hdr.sb_len_wr > 0) {
      *sense_len = io_hdr.sb_len_wr;
      return 0;
    }
  }
  if (io_hdr.masked_status) {
    printf("status=0x%x\n", io_hdr.status);
    printf("masked_status=0x%x\n", io_hdr.masked_status);
    return -2;
  }
  if (io_hdr.host_status) {
    printf("host_status=0x%x\n", io_hdr.host_status);
    return -3;
  }
  if (io_hdr.driver_status) {
    printf("driver_status=0x%x\n", io_hdr.driver_status);
    return -4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<scsi_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %dl,%al
mov    0x18(%rbp),%rdx
mov    0x10(%rbp),%rdx
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1040 <memset@plt>
movb   $0x53,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x11(%rbp),%al
mov    %al,-0x68(%rbp)
mov    0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
movl   $0x1388,-0x44(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11fc <scsi_io+0xbc>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <scsi_io+0x180>
mov    -0x40(%rbp),%eax
mov    0x2dd2(%rip),%rcx
and    (%rcx),%eax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%eax
je     123b <scsi_io+0xfb>
cmpl   $0x0,-0x3c(%rbp)
jbe    1236 <scsi_io+0xf6>
mov    -0x3c(%rbp),%ecx
mov    0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12c0 <scsi_io+0x180>
jmp    123b <scsi_io+0xfb>
cmpl   $0x0,-0x38(%rbp)
je     126f <scsi_io+0x12f>
mov    -0x34(%rbp),%esi
lea    0xdcb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x38(%rbp),%esi
lea    0xdb5(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12c0 <scsi_io+0x180>
cmpl   $0x0,-0x30(%rbp)
je     1294 <scsi_io+0x154>
mov    -0x30(%rbp),%esi
lea    0xda4(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12c0 <scsi_io+0x180>
cmpl   $0x0,-0x2c(%rbp)
je     12b9 <scsi_io+0x179>
mov    -0x2c(%rbp),%esi
lea    0xd91(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12c0 <scsi_io+0x180>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_io>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r13d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1040 <memset@plt>
movb   $0x53,0x8(%rsp)
mov    %rbp,0x10(%rsp)
mov    %bl,0x18(%rsp)
mov    0x98(%rsp),%rbx
mov    0x90(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    (%rbx),%eax
mov    %eax,0x28(%rsp)
movl   $0x0,(%rbx)
mov    %r12d,0x2c(%rsp)
mov    %r15,0x30(%rsp)
mov    (%r14),%eax
mov    %eax,0x38(%rsp)
movl   $0x1388,0x3c(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
lea    0x8(%rsp),%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1261 <scsi_io+0x121>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
and    0x40(%rsp),%eax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%eax
je     11fd <scsi_io+0xbd>
mov    0x44(%rsp),%eax
test   %eax,%eax
je     11fd <scsi_io+0xbd>
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1272 <scsi_io+0x132>
cmpl   $0x0,0x48(%rsp)
je     122b <scsi_io+0xeb>
mov    0x4c(%rsp),%esi
lea    0xe0b(%rip),%rdi
call   1030 <printf@plt>
mov    0x48(%rsp),%esi
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
mov    $0xfffffffe,%eax
jmp    1272 <scsi_io+0x132>
mov    0x50(%rsp),%esi
test   %esi,%esi
je     1246 <scsi_io+0x106>
lea    0xded(%rip),%rdi
call   1030 <printf@plt>
mov    $0xfffffffd,%eax
jmp    1272 <scsi_io+0x132>
mov    0x54(%rsp),%esi
test   %esi,%esi
je     11f9 <scsi_io+0xb9>
lea    0xde4(%rip),%rdi
call   1030 <printf@plt>
mov    $0xfffffffc,%eax
jmp    1272 <scsi_io+0x132>
lea    0xd98(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i8, i8*, i8, i8*, i32, i32, i8*, i32, i32, i32, i32, i32, i32, i32, i32 }
@SG_IO = global i32 0
@.str = [19 x i8] c"SG_IO ioctl failed\00"
@SG_INFO_OK_MASK = global i32 0
@SG_INFO_OK = global i32 0
@.str.1 = [13 x i8] c"status=0x%x\0A\00"
@.str.2 = [20 x i8] c"masked_status=0x%x\0A\00"
@.str.3 = [18 x i8] c"host_status=0x%x\0A\00"
@.str.4 = [20 x i8] c"driver_status=0x%x\0A\00"
define i32 @scsi_io(i32 %0, i8* %1, i8 zeroext %2, i32 %3, i8* %4, i32* %5, i8* %6, i32* %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32*
%16 = alloca i8*
%17 = alloca i32*
%18 = alloca %struct.TYPE_4__
store i32 %0, i32* %10
store i8* %1, i8** %11
store i8 %2, i8* %12
store i32 %3, i32* %13
store i8* %4, i8** %14
store i32* %5, i32** %15
store i8* %6, i8** %16
store i32* %7, i32** %17
%19 = call i32 @memset(%struct.TYPE_4__* %18, i32 0, i32 80)
%20 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 0
store i8 83, i8* %20
%21 = load i8*, i8** %11
%22 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 1
store i8* %21, i8** %22
%23 = load i8, i8* %12
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 2
store i8 %23, i8* %24
%25 = load i8*, i8** %16
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 3
store i8* %25, i8** %26
%27 = load i32*, i32** %17
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 4
store i32 %28, i32* %29
%30 = load i32*, i32** %17
store i32 0, i32* %30
%31 = load i32, i32* %13
%32 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 5
store i32 %31, i32* %32
%33 = load i8*, i8** %14
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 6
store i8* %33, i8** %34
%35 = load i32*, i32** %15
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 7
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 8
store i32 5000, i32* %38
%39 = load i32, i32* %10
%40 = load i32, i32* @SG_IO
%41 = call i64 @ioctl(i32 %39, i32 %40, %struct.TYPE_4__* %18)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %8
%44 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %9
br label %89
45: ; preds = %8
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 9
%47 = load i32, i32* %46
%48 = load i32, i32* @SG_INFO_OK_MASK
%49 = and i32 %47, %48
%50 = load i32, i32* @SG_INFO_OK
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %61
52: ; preds = %45
%53 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 10
%54 = load i32, i32* %53
%55 = icmp ugt i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %52
%57 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 10
%58 = load i32, i32* %57
%59 = load i32*, i32** %17
store i32 %58, i32* %59
store i32 0, i32* %9
br label %89
60: ; preds = %52
br label %61
61: ; preds = %60, %45
%62 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 11
%63 = load i32, i32* %62
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %61
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 12
%67 = load i32, i32* %66
%68 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %67)
%69 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 11
%70 = load i32, i32* %69
%71 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %70)
store i32 -2, i32* %9
br label %89
72: ; preds = %61
%73 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 13
%74 = load i32, i32* %73
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %72
%77 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 13
%78 = load i32, i32* %77
%79 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %78)
store i32 -3, i32* %9
br label %89
80: ; preds = %72
%81 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 14
%82 = load i32, i32* %81
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %88
84: ; preds = %80
%85 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 14
%86 = load i32, i32* %85
%87 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %86)
store i32 -4, i32* %9
br label %89
88: ; preds = %80
store i32 0, i32* %9
br label %89
89: ; preds = %88, %84, %76, %65, %56, %43
%90 = load i32, i32* %9
ret i32 %90
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i8, i8*, i8, i8*, i32, i32, i8*, i32, i32, i32, i32, i32, i32, i32, i32 }
@SG_IO = global i32 0
@.str = [19 x i8] c"SG_IO ioctl failed\00"
@SG_INFO_OK_MASK = global i32 0
@SG_INFO_OK = global i32 0
@.str.1 = [13 x i8] c"status=0x%x\0A\00"
@.str.2 = [20 x i8] c"masked_status=0x%x\0A\00"
@.str.3 = [18 x i8] c"host_status=0x%x\0A\00"
@.str.4 = [20 x i8] c"driver_status=0x%x\0A\00"
define i32 @scsi_io(i32 %0, i8* %1, i8 zeroext %2, i32 %3, i8* %4, i32* nocapture %5, i8* %6, i32* nocapture %7) {
%9 = alloca %struct.TYPE_4__
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 0
%11 = call i32 @memset(%struct.TYPE_4__* %9, i32 0, i32 80)
store i8 83, i8* %10
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 1
store i8* %1, i8** %12
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 2
store i8 %2, i8* %13
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 3
store i8* %6, i8** %14
%15 = load i32, i32* %7
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 4
store i32 %15, i32* %16
store i32 0, i32* %7
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 5
store i32 %3, i32* %17
%18 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 6
store i8* %4, i8** %18
%19 = load i32, i32* %5
%20 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 7
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 8
store i32 5000, i32* %21
%22 = load i32, i32* @SG_IO
%23 = call i64 @ioctl(i32 %0, i32 %22, %struct.TYPE_4__* %9)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %8
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %61
27: ; preds = %8
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 9
%29 = load i32, i32* %28
%30 = load i32, i32* @SG_INFO_OK_MASK
%31 = and i32 %30, %29
%32 = load i32, i32* @SG_INFO_OK
%33 = icmp ne i32 %31, %32
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 10
%35 = load i32, i32* %34
%36 = icmp ne i32 %35, 0
%37 = select i1 %33, i1 %36, i1 false
br i1 %37, label %38, label %39
38: ; preds = %27
store i32 %35, i32* %7
br label %61
39: ; preds = %27
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 11
%41 = load i32, i32* %40
%42 = icmp eq i32 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %39
%44 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 12
%45 = load i32, i32* %44
%46 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %40
%48 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %61
49: ; preds = %39
%50 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 13
%51 = load i32, i32* %50
%52 = icmp eq i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %49
%54 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %51)
br label %61
55: ; preds = %49
%56 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 14
%57 = load i32, i32* %56
%58 = icmp eq i32 %57, 0
br i1 %58, label %61, label %59
59: ; preds = %55
%60 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %57)
br label %61
61: ; preds = %55, %59, %53, %43, %38, %25
%62 = phi i32 [ -1, %25 ], [ 0, %38 ], [ -2, %43 ], [ -3, %53 ], [ -4, %59 ], [ 0, %55 ]
ret i32 %62
}