#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;
  size_t dxfer_len;
  unsigned char *sbp;
  int mx_sb_len;
  int timeout;
  int info;
  int /*<<< orphan*/ dxfer_direction;
  void *dxferp;
};
typedef int /*<<< orphan*/ sense;
typedef int /*<<< orphan*/ io_hdr;
typedef int /*<<< orphan*/ inq_cmd;

/* Variables and functions */
int INQUIRY;
int /*<<< orphan*/ SG_DXFER_FROM_DEV;
int SG_INFO_OK;
int SG_INFO_OK_MASK;
int /*<<< orphan*/ SG_IO;
int ioctl(int, int /*<<< orphan*/, struct sg_io_hdr *);
int /*<<< orphan*/ memset(struct sg_io_hdr *, int /*<<< orphan*/, int);
int scsi_get_serial(int fd, void *buf, size_t buf_len) {
  unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
  unsigned char sense[32];
  struct sg_io_hdr io_hdr;
  int result;
  memset(&io_hdr, 0, sizeof(io_hdr));
  io_hdr.interface_id = 'S';
  io_hdr.cmdp = inq_cmd;
  io_hdr.cmd_len = sizeof(inq_cmd);
  io_hdr.dxferp = buf;
  io_hdr.dxfer_len = buf_len;
  io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  io_hdr.sbp = sense;
  io_hdr.mx_sb_len = sizeof(sense);
  io_hdr.timeout = 5000;
  result = ioctl(fd, SG_IO, &io_hdr);
  if (result < 0)
    return result;
  if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<scsi_get_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x1e(%rbp)
movb   $0x1,-0x1d(%rbp)
movb   $0x80,-0x1c(%rbp)
movb   $0x0,-0x1b(%rbp)
mov    -0x18(%rbp),%rax
mov    %al,-0x1a(%rbp)
movb   $0x0,-0x19(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
movb   $0x53,-0x80(%rbp)
lea    -0x1e(%rbp),%rax
mov    %rax,-0x78(%rbp)
movl   $0x6,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x20,-0x58(%rbp)
movl   $0x1388,-0x54(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    11e4 <scsi_get_serial+0xc4>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1212 <scsi_get_serial+0xf2>
mov    -0x50(%rbp),%eax
mov    0x2dda(%rip),%rcx
and    (%rcx),%eax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%eax
je     120b <scsi_get_serial+0xeb>
movl   $0x1,-0x4(%rbp)
jmp    1212 <scsi_get_serial+0xf2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_get_serial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
mov    0x2ea6(%rip),%rax
mov    (%rax),%al
mov    %al,0xa(%rsp)
movw   $0x8001,0xb(%rsp)
movb   $0x0,0xd(%rsp)
mov    %bl,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
movb   $0x53,0x10(%rsp)
lea    0xa(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x6,0x20(%rsp)
mov    %rbp,0x48(%rsp)
mov    %rbx,0x28(%rsp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x44(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x30(%rsp)
movabs $0x138800000020,%rax
mov    %rax,0x38(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
js     11e3 <scsi_get_serial+0xc3>
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
and    0x40(%rsp),%ecx
mov    0x2dfc(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
setne  %al
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sg_io_hdr = type { i8, i8*, i32, i64, i8*, i32, i32, i32, i32, i8* }
@INQUIRY = global i32 0
@SG_DXFER_FROM_DEV = global i32 0
@SG_IO = global i32 0
@SG_INFO_OK_MASK = global i32 0
@SG_INFO_OK = global i32 0
define i32 @scsi_get_serial(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [6 x i8]
%9 = alloca [32 x i8]
%10 = alloca %struct.sg_io_hdr
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* @INQUIRY
%14 = trunc i32 %13 to i8
store i8 %14, i8* %12
%15 = getelementptr inbounds i8, i8* %12, i64 1
store i8 1, i8* %15
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8 -128, i8* %16
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8 0, i8* %17
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i64, i64* %7
%20 = trunc i64 %19 to i8
store i8 %20, i8* %18
%21 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %21
%22 = call i32 @memset(%struct.sg_io_hdr* %10, i32 0, i32 64)
%23 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 0
store i8 83, i8* %23
%24 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 1
store i8* %24, i8** %25
%26 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 2
store i32 6, i32* %26
%27 = load i8*, i8** %6
%28 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 9
store i8* %27, i8** %28
%29 = load i64, i64* %7
%30 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 3
store i64 %29, i64* %30
%31 = load i32, i32* @SG_DXFER_FROM_DEV
%32 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 8
store i32 %31, i32* %32
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%34 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 4
store i8* %33, i8** %34
%35 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 5
store i32 32, i32* %35
%36 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 6
store i32 5000, i32* %36
%37 = load i32, i32* %5
%38 = load i32, i32* @SG_IO
%39 = call i32 @ioctl(i32 %37, i32 %38, %struct.sg_io_hdr* %10)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %3
%43 = load i32, i32* %11
store i32 %43, i32* %4
br label %53
44: ; preds = %3
%45 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 7
%46 = load i32, i32* %45
%47 = load i32, i32* @SG_INFO_OK_MASK
%48 = and i32 %46, %47
%49 = load i32, i32* @SG_INFO_OK
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 1, i32* %4
br label %53
52: ; preds = %44
store i32 0, i32* %4
br label %53
53: ; preds = %52, %51, %42
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
%struct.sg_io_hdr = type { i8, i8*, i32, i64, i8*, i32, i32, i32, i32, i8* }
@INQUIRY = global i32 0
@SG_DXFER_FROM_DEV = global i32 0
@SG_IO = global i32 0
@SG_INFO_OK_MASK = global i32 0
@SG_INFO_OK = global i32 0
define i32 @scsi_get_serial(i32 %0, i8* %1, i64 %2) {
%4 = alloca [6 x i8]
%5 = alloca [32 x i8]
%6 = alloca %struct.sg_io_hdr
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @INQUIRY
%9 = trunc i32 %8 to i8
store i8 %9, i8* %7
%10 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 1
store i8 1, i8* %10
%11 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 2
store i8 -128, i8* %11
%12 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 3
store i8 0, i8* %12
%13 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 4
%14 = trunc i64 %2 to i8
store i8 %14, i8* %13
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 5
store i8 0, i8* %15
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%17 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 0
%18 = call i32 @memset(%struct.sg_io_hdr* %6, i32 0, i32 64)
store i8 83, i8* %17
%19 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 1
store i8* %7, i8** %19
%20 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 2
store i32 6, i32* %20
%21 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 9
store i8* %1, i8** %21
%22 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 3
store i64 %2, i64* %22
%23 = load i32, i32* @SG_DXFER_FROM_DEV
%24 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 8
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 4
store i8* %16, i8** %25
%26 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 5
store i32 32, i32* %26
%27 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 6
store i32 5000, i32* %27
%28 = load i32, i32* @SG_IO
%29 = call i32 @ioctl(i32 %0, i32 %28, %struct.sg_io_hdr* %6)
%30 = icmp slt i32 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %3
%32 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i64 0, i32 7
%33 = load i32, i32* %32
%34 = load i32, i32* @SG_INFO_OK_MASK
%35 = and i32 %34, %33
%36 = load i32, i32* @SG_INFO_OK
%37 = icmp ne i32 %35, %36
%38 = zext i1 %37 to i32
br label %39
39: ; preds = %31, %3
%40 = phi i32 [ %29, %3 ], [ %38, %31 ]
ret i32 %40
}