#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*/ infoline;
typedef unsigned char d;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int disk_info_from_fd(int, int *, unsigned int *, unsigned int *,
                      unsigned char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int sscanf(char *, char *, unsigned int *, unsigned int *);
int disk_get_ide_pci(int fd, unsigned char *bus, unsigned char *device,
                     unsigned char *function) {
  int num_scanned, procfd;
  unsigned int b = 0, d = 0, disknum = 0, controllernum = 0;
  unsigned char part = 0;
  char procname[80], infoline[80];
  size_t read_count;
  int interface_type;
  int rc;
  rc = disk_info_from_fd(fd, &interface_type, &controllernum, &disknum, &part);
  if (rc)
    return rc;
  sprintf(procname, "/proc/ide/ide%d/config", controllernum);
  procfd = open(procname, O_RDONLY);
  if (!procfd) {
    perror("opening /proc/ide/ide*/config");
    return 1;
  }
  read_count = read(procfd, infoline, sizeof(infoline) - 1);
  close(procfd);
  num_scanned = sscanf(
      infoline, "pci bus %x device %x vid %*x did %*x channel %*x", &b, &d);
  if (num_scanned == 2) {
    *bus = b;
    *device = (((d) >> 3) & 0x1f);
    *function = ((d)&0x07);
  }
  return 0;
}


x86 assembly (clang, -O0):
<disk_get_ide_pci>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movb   $0x0,-0x39(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xec(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x34(%rbp),%rcx
lea    -0x39(%rbp),%r8
call   1050 <disk_info_from_fd@plt>
mov    %eax,-0xf0(%rbp)
cmpl   $0x0,-0xf0(%rbp)
je     11e6 <disk_get_ide_pci+0x76>
mov    -0xf0(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <disk_get_ide_pci+0x142>
lea    -0x90(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xe09(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1236 <disk_get_ide_pci+0xc6>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <disk_get_ide_pci+0x142>
mov    -0x28(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0x4f,%edx
call   1040 <read@plt>
mov    %rax,-0xe8(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
lea    -0xe0(%rbp),%rdi
lea    0xdce(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <sscanf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
jne    12ab <disk_get_ide_pci+0x13b>
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%eax
shr    $0x3,%eax
and    $0x1f,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%eax
and    $0x7,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disk_get_ide_pci>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xc0,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0x18(%rsp)
movl   $0x0,0xc(%rsp)
movb   $0x0,0xb(%rsp)
lea    0x1c(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0xb(%rsp),%r8
call   1050 <disk_info_from_fd@plt>
test   %eax,%eax
jne    1254 <disk_get_ide_pci+0xe4>
mov    0xc(%rsp),%edx
lea    0xe27(%rip),%rsi
lea    0x70(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <sprintf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
je     1264 <disk_get_ide_pci+0xf4>
mov    %eax,%ebp
lea    0x20(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4f,%edx
call   1040 <read@plt>
mov    %ebp,%edi
call   1030 <close@plt>
lea    0xe16(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
call   1060 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x2,%ecx
jne    1254 <disk_get_ide_pci+0xe4>
mov    0x14(%rsp),%cl
mov    %cl,(%r12)
mov    0x10(%rsp),%cl
mov    %ecx,%edx
shr    $0x3,%dl
mov    %dl,(%r15)
and    $0x7,%cl
mov    %cl,(%r14)
add    $0xc0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1254 <disk_get_ide_pci+0xe4>

LLVM IR (-O0):
@.str = [23 x i8] c"/proc/ide/ide%d/config\00"
@O_RDONLY = global i32 0
@.str.1 = [30 x i8] c"opening /proc/ide/ide*/config\00"
@.str.2 = [49 x i8] c"pci bus %x device %x vid %*x did %*x channel %*x\00"
define i32 @disk_get_ide_pci(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8
%17 = alloca [80 x i8]
%18 = alloca [80 x i8]
%19 = alloca i64
%20 = alloca i32
%21 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
store i8 0, i8* %16
%22 = load i32, i32* %6
%23 = call i32 @disk_info_from_fd(i32 %22, i32* %20, i32* %15, i32* %14, i8* %16)
store i32 %23, i32* %21
%24 = load i32, i32* %21
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %4
%27 = load i32, i32* %21
store i32 %27, i32* %5
br label %63
28: ; preds = %4
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %17, i64 0, i64 0
%30 = load i32, i32* %15
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %17, i64 0, i64 0
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %63
39: ; preds = %28
%40 = load i32, i32* %11
%41 = getelementptr inbounds [80 x i8], [80 x i8]* %18, i64 0, i64 0
%42 = call i64 @read(i32 %40, i8* %41, i32 79)
store i64 %42, i64* %19
%43 = load i32, i32* %11
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %18, i64 0, i64 0
%46 = call i32 @sscanf(i8* %45, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32* %12, i32* %13)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, 2
br i1 %48, label %49, label %62
49: ; preds = %39
%50 = load i32, i32* %12
%51 = trunc i32 %50 to i8
%52 = load i8*, i8** %7
store i8 %51, i8* %52
%53 = load i32, i32* %13
%54 = lshr i32 %53, 3
%55 = and i32 %54, 31
%56 = trunc i32 %55 to i8
%57 = load i8*, i8** %8
store i8 %56, i8* %57
%58 = load i32, i32* %13
%59 = and i32 %58, 7
%60 = trunc i32 %59 to i8
%61 = load i8*, i8** %9
store i8 %60, i8* %61
br label %62
62: ; preds = %49, %39
store i32 0, i32* %5
br label %63
63: ; preds = %62, %37, %26
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
@.str = [23 x i8] c"/proc/ide/ide%d/config\00"
@O_RDONLY = global i32 0
@.str.1 = [30 x i8] c"opening /proc/ide/ide*/config\00"
@.str.2 = [49 x i8] c"pci bus %x device %x vid %*x did %*x channel %*x\00"
define i32 @disk_get_ide_pci(i32 %0, i8* nocapture writeonly %1, i8* nocapture writeonly %2, i8* nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca [80 x i8]
%11 = alloca [80 x i8]
%12 = alloca i32
%13 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%14 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%15 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%16 = bitcast i32* %8 to i8*
store i32 0, i32* %8
store i8 0, i8* %9
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%19 = bitcast i32* %12 to i8*
%20 = call i32 @disk_info_from_fd(i32 %0, i32* %12, i32* %8, i32* %7, i8* %9)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %42
22: ; preds = %4
%23 = load i32, i32* %8
%24 = call i32 @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %17, i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %42
30: ; preds = %22
%31 = call i64 @read(i32 %26, i8* %18, i32 79)
%32 = call i32 @close(i32 %26)
%33 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32* %5, i32* %6)
%34 = icmp eq i32 %33, 2
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = load i32, i32* %5
%37 = trunc i32 %36 to i8
store i8 %37, i8* %1
%38 = load i32, i32* %6
%39 = trunc i32 %38 to i8
%40 = lshr i8 %39, 3
store i8 %40, i8* %2
%41 = and i8 %39, 7
store i8 %41, i8* %3
br label %42
42: ; preds = %30, %35, %4, %28
%43 = phi i32 [ 1, %28 ], [ %20, %4 ], [ 0, %35 ], [ 0, %30 ]
ret i32 %43
}