#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*/ check_vgem(int);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, 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];
    snprintf(name, 80, "%s%u", drmstr, i);
    fd = open(name, O_RDWR);
    if (fd < 0)
      continue;
    if (!check_vgem(fd)) {
      close(fd);
      fd = -1;
      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    11e4 <open_vgem+0xa4>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x4(%rbp),%r8d
mov    $0x50,%esi
lea    0xe8b(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ac <open_vgem+0x6c>
jmp    11d6 <open_vgem+0x96>
mov    -0x8(%rbp),%edi
call   1050 <check_vgem@plt>
cmp    $0x0,%eax
jne    11d1 <open_vgem+0x91>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    11d6 <open_vgem+0x96>
jmp    11e4 <open_vgem+0xa4>
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    1184 <open_vgem+0x44>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
add    $0x1,%ebx
cmp    $0x10,%ebx
je     11b7 <open_vgem+0x77>
mov    %r12,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1030 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     117c <open_vgem+0x3c>
mov    %ebp,%edi
call   1050 <check_vgem@plt>
test   %eax,%eax
je     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 @snprintf(i8* %9, i32 80, 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 i32 @check_vgem(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
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 ], [ %17, %15 ]
%5 = call i32 @snprintf(i8* %2, i32 80, i8* 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 = call i32 @check_vgem(i32 %7)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @close(i32 %7)
br label %15
14: ; preds = %9
br label %19
15: ; preds = %12, %3
%16 = phi i32 [ %7, %3 ], [ -1, %12 ]
%17 = add nuw nsw i32 %4, 1
%18 = icmp eq i32 %17, 16
br i1 %18, label %19, label %3
19: ; preds = %15, %14
%20 = phi i32 [ %7, %14 ], [ %16, %15 ]
ret i32 %20
}