#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 */
scalar_t__ checkCmdLineFlag(int, char const **, char *);
int /*<<< orphan*/ cudaDeviceReset();
int /*<<< orphan*/ cudaGLSetGLDevice(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int gpuGLDeviceInit(int, char **);
int gpuGetMaxGflopsDeviceId();
int /*<<< orphan*/ printf(char *);
int findCudaGLDevice(int argc, char **argv) {
  int devID = 0;
  if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
    devID = gpuGLDeviceInit(argc, argv);
    if (devID < 0) {
      printf("no CUDA capable devices found, exiting...\n");
      cudaDeviceReset();
      exit(0);
    }
  } else {
    devID = gpuGetMaxGflopsDeviceId();
    cudaGLSetGLDevice(devID);
  }
  return devID;
}


x86 assembly (clang, -O0):
<findCudaGLDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdx
call   1050 <checkCmdLineFlag@plt>
cmp    $0x0,%rax
je     11db <findCudaGLDevice+0x6b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <gpuGLDeviceInit@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d6 <findCudaGLDevice+0x66>
lea    0xe44(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1080 <cudaDeviceReset@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11ed <findCudaGLDevice+0x7d>
mov    $0x0,%al
call   1060 <gpuGetMaxGflopsDeviceId@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <cudaGLSetGLDevice@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findCudaGLDevice>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe81(%rip),%rdx
call   1060 <checkCmdLineFlag@plt>
test   %rax,%rax
je     11b3 <findCudaGLDevice+0x43>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <gpuGLDeviceInit@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c3 <findCudaGLDevice+0x53>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <cudaDeviceReset@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   1070 <gpuGetMaxGflopsDeviceId@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <cudaGLSetGLDevice@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"device\00"
@.str.1 = [43 x i8] c"no CUDA capable devices found, exiting...\0A\00"
define i32 @findCudaGLDevice(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%6 = load i32, i32* %3
%7 = load i8**, i8*** %4
%8 = call i64 @checkCmdLineFlag(i32 %6, i8** %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i32, i32* %3
%12 = load i8**, i8*** %4
%13 = call i32 @gpuGLDeviceInit(i32 %11, i8** %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 (...) @cudaDeviceReset()
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %10
br label %25
21: ; preds = %2
%22 = call i32 (...) @gpuGetMaxGflopsDeviceId()
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = call i32 @cudaGLSetGLDevice(i32 %23)
br label %25
25: ; preds = %21, %20
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
@.str = [7 x i8] c"device\00"
@str = [42 x i8] c"no CUDA capable devices found, exiting...\00"
define i32 @findCudaGLDevice(i32 %0, i8** %1) {
%3 = tail call i64 @checkCmdLineFlag(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = tail call i32 @gpuGLDeviceInit(i32 %0, i8** %1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 (...) @cudaDeviceReset()
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = tail call i32 (...) @gpuGetMaxGflopsDeviceId()
%14 = tail call i32 @cudaGLSetGLDevice(i32 %13)
br label %15
15: ; preds = %5, %12
%16 = phi i32 [ %6, %5 ], [ %13, %12 ]
ret i32 %16
}