#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 /*<<< 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 /*<<< 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;
  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;
  return ioctl(fd, SG_IO, &io_hdr);
}


x86 assembly (clang, -O0):
<scsi_get_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%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,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x20,-0x58(%rbp)
movl   $0x1388,-0x54(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rdx
call   1040 <ioctl@plt>
add    $0x80,%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,0x40(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x30(%rsp)
movabs $0x138800000020,%rax
mov    %rax,0x38(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
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, i8* }
@INQUIRY = global i32 0
@SG_DXFER_FROM_DEV = global i32 0
@SG_IO = global i32 0
define i32 @scsi_get_serial(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [6 x i8]
%8 = alloca [32 x i8]
%9 = alloca %struct.sg_io_hdr
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* @INQUIRY
%12 = trunc i32 %11 to i8
store i8 %12, i8* %10
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 1, i8* %13
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8 -128, i8* %14
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 0, i8* %15
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i64, i64* %6
%18 = trunc i64 %17 to i8
store i8 %18, i8* %16
%19 = getelementptr inbounds i8, i8* %16, i64 1
store i8 0, i8* %19
%20 = call i32 @memset(%struct.sg_io_hdr* %9, i32 0, i32 64)
%21 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 0
store i8 83, i8* %21
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%23 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 1
store i8* %22, i8** %23
%24 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 2
store i32 6, i32* %24
%25 = load i8*, i8** %5
%26 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 8
store i8* %25, i8** %26
%27 = load i64, i64* %6
%28 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 3
store i64 %27, i64* %28
%29 = load i32, i32* @SG_DXFER_FROM_DEV
%30 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 7
store i32 %29, i32* %30
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%32 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 4
store i8* %31, i8** %32
%33 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 5
store i32 32, i32* %33
%34 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %9, i32 0, i32 6
store i32 5000, i32* %34
%35 = load i32, i32* %4
%36 = load i32, i32* @SG_IO
%37 = call i32 @ioctl(i32 %35, i32 %36, %struct.sg_io_hdr* %9)
ret i32 %37
}

LLVM IR (-O2):
%struct.sg_io_hdr = type { i8, i8*, i32, i64, i8*, i32, i32, i32, i8* }
@INQUIRY = global i32 0
@SG_DXFER_FROM_DEV = global i32 0
@SG_IO = 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 8
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 7
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)
ret i32 %29
}