#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SG_GET_VERSION_NUM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int open_scsi_device(const char *dev) {
  int fd, vers;
  if ((fd = open(dev, O_RDWR)) < 0) {
    printf("ERROR could not open device %s\n", dev);
    return -1;
  }
  if ((ioctl(fd, SG_GET_VERSION_NUM, &vers) < 0) || (vers < 30000)) {
    printf("/dev is not an sg device, or old sg driver\n");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_scsi_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1188 <open_scsi_device+0x48>
mov    -0x10(%rbp),%rsi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <open_scsi_device+0x9c>
mov    -0x14(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     11b4 <open_scsi_device+0x74>
cmpl   $0x7530,-0x18(%rbp)
jge    11d6 <open_scsi_device+0x96>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <open_scsi_device+0x9c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_scsi_device>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11a4 <open_scsi_device+0x54>
mov    %eax,%ebx
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     118f <open_scsi_device+0x3f>
cmpl   $0x752f,0x4(%rsp)
jg     11ba <open_scsi_device+0x6a>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11b5 <open_scsi_device+0x65>
lea    0xe55(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [32 x i8] c"ERROR could not open device %s\0A\00"
@SG_GET_VERSION_NUM = global i32 0
@.str.1 = [44 x i8] c"/dev is not an sg device, or old sg driver\0A\00"
define i32 @open_scsi_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
br label %27
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @SG_GET_VERSION_NUM
%16 = call i64 @ioctl(i32 %14, i32 %15, i32* %5)
%17 = icmp slt i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 30000
br i1 %20, label %21, label %25
21: ; preds = %18, %13
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %27
25: ; preds = %18
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %21, %10
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [32 x i8] c"ERROR could not open device %s\0A\00"
@SG_GET_VERSION_NUM = global i32 0
@str = [43 x i8] c"/dev is not an sg device, or old sg driver\00"
define i32 @open_scsi_device(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %19
9: ; preds = %1
%10 = load i32, i32* @SG_GET_VERSION_NUM
%11 = call i64 @ioctl(i32 %5, i32 %10, i32* %2)
%12 = icmp slt i64 %11, 0
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 30000
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%18 = call i32 @close(i32 %5)
br label %19
19: ; preds = %9, %16, %7
%20 = phi i32 [ -1, %7 ], [ -1, %16 ], [ %5, %9 ]
ret i32 %20
}