#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 *sbp;
  unsigned char mx_sb_len;
  char *masked_status;
  int /*<<< orphan*/ sb_len_wr;
  int /*<<< orphan*/ dxfer_direction;
};
typedef int /*<<< orphan*/ sense_buf;
typedef int /*<<< orphan*/ io_hdr;
typedef int /*<<< orphan*/ dev_name;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
char *CHECK_CONDITION;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SG_DXFER_NONE;
int /*<<< orphan*/ SG_IO;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct sg_io_hdr *);
int /*<<< orphan*/ memset(struct sg_io_hdr *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ phex(unsigned char *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ write_log(int /*<<< orphan*/, char *, char *);
void spindown_disk(char *name) {
  struct sg_io_hdr io_hdr;
  unsigned char sense_buf[255];
  char dev_name[100];
  int fd;
  write_log(LOG_INFO, "disk %s: spindown", name);
  memset(&io_hdr, 0x00, sizeof(io_hdr));
  io_hdr.interface_id = 'S';
  io_hdr.dxfer_direction = SG_DXFER_NONE;
  io_hdr.cmdp = (unsigned char *)"\x1b\x00\x00\x00\x00\x00";
  io_hdr.cmd_len = 6;
  io_hdr.sbp = sense_buf;
  io_hdr.mx_sb_len = (unsigned char)sizeof(sense_buf);
  snprintf(dev_name, sizeof(dev_name), "/dev/%s", name);
  if ((fd = open(dev_name, O_RDONLY)) < 0) {
    perror(dev_name);
    return;
  }
  if (ioctl(fd, SG_IO, &io_hdr) < 0) {
    char buf[100];
    snprintf(buf, sizeof(buf), "ioctl on %s:", name);
    perror(buf);
  } else if (io_hdr.masked_status != 0) {
    write_log(LOG_ERR, "error: SCSI command failed with status 0x%02x",
              io_hdr.masked_status);
    if (io_hdr.masked_status == CHECK_CONDITION) {
      phex(sense_buf, io_hdr.sb_len_wr, "sense buffer:\n");
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<spindown_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe5d(%rip),%rsi
call   1080 <write_log@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
movb   $0x53,-0x40(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe95(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x6,-0x30(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x28(%rbp)
movb   $0xff,-0x20(%rbp)
lea    -0x1b0(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x64,%esi
lea    0xe12(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x1b0(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1b4(%rbp)
cmp    $0x0,%eax
jge    123a <spindown_disk+0xba>
lea    -0x1b0(%rbp),%rdi
call   10a0 <perror@plt>
jmp    12ec <spindown_disk+0x16c>
mov    -0x1b4(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1289 <spindown_disk+0x109>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x64,%esi
lea    0xda7(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x220(%rbp),%rdi
call   10a0 <perror@plt>
jmp    12e1 <spindown_disk+0x161>
cmpq   $0x0,-0x18(%rbp)
je     12dc <spindown_disk+0x15c>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd7f(%rip),%rsi
call   1080 <write_log@plt>
mov    -0x18(%rbp),%rax
mov    0x2d20(%rip),%rcx
cmp    (%rcx),%rax
jne    12d7 <spindown_disk+0x157>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%esi
lea    0xd83(%rip),%rdx
call   1060 <phex@plt>
jmp    12dc <spindown_disk+0x15c>
jmp    12e1 <spindown_disk+0x161>
mov    -0x1b4(%rbp),%edi
call   1070 <close@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spindown_disk>:
push   %rbp
push   %r14
push   %rbx
sub    $0x220,%rsp
mov    %rdi,%rbx
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
mov    %rbx,%rdx
call   1080 <write_log@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
movb   $0x53,0x8(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x3c(%rsp)
lea    0xe94(%rip),%rax
mov    %rax,0x10(%rsp)
movl   $0x6,0x18(%rsp)
lea    0x120(%rsp),%rax
mov    %rax,0x20(%rsp)
movb   $0xff,0x28(%rsp)
lea    0xe1c(%rip),%rdx
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1286 <spindown_disk+0x106>
mov    %eax,%ebp
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     1292 <spindown_disk+0x112>
mov    0x30(%rsp),%rdx
test   %rdx,%rdx
je     12b9 <spindown_disk+0x139>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1080 <write_log@plt>
mov    0x30(%rsp),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    12b9 <spindown_disk+0x139>
mov    0x38(%rsp),%esi
lea    0xdde(%rip),%rdx
lea    0x120(%rsp),%rdi
call   1060 <phex@plt>
jmp    12b9 <spindown_disk+0x139>
lea    0x40(%rsp),%rdi
call   10a0 <perror@plt>
jmp    12c0 <spindown_disk+0x140>
lea    0xd81(%rip),%rdx
lea    0xb0(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x220,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sg_io_hdr = type { i8, i8*, i32, i8*, i8, i8*, i32, i32 }
@LOG_INFO = global i32 0
@.str = [18 x i8] c"disk %s: spindown\00"
@SG_DXFER_NONE = global i32 0
@.str.1 = [7 x i8] c"\1B\00\00\00\00\00\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@O_RDONLY = global i32 0
@SG_IO = global i32 0
@.str.3 = [13 x i8] c"ioctl on %s:\00"
@LOG_ERR = global i32 0
@.str.4 = [46 x i8] c"error: SCSI command failed with status 0x%02x\00"
@CHECK_CONDITION = global i8* null
@.str.5 = [15 x i8] c"sense buffer:\0A\00"
define void @spindown_disk(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sg_io_hdr
%4 = alloca [255 x i8]
%5 = alloca [100 x i8]
%6 = alloca i32
%7 = alloca [100 x i8]
store i8* %0, i8** %2
%8 = load i32, i32* @LOG_INFO
%9 = load i8*, i8** %2
%10 = call i32 @write_log(i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = call i32 @memset(%struct.sg_io_hdr* %3, i32 0, i32 56)
%12 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 0
store i8 83, i8* %12
%13 = load i32, i32* @SG_DXFER_NONE
%14 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 7
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 2
store i32 6, i32* %16
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 3
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 4
store i8 -1, i8* %19
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%21 = load i8*, i8** %2
%22 = call i32 @snprintf(i8* %20, i32 100, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %1
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%29 = call i32 @perror(i8* %28)
br label %64
30: ; preds = %1
%31 = load i32, i32* %6
%32 = load i32, i32* @SG_IO
%33 = call i64 @ioctl(i32 %31, i32 %32, %struct.sg_io_hdr* %3)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%37 = load i8*, i8** %2
%38 = call i32 @snprintf(i8* %36, i32 100, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%40 = call i32 @perror(i8* %39)
br label %61
41: ; preds = %30
%42 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 5
%43 = load i8*, i8** %42
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %60
45: ; preds = %41
%46 = load i32, i32* @LOG_ERR
%47 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 5
%48 = load i8*, i8** %47
%49 = call i32 @write_log(i32 %46, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 5
%51 = load i8*, i8** %50
%52 = load i8*, i8** @CHECK_CONDITION
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %59
54: ; preds = %45
%55 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%56 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %3, i32 0, i32 6
%57 = load i32, i32* %56
%58 = call i32 @phex(i8* %55, i32 %57, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %54, %45
br label %60
60: ; preds = %59, %41
br label %61
61: ; preds = %60, %35
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %27
ret void
}

LLVM IR (-O2):
%struct.sg_io_hdr = type { i8, i8*, i32, i8*, i8, i8*, i32, i32 }
@LOG_INFO = global i32 0
@.str = [18 x i8] c"disk %s: spindown\00"
@SG_DXFER_NONE = global i32 0
@.str.1 = [7 x i8] c"\1B\00\00\00\00\00\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@O_RDONLY = global i32 0
@SG_IO = global i32 0
@.str.3 = [13 x i8] c"ioctl on %s:\00"
@LOG_ERR = global i32 0
@.str.4 = [46 x i8] c"error: SCSI command failed with status 0x%02x\00"
@CHECK_CONDITION = global i8* null
@.str.5 = [15 x i8] c"sense buffer:\0A\00"
define void @spindown_disk(i8* %0) {
%2 = alloca %struct.sg_io_hdr
%3 = alloca [255 x i8]
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 0
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @LOG_INFO
%10 = tail call i32 @write_log(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = call i32 @memset(%struct.sg_io_hdr* %2, i32 0, i32 56)
store i8 83, i8* %6
%12 = load i32, i32* @SG_DXFER_NONE
%13 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 7
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 2
store i32 6, i32* %15
%16 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 3
store i8* %7, i8** %16
%17 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 4
store i8 -1, i8* %17
%18 = call i32 @snprintf(i8* %8, i32 100, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %8, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @perror(i8* %8)
br label %48
24: ; preds = %1
%25 = load i32, i32* @SG_IO
%26 = call i64 @ioctl(i32 %20, i32 %25, %struct.sg_io_hdr* %2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%30 = call i32 @snprintf(i8* %29, i32 100, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%31 = call i32 @perror(i8* %29)
br label %46
32: ; preds = %24
%33 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 5
%34 = load i8*, i8** %33
%35 = icmp eq i8* %34, null
br i1 %35, label %46, label %36
36: ; preds = %32
%37 = load i32, i32* @LOG_ERR
%38 = call i32 @write_log(i32 %37, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%39 = load i8*, i8** %33
%40 = load i8*, i8** @CHECK_CONDITION
%41 = icmp eq i8* %39, %40
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %2, i64 0, i32 6
%44 = load i32, i32* %43
%45 = call i32 @phex(i8* %7, i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %32, %42, %36, %28
%47 = call i32 @close(i32 %20)
br label %48
48: ; preds = %46, %22
ret void
}