#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
scalar_t__ stat(char *, struct stat *);
int __gdev_get_device_count(void) {
  char fname[256] = "/proc/gdev/virtual_device_count";
  char buf[16];
  int minor = 0;
  FILE *fp;
  int shmid;
  int *shm;
  if (!(fp = fopen(fname, "r"))) {
    struct stat st;
    for (;;) {
      sprintf(fname, "/dev/dri/card%d", minor);
      if (stat(fname, &st))
        break;
      minor++;
    }
    if (minor)
      return minor;
    for (;;) {
      sprintf(fname, "/dev/nvidia%d", minor);
      if (stat(fname, &st))
        break;
      minor++;
    }
    shmid =
        shmget(0x600dde11, sizeof(int), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    if (!shmid) {
      shm = (int *)shmat(shmid, NULL, 0);
      minor = *shm;
    }
    return minor;
  }
  if (!fgets(buf, 16, fp))
    sprintf(buf, "0");
  fclose(fp);
  sscanf(buf, "%d", &minor);
  return minor;
}


x86 assembly (clang, -O0):
<__gdev_get_device_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
lea    -0x110(%rbp),%rdi
lea    0xe57(%rip),%rsi
mov    $0x100,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x124(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xf35(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x130(%rbp)
cmp    $0x0,%rax
jne    1323 <__gdev_get_device_count+0x193>
jmp    11e6 <__gdev_get_device_count+0x56>
lea    -0x110(%rbp),%rdi
mov    -0x124(%rbp),%edx
lea    0xf08(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x148(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1223 <__gdev_get_device_count+0x93>
jmp    1237 <__gdev_get_device_count+0xa7>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11e6 <__gdev_get_device_count+0x56>
cmpl   $0x0,-0x124(%rbp)
je     1252 <__gdev_get_device_count+0xc2>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <__gdev_get_device_count+0x1f8>
jmp    1257 <__gdev_get_device_count+0xc7>
lea    -0x110(%rbp),%rdi
mov    -0x124(%rbp),%edx
lea    0xea7(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x148(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1294 <__gdev_get_device_count+0x104>
jmp    12a8 <__gdev_get_device_count+0x118>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1257 <__gdev_get_device_count+0xc7>
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
mov    0x2d28(%rip),%rax
or     (%rax),%edx
mov    0x2d0f(%rip),%rax
or     (%rax),%edx
mov    0x2d1e(%rip),%rax
or     (%rax),%edx
mov    $0x600dde11,%edi
mov    $0x4,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jne    1315 <__gdev_get_device_count+0x185>
mov    -0x134(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x140(%rbp)
mov    -0x140(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <__gdev_get_device_count+0x1f8>
lea    -0x120(%rbp),%rdi
mov    -0x130(%rbp),%rdx
mov    $0x10,%esi
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    1359 <__gdev_get_device_count+0x1c9>
lea    -0x120(%rbp),%rdi
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x130(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x120(%rbp),%rdi
lea    0xdaa(%rip),%rsi
lea    -0x124(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__gdev_get_device_count>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x128,%rsp
lea    0xe5b(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    $0x100,%edx
mov    %rbx,%rdi
call   1060 <memcpy@plt>
movl   $0x0,0xc(%rsp)
lea    0xf3a(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1226 <__gdev_get_device_count+0x96>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
jne    11f3 <__gdev_get_device_count+0x63>
movw   $0x30,0x10(%rsp)
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xf1b(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1070 <sscanf@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xed5(%rip),%rsi
xor    %ebx,%ebx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %edx,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1313 <__gdev_get_device_count+0x183>
lea    0xeb5(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %edx,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    12c6 <__gdev_get_device_count+0x136>
lea    0xe8b(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebx
mov    %ebx,0xc(%rsp)
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     12a0 <__gdev_get_device_count+0x110>
mov    0x2cfb(%rip),%rax
mov    0x2d0c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2cf1(%rip),%rax
or     (%rax),%edx
mov    0x2d00(%rip),%rax
or     (%rax),%edx
mov    $0x600dde11,%edi
mov    $0x4,%esi
call   10a0 <shmget@plt>
test   %eax,%eax
jne    1215 <__gdev_get_device_count+0x85>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    (%rax),%ebx
jmp    1215 <__gdev_get_device_count+0x85>
lea    0xde8(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1330 <__gdev_get_device_count+0x1a0>
jmp    1215 <__gdev_get_device_count+0x85>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.__gdev_get_device_count.fname = [256 x i8] c"/proc/gdev/virtual_device_count\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [16 x i8] c"/dev/dri/card%d\00"
@.str.2 = [14 x i8] c"/dev/nvidia%d\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @__gdev_get_device_count() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [16 x i8]
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = bitcast [256 x i8]* %2 to i8*
store i32 0, i32* %4
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %61, label %13
13: ; preds = %0
br label %14
14: ; preds = %22, %13
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%19 = call i64 @stat(i8* %18, %struct.stat* %8)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
br label %25
22: ; preds = %14
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %14
25: ; preds = %21
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* %4
store i32 %29, i32* %1
br label %75
30: ; preds = %25
br label %31
31: ; preds = %39, %30
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%33 = load i32, i32* %4
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%36 = call i64 @stat(i8* %35, %struct.stat* %8)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %31
br label %42
39: ; preds = %31
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %31
42: ; preds = %38
%43 = load i32, i32* @S_IRUSR
%44 = load i32, i32* @S_IWUSR
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IRGRP
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IWGRP
%49 = or i32 %47, %48
%50 = call i32 @shmget(i32 1611521553, i32 4, i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %42
%54 = load i32, i32* %6
%55 = call i64 @shmat(i32 %54, i32* null, i32 0)
%56 = inttoptr i64 %55 to i32*
store i32* %56, i32** %7
%57 = load i32*, i32** %7
%58 = load i32, i32* %57
store i32 %58, i32* %4
br label %59
59: ; preds = %53, %42
%60 = load i32, i32* %4
store i32 %60, i32* %1
br label %75
61: ; preds = %0
%62 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%63 = load i32*, i32** %5
%64 = call i32 @fgets(i8* %62, i32 16, i32* %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %61
%67 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%68 = call i32 (i8*, i8*, ...) @sprintf(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %69
69: ; preds = %66, %61
%70 = load i32*, i32** %5
%71 = call i32 @fclose(i32* %70)
%72 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%73 = call i32 @sscanf(i8* %72, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%74 = load i32, i32* %4
store i32 %74, i32* %1
br label %75
75: ; preds = %69, %59, %28
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.__gdev_get_device_count.fname = [256 x i8] c"/proc/gdev/virtual_device_count\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [16 x i8] c"/dev/dri/card%d\00"
@.str.2 = [14 x i8] c"/dev/nvidia%d\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.4 = [3 x i8] c"%d\00"
define i32 @__gdev_get_device_count() {
%1 = alloca [256 x i8]
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = alloca %struct.stat
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %48
10: ; preds = %0
%11 = bitcast %struct.stat* %4 to i8*
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 0)
%13 = call i64 @stat(i8* %5, %struct.stat* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %10, %15
%16 = phi i32 [ %17, %15 ], [ 0, %10 ]
%17 = add nuw nsw i32 %16, 1
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i64 @stat(i8* %5, %struct.stat* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %15, label %46
21: ; preds = %10
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0)
%23 = call i64 @stat(i8* %5, %struct.stat* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21, %25
%26 = phi i32 [ %27, %25 ], [ 0, %21 ]
%27 = add nuw nsw i32 %26, 1
store i32 %27, i32* %3
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i64 @stat(i8* %5, %struct.stat* %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %25, label %31
31: ; preds = %25, %21
%32 = phi i32 [ 0, %21 ], [ %27, %25 ]
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %34, %33
%36 = load i32, i32* @S_IRGRP
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IWGRP
%39 = or i32 %37, %38
%40 = call i32 @shmget(i32 1611521553, i32 4, i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %31
%43 = call i64 @shmat(i32 0, i32* null, i32 0)
%44 = inttoptr i64 %43 to i32*
%45 = load i32, i32* %44
br label %46
46: ; preds = %15, %31, %42
%47 = phi i32 [ %45, %42 ], [ %32, %31 ], [ %17, %15 ]
br label %57
48: ; preds = %0
%49 = call i32 @fgets(i8* %6, i32 16, i32* %8)
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = bitcast [16 x i8]* %2 to i16*
store i16 48, i16* %52
br label %53
53: ; preds = %51, %48
%54 = call i32 @fclose(i32* %8)
%55 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%56 = load i32, i32* %3
br label %57
57: ; preds = %53, %46
%58 = phi i32 [ %56, %53 ], [ %47, %46 ]
ret i32 %58
}