#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 */
typedef int u_char;
struct sg_io_hdr {
  int dummy;
};
struct TYPE_4__ {
  char interface_id;
  int cmd_len;
  int mx_sb_len;
  int *cmdp;
  int *sbp;
  int timeout;
  int /*<<< orphan*/ dxfer_direction;
};
typedef TYPE_1__ sg_io_hdr_t;
typedef int /*<<< orphan*/ sense_buffer;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ SG_DXFER_NONE;
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 scsi_reserve(int fd) {
  sg_io_hdr_t io_hdr;
  u_char sense_buffer[32];
  u_char cmd[6] = {0x16, 0, 0, 0, 0, 0};
  memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
  io_hdr.interface_id = 'S';
  io_hdr.cmd_len = sizeof(cmd);
  io_hdr.mx_sb_len = sizeof(sense_buffer);
  io_hdr.dxfer_direction = SG_DXFER_NONE;
  io_hdr.cmdp = cmd;
  io_hdr.sbp = sense_buffer;
  io_hdr.timeout = 5000;
  if (ioctl(fd, SG_IO, &io_hdr) < 0) {
    perror("ioctl");
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<scsi_reserve>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
mov    0xebb(%rip),%rax
mov    %rax,-0xd0(%rbp)
mov    0xeb5(%rip),%rax
mov    %rax,-0xc8(%rbp)
mov    0xeaf(%rip),%rax
mov    %rax,-0xc0(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movb   $0x53,-0x30(%rbp)
movl   $0x18,-0x2c(%rbp)
movl   $0x80,-0x28(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0xd0(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1388,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ea <scsi_reserve+0xba>
lea    0xe3f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <scsi_reserve+0xc1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_reserve>:
push   %rbp
push   %r14
push   %rbx
sub    $0xd0,%rsp
mov    %edi,%ebp
mov    0xecc(%rip),%rax
mov    %rax,0x40(%rsp)
movaps 0xeb0(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
lea    0x8(%rsp),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movb   $0x53,0x8(%rsp)
movabs $0x8000000018,%rax
mov    %rax,0xc(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x18(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x1388,0x28(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     11ce <scsi_reserve+0x9e>
mov    %ebx,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11c0 <scsi_reserve+0x90>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i8, i32, i32, i32*, i32*, i32, i32 }
@__const.scsi_reserve.cmd = [6 x i32] [i32 22, i32 0, i32 0, i32 0, i32 0, i32 0]
@SG_DXFER_NONE = global i32 0
@SG_IO = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i32 @scsi_reserve(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.TYPE_4__
%5 = alloca [32 x i32]
%6 = alloca [6 x i32]
store i32 %0, i32* %3
%7 = bitcast [6 x i32]* %6 to i8*
%8 = call i32 @memset(%struct.TYPE_4__* %4, i32 0, i32 4)
%9 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
store i8 83, i8* %9
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 1
store i32 24, i32* %10
%11 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 2
store i32 128, i32* %11
%12 = load i32, i32* @SG_DXFER_NONE
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 6
store i32 %12, i32* %13
%14 = getelementptr inbounds [6 x i32], [6 x i32]* %6, i64 0, i64 0
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 3
store i32* %14, i32** %15
%16 = getelementptr inbounds [32 x i32], [32 x i32]* %5, i64 0, i64 0
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 4
store i32* %16, i32** %17
%18 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 5
store i32 5000, i32* %18
%19 = load i32, i32* %3
%20 = load i32, i32* @SG_IO
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.TYPE_4__* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %26
25: ; preds = %1
store i32 0, i32* %2
br label %26
26: ; preds = %25, %23
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i8, i32, i32, i32*, i32*, i32, i32 }
@__const.scsi_reserve.cmd = [6 x i32] [i32 22, i32 0, i32 0, i32 0, i32 0, i32 0]
@SG_DXFER_NONE = global i32 0
@SG_IO = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i32 @scsi_reserve(i32 %0) {
%2 = alloca %struct.TYPE_4__
%3 = alloca [32 x i32]
%4 = alloca [6 x i32]
%5 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%6 = bitcast [32 x i32]* %3 to i8*
%7 = bitcast [6 x i32]* %4 to i8*
%8 = call i32 @memset(%struct.TYPE_4__* %2, i32 0, i32 4)
store i8 83, i8* %5
%9 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 1
store i32 24, i32* %9
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 2
store i32 128, i32* %10
%11 = load i32, i32* @SG_DXFER_NONE
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 6
store i32 %11, i32* %12
%13 = getelementptr inbounds [6 x i32], [6 x i32]* %4, i64 0, i64 0
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 3
store i32* %13, i32** %14
%15 = getelementptr inbounds [32 x i32], [32 x i32]* %3, i64 0, i64 0
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 4
store i32* %15, i32** %16
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 5
store i32 5000, i32* %17
%18 = load i32, i32* @SG_IO
%19 = call i64 @ioctl(i32 %0, i32 %18, %struct.TYPE_4__* %2)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %1, %21
%24 = phi i32 [ -1, %21 ], [ 0, %1 ]
ret i32 %24
}