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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int const);
scalar_t__ errno;
scalar_t__ kvss905c_detect(int const);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int kvss905c_open() {
  char devicename[64];
  devicename[63] = 0;
  for (unsigned i = 0;; ++i) {
    snprintf(devicename, sizeof(devicename) - 1, "/dev/sg%d", i);
    const int fd = open(devicename, O_RDWR);
    if (fd < 0) {
      if (errno == ENOENT)
        return -1;
      continue;
    }
    if (kvss905c_detect(fd) == 0)
      return fd;
    close(fd);
  }
}


x86 assembly (clang, -O0):
<kvss905c_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%ecx
mov    $0x3f,%esi
lea    0xe9a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    11b5 <kvss905c_open+0x75>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <kvss905c_open+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <kvss905c_open+0xa8>
jmp    11da <kvss905c_open+0x9a>
mov    -0x58(%rbp),%edi
call   1050 <kvss905c_detect@plt>
cmp    $0x0,%rax
jne    11d2 <kvss905c_open+0x92>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <kvss905c_open+0xa8>
mov    -0x58(%rbp),%edi
call   1040 <close@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1153 <kvss905c_open+0x13>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvss905c_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movb   $0x0,0x3f(%rsp)
xor    %ebx,%ebx
lea    0xea4(%rip),%r14
mov    %rsp,%r15
mov    0x2e82(%rip),%r12
mov    0x2e5b(%rip),%r13
jmp    1183 <kvss905c_open+0x43>
nop
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     11bc <kvss905c_open+0x7c>
add    $0x1,%ebx
mov    %r15,%rdi
mov    $0x3f,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1030 <snprintf@plt>
mov    (%r12),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1170 <kvss905c_open+0x30>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <kvss905c_detect@plt>
test   %rax,%rax
je     11c1 <kvss905c_open+0x81>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1180 <kvss905c_open+0x40>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/sg%d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @kvss905c_open() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 63
store i8 0, i8* %5
store i32 0, i32* %3
br label %6
6: ; preds = %30, %0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %6
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %1
br label %33
20: ; preds = %15
br label %30
21: ; preds = %6
%22 = load i32, i32* %4
%23 = call i64 @kvss905c_detect(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* %4
store i32 %26, i32* %1
br label %33
27: ; preds = %21
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %20
%31 = load i32, i32* %3
%32 = add i32 %31, 1
store i32 %32, i32* %3
br label %6
33: ; preds = %25, %19
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/sg%d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @kvss905c_open() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 63
store i8 0, i8* %3
br label %4
4: ; preds = %19, %0
%5 = phi i32 [ 0, %0 ], [ %20, %19 ]
%6 = call i32 @snprintf(i8* %2, i32 63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %2, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %21, label %19
14: ; preds = %4
%15 = call i64 @kvss905c_detect(i32 %8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = call i32 @close(i32 %8)
br label %19
19: ; preds = %17, %10
%20 = add i32 %5, 1
br label %4
21: ; preds = %10, %14
%22 = phi i32 [ %8, %14 ], [ -1, %10 ]
ret i32 %22
}