#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
int get_scsi_pci(int, char *);
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, unsigned int *, unsigned int *, unsigned int *);
int /*<<< orphan*/ stderr;
int disk_get_scsi_pci(int fd, unsigned char *bus, unsigned char *device,
                      unsigned char *function) {
  int rc, usefd = fd;
  struct stat buf;
  char slot_name[8];
  unsigned int b = 0, d = 0, f = 0;
  memset(&buf, 0, sizeof(buf));
  rc = fstat(fd, &buf);
  if (rc == -1) {
    perror("stat");
    return 1;
  }
  if (S_ISREG(buf.st_mode)) {
    fprintf(stderr, "You must call this program with "
                    "a file name such as /dev/sda.\n");
    return 1;
  }
  rc = get_scsi_pci(usefd, slot_name);
  if (rc) {
    perror("get_scsi_pci");
    return rc;
  }
  rc = sscanf(slot_name, "%x:%x.%x", &b, &d, &f);
  if (rc != 3) {
    printf("sscanf failed\n");
    return 1;
  }
  *bus = b & 0xFF;
  *device = d & 0xFF;
  *function = f & 0xFF;
  return 0;
}


x86 assembly (clang, -O0):
<disk_get_scsi_pci>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f3 <disk_get_scsi_pci+0x73>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <disk_get_scsi_pci+0x14e>
mov    -0x30(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     1226 <disk_get_scsi_pci+0xa6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <disk_get_scsi_pci+0x14e>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <get_scsi_pci@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1256 <disk_get_scsi_pci+0xd6>
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ce <disk_get_scsi_pci+0x14e>
lea    -0x38(%rbp),%rdi
lea    0xdf0(%rip),%rsi
lea    -0x3c(%rbp),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x44(%rbp),%r8
call   1070 <sscanf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
je     1297 <disk_get_scsi_pci+0x117>
lea    0xdd4(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <disk_get_scsi_pci+0x14e>
mov    -0x3c(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x40(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x44(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disk_get_scsi_pci>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1265 <disk_get_scsi_pci+0xe5>
mov    0x10(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     1203 <disk_get_scsi_pci+0x83>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebp
jmp    1286 <disk_get_scsi_pci+0x106>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
call   1080 <get_scsi_pci@plt>
test   %eax,%eax
jne    1278 <disk_get_scsi_pci+0xf8>
lea    0xe37(%rip),%rsi
lea    0x18(%rsp),%rdi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
call   1070 <sscanf@plt>
cmp    $0x3,%eax
jne    1252 <disk_get_scsi_pci+0xd2>
mov    0xc(%rsp),%al
mov    %al,(%r12)
mov    0x8(%rsp),%al
mov    %al,(%r15)
mov    0x4(%rsp),%al
mov    %al,(%r14)
xor    %ebp,%ebp
jmp    1286 <disk_get_scsi_pci+0x106>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    1286 <disk_get_scsi_pci+0x106>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebp
jmp    1286 <disk_get_scsi_pci+0x106>
mov    %eax,%ebp
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.1 = [63 x i8] c"You must call this program with a file name such as /dev/sda.\0A\00"
@.str.2 = [13 x i8] c"get_scsi_pci\00"
@.str.3 = [9 x i8] c"%x:%x.%x\00"
@.str.4 = [15 x i8] c"sscanf failed\0A\00"
define i32 @disk_get_scsi_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 %struct.stat
%13 = alloca [8 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%17 = load i32, i32* %6
store i32 %17, i32* %11
store i32 0, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
%18 = call i32 @memset(%struct.stat* %12, i32 0, i32 4)
%19 = load i32, i32* %6
%20 = call i32 @fstat(i32 %19, %struct.stat* %12)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %62
25: ; preds = %4
%26 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISREG(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %62
33: ; preds = %25
%34 = load i32, i32* %11
%35 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%36 = call i32 @get_scsi_pci(i32 %34, i8* %35)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %10
store i32 %41, i32* %5
br label %62
42: ; preds = %33
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%44 = call i32 @sscanf(i8* %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %14, i32* %15, i32* %16)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp ne i32 %45, 3
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %5
br label %62
49: ; preds = %42
%50 = load i32, i32* %14
%51 = and i32 %50, 255
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %7
store i8 %52, i8* %53
%54 = load i32, i32* %15
%55 = and i32 %54, 255
%56 = trunc i32 %55 to i8
%57 = load i8*, i8** %8
store i8 %56, i8* %57
%58 = load i32, i32* %16
%59 = and i32 %58, 255
%60 = trunc i32 %59 to i8
%61 = load i8*, i8** %9
store i8 %60, i8* %61
store i32 0, i32* %5
br label %62
62: ; preds = %49, %47, %39, %30, %23
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.1 = [63 x i8] c"You must call this program with a file name such as /dev/sda.\0A\00"
@.str.2 = [13 x i8] c"get_scsi_pci\00"
@.str.3 = [9 x i8] c"%x:%x.%x\00"
@str = [14 x i8] c"sscanf failed\00"
define i32 @disk_get_scsi_pci(i32 %0, i8* nocapture writeonly %1, i8* nocapture writeonly %2, i8* nocapture writeonly %3) {
%5 = alloca %struct.stat
%6 = alloca [8 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = bitcast %struct.stat* %5 to i8*
%11 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%12 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%13 = bitcast i32* %8 to i8*
store i32 0, i32* %8
%14 = bitcast i32* %9 to i8*
store i32 0, i32* %9
%15 = call i32 @memset(%struct.stat* %5, i32 0, i32 4)
%16 = call i32 @fstat(i32 %0, %struct.stat* %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %45
20: ; preds = %4
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @S_ISREG(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
br label %45
28: ; preds = %20
%29 = call i32 @get_scsi_pci(i32 %0, i8* %11)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %45
33: ; preds = %28
%34 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %7, i32* %8, i32* %9)
%35 = icmp eq i32 %34, 3
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %45
38: ; preds = %33
%39 = load i32, i32* %7
%40 = trunc i32 %39 to i8
store i8 %40, i8* %1
%41 = load i32, i32* %8
%42 = trunc i32 %41 to i8
store i8 %42, i8* %2
%43 = load i32, i32* %9
%44 = trunc i32 %43 to i8
store i8 %44, i8* %3
br label %45
45: ; preds = %38, %36, %31, %25, %18
%46 = phi i32 [ 1, %18 ], [ 1, %25 ], [ %29, %31 ], [ 1, %36 ], [ 0, %38 ]
ret i32 %46
}