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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ SCSI_IOCTL_GET_IDLUN;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int);
int get_id_lun(const char *dev) {
  int fd;
  int idlun[2];
  if ((fd = open(dev, O_RDONLY | O_NONBLOCK)) < 0) {
    return -1;
  }
  if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  return idlun[0];
}


x86 assembly (clang, -O0):
<get_id_lun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    116f <get_id_lun+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <get_id_lun+0x80>
mov    -0x14(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11a2 <get_id_lun+0x72>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <get_id_lun+0x80>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_id_lun>:
push   %r14
push   %rbx
push   %rax
mov    0x2ea5(%rip),%rax
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     117d <get_id_lun+0x4d>
mov    %eax,%ebx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
test   %r14,%r14
js     1182 <get_id_lun+0x52>
mov    (%rsp),%eax
jmp    1182 <get_id_lun+0x52>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SCSI_IOCTL_GET_IDLUN = global i32 0
define i32 @get_id_lun(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [2 x i32]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %27
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @SCSI_IOCTL_GET_IDLUN
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%17 = call i64 @ioctl(i32 %14, i32 %15, i32* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %2
br label %27
22: ; preds = %13
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%26 = load i32, i32* %25
store i32 %26, i32* %2
br label %27
27: ; preds = %22, %19, %12
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SCSI_IOCTL_GET_IDLUN = global i32 0
define i32 @get_id_lun(i8* nocapture %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = load i32, i32* @SCSI_IOCTL_GET_IDLUN
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i64 @ioctl(i32 %7, i32 %10, i32* %11)
%13 = icmp slt i64 %12, 0
%14 = call i32 @close(i32 %7)
%15 = load i32, i32* %11
%16 = select i1 %13, i32 -1, i32 %15
br label %17
17: ; preds = %9, %1
%18 = phi i32 [ -1, %1 ], [ %16, %9 ]
ret i32 %18
}