#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 */
typedef int /*<<< orphan*/ cdb;

/* Variables and functions */
int /*<<< orphan*/ SG_DXFER_FROM_DEV;
int /*<<< orphan*/ print_sense_data(unsigned char *, unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int scsi_io(int, unsigned char *, int, int /*<<< orphan*/, unsigned char *,
            unsigned int *, unsigned char *, unsigned int *);
int scsi_inquiry_unit_serial_number(int fd) {
  unsigned char cdb[] = {0x12, 0x01, 0x80, 0, 0, 0};
  unsigned int data_size = 0x00ff;
  unsigned char data[data_size];
  unsigned int sense_len = 32;
  unsigned char sense[sense_len];
  int res, pl, i;
  cdb[3] = (data_size >> 8) & 0xff;
  cdb[4] = data_size & 0xff;
  printf("INQUIRY Unit Serial Number:\n");
  res = scsi_io(fd, cdb, sizeof(cdb), SG_DXFER_FROM_DEV, data, &data_size,
                sense, &sense_len);
  if (res) {
    printf("SCSI_IO failed\n");
    return -1;
  }
  if (sense_len) {
    print_sense_data(sense, sense_len);
    return -1;
  }
  pl = data[3];
  printf("Unit Serial Number:");
  for (i = 4; i < (pl + 4); i++)
    printf("%c", data[i] & 0xff);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<scsi_inquiry_unit_serial_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
movw   $0x0,-0xc(%rbp)
movl   $0x800112,-0x10(%rbp)
movl   $0xff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x20,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0xd(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,-0xc(%rbp)
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x58(%rbp),%r8
mov    -0x50(%rbp),%r10
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
mov    $0x6,%edx
lea    -0x14(%rbp),%r9
lea    -0x2c(%rbp),%rax
sub    $0x10,%rsp
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <scsi_io@plt>
add    $0x10,%rsp
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     122b <scsi_inquiry_unit_serial_number+0xfb>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    12cf <scsi_inquiry_unit_serial_number+0x19f>
cmpl   $0x0,-0x2c(%rbp)
je     1254 <scsi_inquiry_unit_serial_number+0x124>
mov    -0x50(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <print_sense_data@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    12cf <scsi_inquiry_unit_serial_number+0x19f>
mov    -0x58(%rbp),%rax
movzbl 0x3(%rax),%eax
mov    %eax,-0x40(%rbp)
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x4,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x40(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
jge    12b3 <scsi_inquiry_unit_serial_number+0x183>
mov    -0x58(%rbp),%rax
movslq -0x44(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
and    $0xff,%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1274 <scsi_inquiry_unit_serial_number+0x144>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_inquiry_unit_serial_number>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x138,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
movw   $0x0,0x8(%rsp)
movl   $0x800112,0x4(%rsp)
movl   $0xff,0xc(%rsp)
movl   $0x20,(%rsp)
movw   $0xff00,0x7(%rsp)
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rax
lea    0x10(%rsp),%rbp
lea    0x4(%rsp),%rsi
lea    0x30(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0x6,%edx
push   %rax
push   %rbp
call   1060 <scsi_io@plt>
add    $0x10,%rsp
test   %eax,%eax
je     11dd <scsi_inquiry_unit_serial_number+0x8d>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r15d
jmp    123d <scsi_inquiry_unit_serial_number+0xed>
mov    (%rsp),%esi
test   %esi,%esi
je     11f6 <scsi_inquiry_unit_serial_number+0xa6>
lea    0x10(%rsp),%rdi
call   1070 <print_sense_data@plt>
mov    $0xffffffff,%r15d
jmp    123d <scsi_inquiry_unit_serial_number+0xed>
movzbl 0x33(%rsp),%ebx
lea    0xe0c(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1233 <scsi_inquiry_unit_serial_number+0xe3>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl 0x34(%rsp,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1220 <scsi_inquiry_unit_serial_number+0xd0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rsp
mov    %r15d,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.scsi_inquiry_unit_serial_number.cdb = [6 x i8] c"\12\01\80\00\00\00"
@.str = [29 x i8] c"INQUIRY Unit Serial Number:\0A\00"
@SG_DXFER_FROM_DEV = global i32 0
@.str.1 = [16 x i8] c"SCSI_IO failed\0A\00"
@.str.2 = [20 x i8] c"Unit Serial Number:\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @scsi_inquiry_unit_serial_number(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [6 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %3
%14 = bitcast [6 x i8]* %4 to i8*
store i32 255, i32* %5
%15 = load i32, i32* %5
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %6
%18 = alloca i8, i64 %16
store i64 %16, i64* %7
store i32 32, i32* %8
%19 = load i32, i32* %8
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
store i64 %20, i64* %9
%22 = load i32, i32* %5
%23 = lshr i32 %22, 8
%24 = and i32 %23, 255
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 3
store i8 %25, i8* %26
%27 = load i32, i32* %5
%28 = and i32 %27, 255
%29 = trunc i32 %28 to i8
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 4
store i8 %29, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* %3
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* @SG_DXFER_FROM_DEV
%35 = call i32 @scsi_io(i32 %32, i8* %33, i32 6, i32 %34, i8* %18, i32* %5, i8* %21, i32* %8)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %1
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %13
br label %69
40: ; preds = %1
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* %8
%45 = call i32 @print_sense_data(i8* %21, i32 %44)
store i32 -1, i32* %2
store i32 1, i32* %13
br label %69
46: ; preds = %40
%47 = getelementptr inbounds i8, i8* %18, i64 3
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
store i32 %49, i32* %11
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 4, i32* %12
br label %51
51: ; preds = %64, %46
%52 = load i32, i32* %12
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 4
%55 = icmp slt i32 %52, %54
br i1 %55, label %56, label %67
56: ; preds = %51
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %18, i64 %58
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = and i32 %61, 255
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %56
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 1
store i32 %66, i32* %12
br label %51
67: ; preds = %51
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
store i32 1, i32* %13
br label %69
69: ; preds = %67, %43, %38
%70 = load i8*, i8** %6
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
@__const.scsi_inquiry_unit_serial_number.cdb = [6 x i8] c"\12\01\80\00\00\00"
@SG_DXFER_FROM_DEV = global i32 0
@.str.2 = [20 x i8] c"Unit Serial Number:\00"
@str = [28 x i8] c"INQUIRY Unit Serial Number:\00"
@str.5 = [15 x i8] c"SCSI_IO failed\00"
define i32 @scsi_inquiry_unit_serial_number(i32 %0) {
%2 = alloca [6 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
store i32 255, i32* %3
%7 = tail call i8* @llvm.stacksave()
%8 = alloca [255 x i8]
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%10 = bitcast i32* %4 to i8*
store i32 32, i32* %4
%11 = alloca [32 x i8]
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%13 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 3
store i8 0, i8* %13
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 4
store i8 -1, i8* %14
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @SG_DXFER_FROM_DEV
%17 = call i32 @scsi_io(i32 %0, i8* %5, i32 6, i32 %16, i8* %9, i32* %3, i8* %12, i32* %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %1
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
br label %44
21: ; preds = %1
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @print_sense_data(i8* %12, i32 %22)
br label %44
26: ; preds = %21
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 3
%28 = load i8, i8* %27
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i8 %28, 0
br i1 %30, label %42, label %31
31: ; preds = %26
%32 = zext i8 %28 to i64
%33 = add nuw nsw i64 %32, 3
br label %34
34: ; preds = %31, %34
%35 = phi i64 [ 4, %31 ], [ %40, %34 ]
%36 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = call i32 @putchar(i32 %38)
%40 = add nuw nsw i64 %35, 1
%41 = icmp eq i64 %35, %33
br i1 %41, label %42, label %34
42: ; preds = %34, %26
%43 = call i32 @putchar(i32 10)
br label %44
44: ; preds = %42, %24, %19
%45 = phi i32 [ -1, %19 ], [ -1, %24 ], [ 0, %42 ]
ret i32 %45
}