#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 hd_driveid {
  int config;
  int command_set_1;
  char *serial_no;
};

/* Variables and functions */
int /*<<< orphan*/ HDIO_GET_IDENTITY;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct hd_driveid *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  struct hd_driveid *id;
  char *dev = "/dev/hda";
  int fd;
  if (argc > 1)
    dev = argv[1];
  fd = open(dev, O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    perror("cannot open");
  }
  if (ioctl(fd, HDIO_GET_IDENTITY, id) < 0) {
    close(fd);
    perror("ioctl error");
  } else {
    if ((id->config & (1 << 7)) || (id->command_set_1 & 4)) {
      close(fd);
      printf("Serial Number: %s\n", id->serial_no);
    } else {
      perror("support not removable");
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1187 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11bb <main+0x6b>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11f3 <main+0xa3>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
jmp    124f <main+0xff>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    121a <main+0xca>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     123b <main+0xeb>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf0(%rip),%rdi
call   1030 <printf@plt>
jmp    1247 <main+0xf7>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jl     114c <main+0xc>
mov    0x8(%rsi),%rdi
jmp    1153 <main+0x13>
lea    0xead(%rip),%rdi
mov    0x2e86(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    117c <main+0x3c>
lea    0xe92(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jns    11a6 <main+0x66>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xe76(%rip),%rdi
call   1060 <perror@plt>
jmp    11b9 <main+0x79>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hd_driveid = type { i32, i32, i8* }
@.str = [9 x i8] c"/dev/hda\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [12 x i8] c"cannot open\00"
@HDIO_GET_IDENTITY = global i32 0
@.str.2 = [12 x i8] c"ioctl error\00"
@.str.3 = [19 x i8] c"Serial Number: %s\0A\00"
@.str.4 = [22 x i8] c"support not removable\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.hd_driveid*
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %7
br label %15
15: ; preds = %11, %2
%16 = load i8*, i8** %7
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = load i32, i32* %8
%27 = load i32, i32* @HDIO_GET_IDENTITY
%28 = load %struct.hd_driveid*, %struct.hd_driveid** %6
%29 = call i64 @ioctl(i32 %26, i32 %27, %struct.hd_driveid* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %59
35: ; preds = %25
%36 = load %struct.hd_driveid*, %struct.hd_driveid** %6
%37 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, 128
%40 = icmp ne i32 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %35
%42 = load %struct.hd_driveid*, %struct.hd_driveid** %6
%43 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = and i32 %44, 4
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %41, %35
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load %struct.hd_driveid*, %struct.hd_driveid** %6
%51 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %50, i32 0, i32 2
%52 = load i8*, i8** %51
%53 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %52)
br label %56
54: ; preds = %41
%55 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %47
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %31
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.hd_driveid = type { i32, i32, i8* }
@.str = [9 x i8] c"/dev/hda\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [12 x i8] c"cannot open\00"
@HDIO_GET_IDENTITY = global i32 0
@.str.2 = [12 x i8] c"ioctl error\00"
@.str.4 = [22 x i8] c"support not removable\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %4, %2
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %7
%17 = load i32, i32* @HDIO_GET_IDENTITY
%18 = tail call i64 @ioctl(i32 %12, i32 %17, %struct.hd_driveid* undef)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @close(i32 %12)
%22 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %26
23: ; preds = %16
%24 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @close(i32 %12)
br label %26
26: ; preds = %23, %20
ret i32 0
}