#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 int dxfer_len;
  unsigned char *cmdp;
  unsigned int cmd_len;
  int flags;
  unsigned char *sbp;
  int timeout;
  int mx_sb_len;
  int dxfer_direction;
  scalar_t__ masked_status;
  void *dxferp;
};
typedef TYPE_1__ sg_io_hdr_t;
typedef int /*<<< orphan*/ hdr;

/* Variables and functions */
int /*<<< orphan*/ SG_IO;
scalar_t__ ioctl(int, int /*<<< orphan*/, TYPE_1__ *);
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ scsi_transient_error(void *);
int /*<<< orphan*/ sleep(int);
int scsi_command(int fd, int direction, const void *command,
                 unsigned command_length, void *data, unsigned data_length,
                 void *requestsense, int timeout) {
  for (int retries = 0; retries < 5; ++retries) {
    sg_io_hdr_t hdr;
    memset(&hdr, 0, sizeof(hdr));
    hdr.interface_id = 'S';
    if (!timeout)
      timeout = 30000;
    hdr.dxferp = data;
    hdr.dxfer_len = data_length;
    hdr.cmdp = (unsigned char *)command;
    hdr.cmd_len = command_length;
    if (!data)
      hdr.flags |= 4;
    hdr.sbp = (unsigned char *)requestsense;
    hdr.timeout = timeout;
    if (requestsense)
      hdr.mx_sb_len = 20;
    hdr.dxfer_direction = direction;
    if (ioctl(fd, SG_IO, &hdr)) {
      perror("SG_IO");
      return 1;
    }
    if (hdr.masked_status) {
      if (scsi_transient_error(requestsense)) {
        sleep(3);
        continue;
      }
      return 2;
    }
    return 0;
  }
  return 2;
}


