#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 *dxferp;
  int dxfer_len;
  unsigned char *sbp;
  int mx_sb_len;
  int timeout;
  int info;
  int /*<<< orphan*/ dxfer_direction;
};
typedef int /*<<< orphan*/ sense;
typedef int /*<<< orphan*/ rsp_buf;
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*/ memcpy(void *, unsigned char *, unsigned int);
int /*<<< orphan*/ memset(struct sg_io_hdr *, int /*<<< orphan*/, int);
int scsi_get_serial(int fd, void *buf, size_t buf_len) {
  unsigned char rsp_buf[255];
  unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, sizeof(rsp_buf), 0};
  unsigned char sense[32];
  struct sg_io_hdr io_hdr;
  int rv;
  unsigned int rsp_len;
  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 = rsp_buf;
  io_hdr.dxfer_len = sizeof(rsp_buf);
  io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  io_hdr.sbp = sense;
  io_hdr.mx_sb_len = sizeof(sense);
  io_hdr.timeout = 5000;
  rv = ioctl(fd, SG_IO, &io_hdr);
  if (rv)
    return rv;
  if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
    return -1;
  rsp_len = rsp_buf[3];
  if (!rsp_len || buf_len < rsp_len)
    return -1;
  memcpy(buf, &rsp_buf[4], rsp_len);
  return 0;
}


x86 assembly (clang, -O0):
<scsi_get_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x126(%rbp)
movb   $0x1,-0x125(%rbp)
movb   $0x80,-0x124(%rbp)
movb   $0x0,-0x123(%rbp)
movb   $0xff,-0x122(%rbp)
movb   $0x0,-0x121(%rbp)
lea    -0x190(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
movb   $0x53,-0x190(%rbp)
lea    -0x126(%rbp),%rax
mov    %rax,-0x188(%rbp)
movl   $0x6,-0x180(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x178(%rbp)
movl   $0xff,-0x170(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x154(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x168(%rbp)
movl   $0x20,-0x160(%rbp)
movl   $0x1388,-0x15c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    -0x190(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x194(%rbp)
cmpl   $0x0,-0x194(%rbp)
je     122c <scsi_get_serial+0xfc>
mov    -0x194(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b0 <scsi_get_serial+0x180>
mov    -0x158(%rbp),%eax
mov    0x2d8f(%rip),%rcx
and    (%rcx),%eax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%eax
je     1256 <scsi_get_serial+0x126>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <scsi_get_serial+0x180>
movzbl -0x11d(%rbp),%eax
mov    %eax,-0x198(%rbp)
cmpl   $0x0,-0x198(%rbp)
je     1283 <scsi_get_serial+0x153>
mov    -0x18(%rbp),%rax
mov    -0x198(%rbp),%ecx
cmp    %rcx,%rax
jae    128f <scsi_get_serial+0x15f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <scsi_get_serial+0x180>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
add    $0x4,%rsi
mov    -0x198(%rbp),%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_get_serial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x178,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e94(%rip),%rax
mov    (%rax),%al
mov    %al,0xa(%rsp)
movl   $0xff008001,0xb(%rsp)
ff
movb   $0x0,0xf(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%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)
lea    0x70(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0xff,0x30(%rsp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4c(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x38(%rsp)
movabs $0x138800000020,%rax
mov    %rax,0x40(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
jne    1214 <scsi_get_serial+0xe4>
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
and    0x48(%rsp),%ecx
mov    0x2deb(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    1214 <scsi_get_serial+0xe4>
movzbl 0x73(%rsp),%edx
test   %rdx,%rdx
je     1214 <scsi_get_serial+0xe4>
cmp    %r15,%rdx
ja     1214 <scsi_get_serial+0xe4>
lea    0x74(%rsp),%rsi
mov    %r14,%rdi
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x178,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sg_io_hdr = type { i8, i8*, i32, i8*, i32, i8*, i32, i32, i32, i32 }
@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 [255 x i8]
%9 = alloca [6 x i8]
%10 = alloca [32 x i8]
%11 = alloca %struct.sg_io_hdr
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* @INQUIRY
%16 = trunc i32 %15 to i8
store i8 %16, i8* %14
%17 = getelementptr inbounds i8, i8* %14, i64 1
store i8 1, i8* %17
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8 -128, i8* %18
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %19
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8 -1, i8* %20
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8 0, i8* %21
%22 = call i32 @memset(%struct.sg_io_hdr* %11, i32 0, i32 64)
%23 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 0
store i8 83, i8* %23
%24 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%25 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 1
store i8* %24, i8** %25
%26 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 2
store i32 6, i32* %26
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%28 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 3
store i8* %27, i8** %28
%29 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 4
store i32 255, i32* %29
%30 = load i32, i32* @SG_DXFER_FROM_DEV
%31 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 9
store i32 %30, i32* %31
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%33 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 5
store i8* %32, i8** %33
%34 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 6
store i32 32, i32* %34
%35 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 7
store i32 5000, i32* %35
%36 = load i32, i32* %5
%37 = load i32, i32* @SG_IO
%38 = call i32 @ioctl(i32 %36, i32 %37, %struct.sg_io_hdr* %11)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %3
%42 = load i32, i32* %12
store i32 %42, i32* %4
br label %68
43: ; preds = %3
%44 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %11, i32 0, i32 8
%45 = load i32, i32* %44
%46 = load i32, i32* @SG_INFO_OK_MASK
%47 = and i32 %45, %46
%48 = load i32, i32* @SG_INFO_OK
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %43
store i32 -1, i32* %4
br label %68
51: ; preds = %43
%52 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 3
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
store i32 %54, i32* %13
%55 = load i32, i32* %13
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %51
%58 = load i64, i64* %7
%59 = load i32, i32* %13
%60 = zext i32 %59 to i64
%61 = icmp ult i64 %58, %60
br i1 %61, label %62, label %63
62: ; preds = %57, %51
store i32 -1, i32* %4
br label %68
63: ; preds = %57
%64 = load i8*, i8** %6
%65 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 4
%66 = load i32, i32* %13
%67 = call i32 @memcpy(i8* %64, i8* %65, i32 %66)
store i32 0, i32* %4
br label %68
68: ; preds = %63, %62, %50, %41
%69 = load i32, i32* %4
ret i32 %69
}

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