#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*/ identity;

/* Variables and functions */
int /*<<< orphan*/ HDIO_GET_IDENTITY;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char, char);
int main(int argc, char *argv[]) {
  char *device = "/dev/sda";
  if (argc > 1)
    device = argv[1];
  int fd = open(device, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }
  char identity[512];
  int sc = ioctl(fd, HDIO_GET_IDENTITY, identity);
  if (sc == -1) {
    perror("ioctl");
    close(fd);
    return -1;
  }
  int i;
  for (i = 0; i < sizeof(identity); i++) {
    if (identity[i]) {
      printf("%d %c (%x)\n", i, identity[i], identity[i] & 0xff);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    118a <main+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c1 <main+0x71>
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <main+0x141>
mov    -0x1c(%rbp),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    -0x220(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0xffffffff,-0x224(%rbp)
jne    120c <main+0xbc>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <main+0x141>
movl   $0x0,-0x228(%rbp)
movslq -0x228(%rbp),%rax
cmp    $0x200,%rax
jae    1291 <main+0x141>
movslq -0x228(%rbp),%rax
cmpb   $0x0,-0x220(%rbp,%rax,1)
je     1278 <main+0x128>
mov    -0x228(%rbp),%esi
movslq -0x228(%rbp),%rax
mov    -0x220(%rbp,%rax,1),%cl
movslq -0x228(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
and    $0xff,%eax
lea    0xda7(%rip),%rdi
movsbl %cl,%edx
movsbl %al,%ecx
call   1030 <printf@plt>
jmp    127d <main+0x12d>
mov    -0x228(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x228(%rbp)
jmp    1216 <main+0xc6>
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x208,%rsp
cmp    $0x2,%edi
jl     1165 <main+0x15>
mov    0x8(%rsi),%rdi
jmp    116c <main+0x1c>
lea    0xe94(%rip),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e3 <main+0x93>
mov    %eax,%ebx
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0xa6>
xor    %ebx,%ebx
lea    0xe72(%rip),%r14
jmp    11bd <main+0x6d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
cmp    $0x200,%rbx
je     11d6 <main+0x86>
movzbl (%rsp,%rbx,1),%eax
test   %al,%al
je     11b0 <main+0x60>
movsbl %al,%ecx
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ecx,%edx
call   1030 <printf@plt>
jmp    11b0 <main+0x60>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11d8 <main+0x88>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11d8 <main+0x88>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/sda\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@HDIO_GET_IDENTITY = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [12 x i8] c"%d %c (%x)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = 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** %6
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
br label %17
17: ; preds = %13, %2
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
25: ; preds = %17
%26 = load i32, i32* %7
%27 = load i32, i32* @HDIO_GET_IDENTITY
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%29 = call i32 @ioctl(i32 %26, i32 %27, i8* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %3
br label %65
36: ; preds = %25
store i32 0, i32* %10
br label %37
37: ; preds = %62, %36
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = icmp ult i64 %39, 512
br i1 %40, label %41, label %65
41: ; preds = %37
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = icmp ne i8 %45, 0
br i1 %46, label %47, label %61
47: ; preds = %41
%48 = load i32, i32* %10
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = and i32 %57, 255
%59 = trunc i32 %58 to i8
%60 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %48, i8 signext %52, i8 signext %59)
br label %61
61: ; preds = %47, %41
br label %62
62: ; preds = %61
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %37
65: ; preds = %23, %32, %37
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/sda\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@HDIO_GET_IDENTITY = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [12 x i8] c"%d %c (%x)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
br label %8
8: ; preds = %5, %2
%9 = phi i8* [ %7, %5 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %2 ]
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %36
15: ; preds = %8
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* @HDIO_GET_IDENTITY
%18 = call i32 @ioctl(i32 %11, i32 %17, i8* %16)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @close(i32 %11)
br label %34
23: ; preds = %15, %31
%24 = phi i64 [ %32, %31 ], [ 0, %15 ]
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = trunc i64 %24 to i32
%30 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %29, i8 signext %26, i8 signext %26)
br label %31
31: ; preds = %23, %28
%32 = add nuw nsw i64 %24, 1
%33 = icmp eq i64 %32, 512
br i1 %33, label %34, label %23
34: ; preds = %31, %20
%35 = phi i32 [ -1, %20 ], [ 0, %31 ]
br label %36
36: ; preds = %34, %13
%37 = phi i32 [ -1, %13 ], [ %35, %34 ]
ret i32 %37
}