x86 assembly (clang, -O0):
<scsi_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x5,-0x30(%rbp)
jge    128a <scsi_command+0x13a>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
movb   $0x53,-0x70(%rbp)
cmpl   $0x0,0x18(%rbp)
jne    11aa <scsi_command+0x5a>
movl   $0x7530,0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x60(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11da <scsi_command+0x8a>
mov    -0x5c(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x5c(%rbp)
mov    0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x18(%rbp),%eax
mov    %eax,-0x50(%rbp)
cmpq   $0x0,0x10(%rbp)
je     11fa <scsi_command+0xaa>
movl   $0x14,-0x4c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    -0x70(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1237 <scsi_command+0xe7>
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1291 <scsi_command+0x141>
cmpq   $0x0,-0x40(%rbp)
je     1270 <scsi_command+0x120>
mov    0x10(%rbp),%rdi
call   1050 <scsi_transient_error@plt>
cmp    $0x0,%rax
je     1264 <scsi_command+0x114>
mov    $0x3,%edi
call   1070 <sleep@plt>
jmp    127c <scsi_command+0x12c>
movl   $0x2,-0x4(%rbp)
jmp    1291 <scsi_command+0x141>
movl   $0x0,-0x4(%rbp)
jmp    1291 <scsi_command+0x141>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    117b <scsi_command+0x2b>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9d,0xc(%rsp)
mov    %r8,%rbx
mov    %ecx,0x8(%rsp)
mov    %rdx,0x10(%rsp)
mov    %esi,0x4(%rsp)
mov    %edi,%ebp
mov    0x98(%rsp),%r12d
mov    0x90(%rsp),%r14
mov    $0x5,%r13d
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
test   %r12d,%r12d
mov    $0x7530,%eax
cmove  %eax,%r12d
movb   $0x53,0x18(%rsp)
mov    %rbx,0x50(%rsp)
mov    0xc(%rsp),%eax
mov    %eax,0x1c(%rsp)
mov    0x10(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x28(%rsp)
test   %rbx,%rbx
jne    11d9 <scsi_command+0x89>
orb    $0x4,0x2c(%rsp)
mov    %r14,0x30(%rsp)
mov    %r12d,0x38(%rsp)
test   %r14,%r14
je     11f0 <scsi_command+0xa0>
movl   $0x14,0x3c(%rsp)
mov    0x4(%rsp),%eax
mov    %eax,0x40(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1251 <scsi_command+0x101>
cmpq   $0x0,0x48(%rsp)
je     1240 <scsi_command+0xf0>
mov    %r14,%rdi
call   1050 <scsi_transient_error@plt>
test   %rax,%rax
je     1239 <scsi_command+0xe9>
mov    $0x3,%edi
call   1070 <sleep@plt>
add    $0xffffffff,%r13d
jne    1190 <scsi_command+0x40>
mov    $0x2,%eax
jmp    1242 <scsi_command+0xf2>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda8(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    1242 <scsi_command+0xf2>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i8, i32, i8*, i32, i32, i8*, i32, i32, i32, i64, i8* }
@SG_IO = global i32 0
@.str = [6 x i8] c"SG_IO\00"
define i32 @scsi_command(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4, i32 %5, i8* %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
%19 = alloca %struct.TYPE_4__
store i32 %0, i32* %10
store i32 %1, i32* %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
store i32 0, i32* %18
br label %20
20: ; preds = %74, %8
%21 = load i32, i32* %18
%22 = icmp slt i32 %21, 5
br i1 %22, label %23, label %77
23: ; preds = %20
%24 = call i32 @memset(%struct.TYPE_4__* %19, i32 0, i32 64)
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 0
store i8 83, i8* %25
%26 = load i32, i32* %17
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %23
store i32 30000, i32* %17
br label %29
29: ; preds = %28, %23
%30 = load i8*, i8** %14
%31 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 10
store i8* %30, i8** %31
%32 = load i32, i32* %15
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i8*, i8** %12
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 2
store i8* %34, i8** %35
%36 = load i32, i32* %13
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 3
store i32 %36, i32* %37
%38 = load i8*, i8** %14
%39 = icmp ne i8* %38, null
br i1 %39, label %44, label %40
40: ; preds = %29
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 4
%42 = load i32, i32* %41
%43 = or i32 %42, 4
store i32 %43, i32* %41
br label %44
44: ; preds = %40, %29
%45 = load i8*, i8** %16
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 5
store i8* %45, i8** %46
%47 = load i32, i32* %17
%48 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 6
store i32 %47, i32* %48
%49 = load i8*, i8** %16
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %53
51: ; preds = %44
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 7
store i32 20, i32* %52
br label %53
53: ; preds = %51, %44
%54 = load i32, i32* %11
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 8
store i32 %54, i32* %55
%56 = load i32, i32* %10
%57 = load i32, i32* @SG_IO
%58 = call i64 @ioctl(i32 %56, i32 %57, %struct.TYPE_4__* %19)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %9
br label %78
62: ; preds = %53
%63 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 9
%64 = load i64, i64* %63
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %62
%67 = load i8*, i8** %16
%68 = call i64 @scsi_transient_error(i8* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @sleep(i32 3)
br label %74
72: ; preds = %66
store i32 2, i32* %9
br label %78
73: ; preds = %62
store i32 0, i32* %9
br label %78
74: ; preds = %70
%75 = load i32, i32* %18
%76 = add nsw i32 %75, 1
store i32 %76, i32* %18
br label %20
77: ; preds = %20
store i32 2, i32* %9
br label %78
78: ; preds = %77, %73, %72, %60
%79 = load i32, i32* %9
ret i32 %79
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i8, i32, i8*, i32, i32, i8*, i32, i32, i32, i64, i8* }
@SG_IO = global i32 0
@.str = [6 x i8] c"SG_IO\00"
define i32 @scsi_command(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4, i32 %5, i8* %6, i32 %7) {
%9 = alloca %struct.TYPE_4__
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 0
%11 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 10
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 2
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 3
%15 = icmp eq i8* %4, null
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 4
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 5
%18 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 6
%19 = icmp eq i8* %6, null
%20 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 7
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 8
%22 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 9
br label %23
23: ; preds = %8, %48
%24 = phi i32 [ 0, %8 ], [ %50, %48 ]
%25 = phi i32 [ %7, %8 ], [ %28, %48 ]
%26 = call i32 @memset(%struct.TYPE_4__* %9, i32 0, i32 64)
store i8 83, i8* %10
%27 = icmp eq i32 %25, 0
%28 = select i1 %27, i32 30000, i32 %25
store i8* %4, i8** %11
store i32 %5, i32* %12
store i8* %2, i8** %13
store i32 %3, i32* %14
br i1 %15, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %16
%31 = or i32 %30, 4
store i32 %31, i32* %16
br label %32
32: ; preds = %29, %23
store i8* %6, i8** %17
store i32 %28, i32* %18
br i1 %19, label %34, label %33
33: ; preds = %32
store i32 20, i32* %20
br label %34
34: ; preds = %33, %32
store i32 %1, i32* %21
%35 = load i32, i32* @SG_IO
%36 = call i64 @ioctl(i32 %0, i32 %35, %struct.TYPE_4__* %9)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %46
40: ; preds = %34
%41 = load i64, i64* %22
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %40
%44 = call i64 @scsi_transient_error(i8* %6)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43, %40, %38
%47 = phi i32 [ 1, %38 ], [ 2, %43 ], [ 0, %40 ]
br label %52
48: ; preds = %43
%49 = call i32 @sleep(i32 3)
%50 = add nuw nsw i32 %24, 1
%51 = icmp eq i32 %50, 5
br i1 %51, label %52, label %23
52: ; preds = %48, %46
%53 = phi i32 [ %47, %46 ], [ 2, %48 ]
ret i32 %53
}