#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 raw_config_request {
  int block_major;
  int block_minor;
  int /*<<< orphan*/ raw_minor;
};
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ RAW_GETBIND;
scalar_t__ RAW_MAJOR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct raw_config_request *);
scalar_t__ major(int /*<<< orphan*/);
int /*<<< orphan*/ minor(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int fio_lookup_raw(dev_t dev, int *majdev, int *mindev) {
  struct raw_config_request rq;
  int fd;
  if (major(dev) != RAW_MAJOR)
    return 1;
  fd = open("/dev/rawctl", O_RDONLY);
  if (fd < 0) {
    fd = open("/dev/raw/rawctl", O_RDONLY);
    if (fd < 0)
      return 1;
  }
  rq.raw_minor = minor(dev);
  if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
    close(fd);
    return 1;
  }
  close(fd);
  *majdev = rq.block_major;
  *mindev = rq.block_minor;
  return 0;
}


x86 assembly (clang, -O0):
<fio_lookup_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <major@plt>
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
je     1187 <fio_lookup_raw+0x37>
movl   $0x1,-0x4(%rbp)
jmp    123b <fio_lookup_raw+0xeb>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11dc <fio_lookup_raw+0x8c>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d7 <fio_lookup_raw+0x87>
movl   $0x1,-0x4(%rbp)
jmp    123b <fio_lookup_raw+0xeb>
jmp    11dc <fio_lookup_raw+0x8c>
mov    -0x8(%rbp),%edi
call   1030 <minor@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    121a <fio_lookup_raw+0xca>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <fio_lookup_raw+0xeb>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fio_lookup_raw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
call   1040 <major@plt>
mov    0x2e6e(%rip),%rcx
mov    $0x1,%r12d
cmp    (%rcx),%rax
jne    11f0 <fio_lookup_raw+0xa0>
mov    0x2e64(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b0 <fio_lookup_raw+0x60>
mov    0x0(%r13),%esi
lea    0xe67(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11f0 <fio_lookup_raw+0xa0>
mov    %ebx,%edi
call   1030 <minor@plt>
mov    %eax,0x10(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
test   %rbx,%rbx
js     11f0 <fio_lookup_raw+0xa0>
mov    0x8(%rsp),%eax
mov    %eax,(%r15)
mov    0xc(%rsp),%eax
mov    %eax,(%r14)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.raw_config_request = type { i32, i32, i32 }
@RAW_MAJOR = global i64 0
@.str = [12 x i8] c"/dev/rawctl\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"/dev/raw/rawctl\00"
@RAW_GETBIND = global i32 0
define i32 @fio_lookup_raw(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.raw_config_request
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%10 = load i32, i32* %5
%11 = call i64 @major(i32 %10)
%12 = load i64, i64* @RAW_MAJOR
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 1, i32* %4
br label %47
15: ; preds = %3
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 1, i32* %4
br label %47
26: ; preds = %20
br label %27
27: ; preds = %26, %15
%28 = load i32, i32* %5
%29 = call i32 @minor(i32 %28)
%30 = getelementptr inbounds %struct.raw_config_request, %struct.raw_config_request* %8, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* %9
%32 = load i32, i32* @RAW_GETBIND
%33 = call i64 @ioctl(i32 %31, i32 %32, %struct.raw_config_request* %8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = load i32, i32* %9
%37 = call i32 @close(i32 %36)
store i32 1, i32* %4
br label %47
38: ; preds = %27
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds %struct.raw_config_request, %struct.raw_config_request* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32*, i32** %6
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.raw_config_request, %struct.raw_config_request* %8, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32*, i32** %7
store i32 %45, i32* %46
store i32 0, i32* %4
br label %47
47: ; preds = %38, %35, %25, %14
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.raw_config_request = type { i32, i32, i32 }
@RAW_MAJOR = global i64 0
@.str = [12 x i8] c"/dev/rawctl\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"/dev/raw/rawctl\00"
@RAW_GETBIND = global i32 0
define i32 @fio_lookup_raw(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.raw_config_request
%5 = bitcast %struct.raw_config_request* %4 to i8*
%6 = tail call i64 @major(i32 %0)
%7 = load i64, i64* @RAW_MAJOR
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %30
9: ; preds = %3
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %13, %9
%18 = phi i32 [ %15, %13 ], [ %11, %9 ]
%19 = tail call i32 @minor(i32 %0)
%20 = getelementptr inbounds %struct.raw_config_request, %struct.raw_config_request* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @RAW_GETBIND
%22 = call i64 @ioctl(i32 %18, i32 %21, %struct.raw_config_request* %4)
%23 = icmp slt i64 %22, 0
%24 = call i32 @close(i32 %18)
br i1 %23, label %30, label %25
25: ; preds = %17
%26 = getelementptr inbounds %struct.raw_config_request, %struct.raw_config_request* %4, i64 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %1
%28 = getelementptr inbounds %struct.raw_config_request, %struct.raw_config_request* %4, i64 0, i32 1
%29 = load i32, i32* %28
store i32 %29, i32* %2
br label %30
30: ; preds = %17, %13, %3, %25
%31 = phi i32 [ 0, %25 ], [ 1, %3 ], [ 1, %13 ], [ 1, %17 ]
ret i32 %31
}