#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;
scalar_t__ check_vgem(int);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
int open_vgem(void) {
  int i, fd;
  const char *drmstr = "/dev/dri/card";
  fd = -1;
  for (i = 0; i < 16; i++) {
    char name[80];
    sprintf(name, "%s%u", drmstr, i);
    fd = open(name, O_RDWR);
    if (fd < 0)
      continue;
    if (check_vgem(fd)) {
      close(fd);
      continue;
    } else {
      break;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_vgem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    0xeb1(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x10,-0x4(%rbp)
jge    11d8 <open_vgem+0x98>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x4(%rbp),%ecx
lea    0xe91(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a6 <open_vgem+0x66>
jmp    11ca <open_vgem+0x8a>
mov    -0x8(%rbp),%edi
call   1040 <check_vgem@plt>
cmp    $0x0,%rax
je     11c5 <open_vgem+0x85>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
jmp    11ca <open_vgem+0x8a>
jmp    11d8 <open_vgem+0x98>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1161 <open_vgem+0x21>
mov    -0x8(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_vgem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
xor    %ebx,%ebx
lea    0xeb7(%rip),%r14
lea    0xea2(%rip),%r15
mov    %rsp,%r12
mov    0x2e80(%rip),%r13
jmp    117f <open_vgem+0x3f>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    $0x10,%ebx
je     11ad <open_vgem+0x6d>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
call   1060 <sprintf@plt>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1177 <open_vgem+0x37>
mov    %ebp,%edi
call   1040 <check_vgem@plt>
test   %rax,%rax
jne    1170 <open_vgem+0x30>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/dri/card\00"
@.str.1 = [5 x i8] c"%s%u\00"
@O_RDWR = global i32 0
define i32 @open_vgem() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [80 x i8]
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %3
store i32 -1, i32* %2
store i32 0, i32* %1
br label %5
5: ; preds = %27, %0
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 16
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = load i32, i32* %1
%12 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 %11)
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %8
br label %27
19: ; preds = %8
%20 = load i32, i32* %2
%21 = call i64 @check_vgem(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
br label %27
26: ; preds = %19
br label %30
27: ; preds = %23, %18
%28 = load i32, i32* %1
%29 = add nsw i32 %28, 1
store i32 %29, i32* %1
br label %5
30: ; preds = %26, %5
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/dri/card\00"
@.str.1 = [5 x i8] c"%s%u\00"
@O_RDWR = global i32 0
define i32 @open_vgem() {
%1 = alloca [80 x i8]
%2 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %15
%4 = phi i32 [ 0, %0 ], [ %16, %15 ]
%5 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %2, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = tail call i64 @check_vgem(i32 %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @close(i32 %7)
br label %15
14: ; preds = %9
br label %18
15: ; preds = %12, %3
%16 = add nuw nsw i32 %4, 1
%17 = icmp eq i32 %16, 16
br i1 %17, label %18, label %3
18: ; preds = %15, %14
ret i32 %7
}