#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 acc_device_t;

/* Variables and functions */
int acc_device_nvidia;
int acc_get_device_num(int);
int acc_get_device_type();
int acc_get_num_devices(int);
int /*<<< orphan*/ acc_set_device_num(int, int);
int /*<<< orphan*/ acc_set_device_type(int);
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char **argv) {
  acc_device_t type = acc_device_nvidia;
  const char *type_name = "acc_device_nvidia";
  int naccelerators = acc_get_num_devices(type);
  printf("This OpenACC installation has %d accelerators of type %s(%d)\n",
         naccelerators, type_name, type);
  for (int i = 0; i < naccelerators; i++) {
    acc_set_device_num(i, type);
    printf("Switched to accelerator %d, device type=%d\n",
           acc_get_device_num(type), acc_get_device_type());
  }
  acc_set_device_type(type);
  printf("Using acc_set_device_type put us on accelerator %d, type %d\n",
         acc_get_device_num(type), acc_get_device_type());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <acc_get_num_devices@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1201 <main+0xa1>
mov    -0x28(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <acc_set_device_num@plt>
mov    -0x14(%rbp),%edi
call   1050 <acc_get_device_num@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <acc_get_device_type@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b7 <main+0x57>
mov    -0x14(%rbp),%edi
call   1060 <acc_set_device_type@plt>
mov    -0x14(%rbp),%edi
call   1050 <acc_get_device_num@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <acc_get_device_type@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,%edi
call   1080 <acc_get_num_devices@plt>
mov    %eax,%r15d
lea    0xe8e(%rip),%rdi
lea    0xe75(%rip),%rdx
mov    %eax,%esi
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
test   %r15d,%r15d
jle    11e1 <main+0x81>
lea    0xead(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r12d,%esi
call   1070 <acc_set_device_num@plt>
mov    %r12d,%edi
call   1050 <acc_get_device_num@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <acc_get_device_type@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    11b0 <main+0x50>
mov    %r12d,%edi
call   1060 <acc_set_device_type@plt>
mov    %r12d,%edi
call   1050 <acc_get_device_num@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <acc_get_device_type@plt>
lea    0xe7b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@acc_device_nvidia = global i32 0
@.str = [18 x i8] c"acc_device_nvidia\00"
@.str.1 = [62 x i8] c"This OpenACC installation has %d accelerators of type %s(%d)\0A\00"
@.str.2 = [44 x i8] c"Switched to accelerator %d, device type=%d\0A\00"
@.str.3 = [61 x i8] c"Using acc_set_device_type put us on accelerator %d, type %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @acc_device_nvidia
store i32 %10, i32* %6
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i32, i32* %6
%12 = call i32 @acc_get_num_devices(i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i8*, i8** %7
%15 = load i32, i32* %6
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %14, i32 %15)
store i32 0, i32* %9
br label %17
17: ; preds = %29, %2
%18 = load i32, i32* %9
%19 = load i32, i32* %8
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i32, i32* %9
%23 = load i32, i32* %6
%24 = call i32 @acc_set_device_num(i32 %22, i32 %23)
%25 = load i32, i32* %6
%26 = call i32 @acc_get_device_num(i32 %25)
%27 = call i32 (...) @acc_get_device_type()
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %21
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %17
32: ; preds = %17
%33 = load i32, i32* %6
%34 = call i32 @acc_set_device_type(i32 %33)
%35 = load i32, i32* %6
%36 = call i32 @acc_get_device_num(i32 %35)
%37 = call i32 (...) @acc_get_device_type()
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %37)
ret i32 0
}

LLVM IR (-O2):
@acc_device_nvidia = global i32 0
@.str = [18 x i8] c"acc_device_nvidia\00"
@.str.1 = [62 x i8] c"This OpenACC installation has %d accelerators of type %s(%d)\0A\00"
@.str.2 = [44 x i8] c"Switched to accelerator %d, device type=%d\0A\00"
@.str.3 = [61 x i8] c"Using acc_set_device_type put us on accelerator %d, type %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @acc_device_nvidia
%4 = tail call i32 @acc_get_num_devices(i32 %3)
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = icmp sgt i32 %4, 0
br i1 %6, label %12, label %7
7: ; preds = %12, %2
%8 = tail call i32 @acc_set_device_type(i32 %3)
%9 = tail call i32 @acc_get_device_num(i32 %3)
%10 = tail call i32 (...) @acc_get_device_type()
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 %10)
ret i32 0
12: ; preds = %2, %12
%13 = phi i32 [ %18, %12 ], [ 0, %2 ]
%14 = tail call i32 @acc_set_device_num(i32 %13, i32 %3)
%15 = tail call i32 @acc_get_device_num(i32 %3)
%16 = tail call i32 (...) @acc_get_device_type()
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16)
%18 = add nuw nsw i32 %13, 1
%19 = icmp eq i32 %18, %4
br i1 %19, label %7, label %12
}