#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 /*<<< orphan*/ serial_no;
  int /*<<< orphan*/ fw_rev;
  int /*<<< orphan*/ model;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fix_string(int /*<<< orphan*/, int);
int /*<<< orphan*/ *fopen(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
short strtol(char *, int /*<<< orphan*/ *, int);
int read_proc_identify(char *dev, struct hd_driveid *id) {
  char proc[] = "/proc/ide/hd#/identify";
  char s[42];
  int i;
  if ((strncmp(dev, "/dev/hd", 7) != 0) || (strlen(dev) != 8))
    return -1;
  proc[12] = dev[7];
  if (access(proc, R_OK) == 0) {
    FILE *f = fopen(proc, "r");
    short *b = (short *)id;
    while (fgets(s, 41, f)) {
      for (i = 0; i < 40; i += 5, b++) {
        *b = (strtol(s + i, NULL, 16));
      }
    }
    fclose(f);
    fix_string(id->model, sizeof(id->model));
    fix_string(id->fw_rev, sizeof(id->fw_rev));
    fix_string(id->serial_no, sizeof(id->serial_no));
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_proc_identify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe66(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe63(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe61(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe5b(%rip),%ax
mov    %ax,-0x1c(%rbp)
mov    0xe53(%rip),%al
mov    %al,-0x1a(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f7 <read_proc_identify+0x77>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x8,%eax
je     1203 <read_proc_identify+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <read_proc_identify+0x182>
mov    -0x10(%rbp),%rax
mov    0x7(%rax),%al
mov    %al,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    12fb <read_proc_identify+0x17b>
lea    -0x30(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x70(%rbp),%rdx
mov    $0x29,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12b4 <read_proc_identify+0x134>
movl   $0x0,-0x64(%rbp)
cmpl   $0x28,-0x64(%rbp)
jge    12af <read_proc_identify+0x12f>
lea    -0x60(%rbp),%rdi
movslq -0x64(%rbp),%rax
add    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,%cx
mov    -0x78(%rbp),%rax
mov    %cx,(%rax)
mov    -0x64(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x64(%rbp)
mov    -0x78(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x78(%rbp)
jmp    1268 <read_proc_identify+0xe8>
jmp    1245 <read_proc_identify+0xc5>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    $0x4,%esi
call   1060 <fix_string@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    $0x4,%esi
call   1060 <fix_string@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    $0x4,%esi
call   1060 <fix_string@plt>
movl   $0x0,-0x4(%rbp)
jmp    1302 <read_proc_identify+0x182>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_identify>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
movaps 0xe65(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x796669746e6564,%rax
mov    %rax,0x1f(%rsp)
lea    0xe61(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1342 <read_proc_identify+0x1c2>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    $0x8,%eax
jne    1342 <read_proc_identify+0x1c2>
mov    0x7(%rbp),%al
mov    %al,0x1c(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1090 <access@plt>
test   %rax,%rax
jne    1342 <read_proc_identify+0x1c2>
mov    %r14,0x8(%rsp)
lea    0xe11(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0x30(%rsp),%rdi
mov    $0x29,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     130d <read_proc_identify+0x18d>
lea    0x49(%rsp),%r14
lea    0x4e(%rsp),%r15
lea    0x53(%rsp),%r12
lea    0x30(%rsp),%r13
mov    0x8(%rsp),%rbp
nop
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0x0(%rbp)
lea    0x35(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0x2(%rbp)
lea    0x3a(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0x4(%rbp)
lea    0x3f(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0x6(%rbp)
lea    0x44(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0x8(%rbp)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0xa(%rbp)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0xc(%rbp)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %ax,0xe(%rbp)
mov    %r13,%rdi
mov    $0x29,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
add    $0x10,%rbp
test   %rax,%rax
jne    1250 <read_proc_identify+0xd0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rbx
mov    0x8(%rbx),%edi
mov    $0x4,%esi
call   1060 <fix_string@plt>
mov    0x4(%rbx),%edi
mov    $0x4,%esi
call   1060 <fix_string@plt>
mov    (%rbx),%edi
mov    $0x4,%esi
call   1060 <fix_string@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hd_driveid = type { i32, i32, i32 }
@__const.read_proc_identify.proc = [23 x i8] c"/proc/ide/hd#/identify\00"
@.str = [8 x i8] c"/dev/hd\00"
@R_OK = global i32 0
@.str.1 = [2 x i8] c"r\00"
define i32 @read_proc_identify(i8* %0, %struct.hd_driveid* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.hd_driveid*
%6 = alloca [23 x i8]
%7 = alloca [42 x i8]
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i16*
store i8* %0, i8** %4
store %struct.hd_driveid* %1, %struct.hd_driveid** %5
%11 = bitcast [23 x i8]* %6 to i8*
%12 = load i8*, i8** %4
%13 = call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%14 = icmp ne i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = icmp ne i32 %17, 8
br i1 %18, label %19, label %20
19: ; preds = %15, %2
store i32 -1, i32* %3
br label %72
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 7
%23 = load i8, i8* %22
%24 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 12
store i8 %23, i8* %24
%25 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* @R_OK
%27 = call i64 @access(i8* %25, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %71
29: ; preds = %20
%30 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %9
%32 = load %struct.hd_driveid*, %struct.hd_driveid** %5
%33 = bitcast %struct.hd_driveid* %32 to i16*
store i16* %33, i16** %10
br label %34
34: ; preds = %55, %29
%35 = getelementptr inbounds [42 x i8], [42 x i8]* %7, i64 0, i64 0
%36 = load i32*, i32** %9
%37 = call i64 @fgets(i8* %35, i32 41, i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %56
39: ; preds = %34
store i32 0, i32* %8
br label %40
40: ; preds = %50, %39
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 40
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = getelementptr inbounds [42 x i8], [42 x i8]* %7, i64 0, i64 0
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = call signext i16 @strtol(i8* %47, i32* null, i32 16)
%49 = load i16*, i16** %10
store i16 %48, i16* %49
br label %50
50: ; preds = %43
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 5
store i32 %52, i32* %8
%53 = load i16*, i16** %10
%54 = getelementptr inbounds i16, i16* %53, i32 1
store i16* %54, i16** %10
br label %40
55: ; preds = %40
br label %34
56: ; preds = %34
%57 = load i32*, i32** %9
%58 = call i32 @fclose(i32* %57)
%59 = load %struct.hd_driveid*, %struct.hd_driveid** %5
%60 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %59, i32 0, i32 2
%61 = load i32, i32* %60
%62 = call i32 @fix_string(i32 %61, i32 4)
%63 = load %struct.hd_driveid*, %struct.hd_driveid** %5
%64 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %63, i32 0, i32 1
%65 = load i32, i32* %64
%66 = call i32 @fix_string(i32 %65, i32 4)
%67 = load %struct.hd_driveid*, %struct.hd_driveid** %5
%68 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @fix_string(i32 %69, i32 4)
store i32 0, i32* %3
br label %72
71: ; preds = %20
store i32 -1, i32* %3
br label %72
72: ; preds = %71, %56, %19
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.hd_driveid = type { i32, i32, i32 }
@__const.read_proc_identify.proc = [23 x i8] c"/proc/ide/hd#/identify\00"
@.str = [8 x i8] c"/dev/hd\00"
@R_OK = global i32 0
@.str.1 = [2 x i8] c"r\00"
define i32 @read_proc_identify(i8* %0, %struct.hd_driveid* nocapture %1) {
%3 = alloca [23 x i8]
%4 = alloca [42 x i8]
%5 = getelementptr inbounds [23 x i8], [23 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 0
%7 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %63
9: ; preds = %2
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp eq i32 %10, 8
br i1 %11, label %12, label %63
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %0, i64 7
%14 = load i8, i8* %13
%15 = getelementptr inbounds [23 x i8], [23 x i8]* %3, i64 0, i64 12
store i8 %14, i8* %15
%16 = load i32, i32* @R_OK
%17 = call i64 @access(i8* %5, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %63
19: ; preds = %12
%20 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = call i64 @fgets(i8* %6, i32 41, i32* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %52, label %23
23: ; preds = %19
%24 = bitcast %struct.hd_driveid* %1 to i16*
%25 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 5
%26 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 10
%27 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 15
%28 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 20
%29 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 25
%30 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 30
%31 = getelementptr inbounds [42 x i8], [42 x i8]* %4, i64 0, i64 35
br label %32
32: ; preds = %23, %32
%33 = phi i16* [ %49, %32 ], [ %24, %23 ]
%34 = call signext i16 @strtol(i8* nocapture %6, i32* null, i32 16)
store i16 %34, i16* %33
%35 = getelementptr inbounds i16, i16* %33, i64 1
%36 = call signext i16 @strtol(i8* nocapture %25, i32* null, i32 16)
store i16 %36, i16* %35
%37 = getelementptr inbounds i16, i16* %33, i64 2
%38 = call signext i16 @strtol(i8* nocapture %26, i32* null, i32 16)
store i16 %38, i16* %37
%39 = getelementptr inbounds i16, i16* %33, i64 3
%40 = call signext i16 @strtol(i8* nocapture %27, i32* null, i32 16)
store i16 %40, i16* %39
%41 = getelementptr inbounds i16, i16* %33, i64 4
%42 = call signext i16 @strtol(i8* nocapture %28, i32* null, i32 16)
store i16 %42, i16* %41
%43 = getelementptr inbounds i16, i16* %33, i64 5
%44 = call signext i16 @strtol(i8* nocapture %29, i32* null, i32 16)
store i16 %44, i16* %43
%45 = getelementptr inbounds i16, i16* %33, i64 6
%46 = call signext i16 @strtol(i8* nocapture %30, i32* null, i32 16)
store i16 %46, i16* %45
%47 = getelementptr inbounds i16, i16* %33, i64 7
%48 = call signext i16 @strtol(i8* nocapture %31, i32* null, i32 16)
store i16 %48, i16* %47
%49 = getelementptr inbounds i16, i16* %33, i64 8
%50 = call i64 @fgets(i8* %6, i32 41, i32* %20)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %32
52: ; preds = %32, %19
%53 = call i32 @fclose(i32* %20)
%54 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %1, i64 0, i32 2
%55 = load i32, i32* %54
%56 = call i32 @fix_string(i32 %55, i32 4)
%57 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %1, i64 0, i32 1
%58 = load i32, i32* %57
%59 = call i32 @fix_string(i32 %58, i32 4)
%60 = getelementptr inbounds %struct.hd_driveid, %struct.hd_driveid* %1, i64 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @fix_string(i32 %61, i32 4)
br label %63
63: ; preds = %12, %2, %9, %52
%64 = phi i32 [ 0, %52 ], [ -1, %9 ], [ -1, %2 ], [ -1, %12 ]
ret i32 %64
}