#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 /*<<< orphan*/ acc_device_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ acc_device_host;
int acc_get_device_num(int /*<<< orphan*/);
int acc_get_num_devices(int /*<<< orphan*/);
int /*<<< orphan*/ acc_init(int /*<<< orphan*/);
int /*<<< orphan*/ acc_set_device_num(int, int /*<<< orphan*/);
int /*<<< orphan*/ acc_shutdown(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int i;
  int num_devices;
  int devnum;
  acc_device_t devtype = acc_device_host;
  num_devices = acc_get_num_devices(devtype);
  if (num_devices == 0)
    return 0;
  acc_init(devtype);
  for (i = 0; i < num_devices; i++) {
    acc_set_device_num(i, devtype);
    devnum = acc_get_device_num(devtype);
    if (devnum != i)
      abort();
  }
  acc_shutdown(devtype);
  num_devices = acc_get_num_devices(devtype);
  if (num_devices == 0)
    abort();
  for (i = 0; i < num_devices; i++) {
    acc_set_device_num(i, devtype);
    devnum = acc_get_device_num(devtype);
    if (devnum != i)
      abort();
  }
  acc_shutdown(devtype);
  acc_init(devtype);
  acc_set_device_num(0, devtype);
  devnum = acc_get_device_num(devtype);
  if (devnum != 0)
    abort();
  if (num_devices > 1) {
    acc_set_device_num(1, (acc_device_t)0);
    devnum = acc_get_device_num(devtype);
    if (devnum != 0)
      abort();
  }
  acc_shutdown(devtype);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <acc_get_num_devices@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11a3 <main+0x43>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <main+0x189>
mov    -0x20(%rbp),%edi
call   1060 <acc_init@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11fa <main+0x9a>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <acc_set_device_num@plt>
mov    -0x20(%rbp),%edi
call   1040 <acc_get_device_num@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11e7 <main+0x87>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11ec <main+0x8c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b2 <main+0x52>
mov    -0x20(%rbp),%edi
call   1050 <acc_shutdown@plt>
mov    -0x20(%rbp),%edi
call   1080 <acc_get_num_devices@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    121e <main+0xbe>
mov    $0x0,%al
call   1030 <abort@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    126d <main+0x10d>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <acc_set_device_num@plt>
mov    -0x20(%rbp),%edi
call   1040 <acc_get_device_num@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     125a <main+0xfa>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    125f <main+0xff>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1225 <main+0xc5>
mov    -0x20(%rbp),%edi
call   1050 <acc_shutdown@plt>
mov    -0x20(%rbp),%edi
call   1060 <acc_init@plt>
mov    -0x20(%rbp),%esi
xor    %edi,%edi
call   1070 <acc_set_device_num@plt>
mov    -0x20(%rbp),%edi
call   1040 <acc_get_device_num@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12a3 <main+0x143>
mov    $0x0,%al
call   1030 <abort@plt>
cmpl   $0x1,-0x18(%rbp)
jle    12da <main+0x17a>
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <acc_set_device_num@plt>
mov    -0x20(%rbp),%edi
call   1040 <acc_get_device_num@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12d5 <main+0x175>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    12da <main+0x17a>
mov    -0x20(%rbp),%edi
call   1050 <acc_shutdown@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e75(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <acc_get_num_devices@plt>
test   %eax,%eax
je     1232 <main+0xd2>
mov    %eax,%r14d
mov    %ebx,%edi
call   1060 <acc_init@plt>
test   %r14d,%r14d
jle    11b0 <main+0x50>
xor    %ebp,%ebp
nopl   (%rax)
mov    %ebp,%edi
mov    %ebx,%esi
call   1070 <acc_set_device_num@plt>
mov    %ebx,%edi
call   1040 <acc_get_device_num@plt>
cmp    %eax,%ebp
jne    1239 <main+0xd9>
add    $0x1,%ebp
cmp    %ebp,%r14d
jne    1190 <main+0x30>
mov    %ebx,%edi
call   1050 <acc_shutdown@plt>
mov    %ebx,%edi
call   1080 <acc_get_num_devices@plt>
test   %eax,%eax
je     1239 <main+0xd9>
mov    %eax,%r14d
jle    11ec <main+0x8c>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %ebx,%esi
call   1070 <acc_set_device_num@plt>
mov    %ebx,%edi
call   1040 <acc_get_device_num@plt>
cmp    %eax,%ebp
jne    1239 <main+0xd9>
add    $0x1,%ebp
cmp    %ebp,%r14d
jne    11d0 <main+0x70>
mov    %ebx,%edi
call   1050 <acc_shutdown@plt>
mov    %ebx,%edi
call   1060 <acc_init@plt>
xor    %edi,%edi
mov    %ebx,%esi
call   1070 <acc_set_device_num@plt>
mov    %ebx,%edi
call   1040 <acc_get_device_num@plt>
test   %eax,%eax
jne    1239 <main+0xd9>
cmp    $0x2,%r14d
jl     122b <main+0xcb>
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <acc_set_device_num@plt>
mov    %ebx,%edi
call   1040 <acc_get_device_num@plt>
test   %eax,%eax
jne    1239 <main+0xd9>
mov    %ebx,%edi
call   1050 <acc_shutdown@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@acc_device_host = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%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_host
store i32 %10, i32* %9
%11 = load i32, i32* %9
%12 = call i32 @acc_get_num_devices(i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %95
16: ; preds = %2
%17 = load i32, i32* %9
%18 = call i32 @acc_init(i32 %17)
store i32 0, i32* %6
br label %19
19: ; preds = %35, %16
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %38
23: ; preds = %19
%24 = load i32, i32* %6
%25 = load i32, i32* %9
%26 = call i32 @acc_set_device_num(i32 %24, i32 %25)
%27 = load i32, i32* %9
%28 = call i32 @acc_get_device_num(i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = load i32, i32* %6
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %23
br label %35
35: ; preds = %34
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %19
38: ; preds = %19
%39 = load i32, i32* %9
%40 = call i32 @acc_shutdown(i32 %39)
%41 = load i32, i32* %9
%42 = call i32 @acc_get_num_devices(i32 %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %38
store i32 0, i32* %6
br label %48
48: ; preds = %64, %47
%49 = load i32, i32* %6
%50 = load i32, i32* %7
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %67
52: ; preds = %48
%53 = load i32, i32* %6
%54 = load i32, i32* %9
%55 = call i32 @acc_set_device_num(i32 %53, i32 %54)
%56 = load i32, i32* %9
%57 = call i32 @acc_get_device_num(i32 %56)
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = load i32, i32* %6
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %52
%62 = call i32 (...) @abort()
unreachable
63: ; preds = %52
br label %64
64: ; preds = %63
%65 = load i32, i32* %6
%66 = add nsw i32 %65, 1
store i32 %66, i32* %6
br label %48
67: ; preds = %48
%68 = load i32, i32* %9
%69 = call i32 @acc_shutdown(i32 %68)
%70 = load i32, i32* %9
%71 = call i32 @acc_init(i32 %70)
%72 = load i32, i32* %9
%73 = call i32 @acc_set_device_num(i32 0, i32 %72)
%74 = load i32, i32* %9
%75 = call i32 @acc_get_device_num(i32 %74)
store i32 %75, i32* %8
%76 = load i32, i32* %8
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %67
%79 = call i32 (...) @abort()
unreachable
80: ; preds = %67
%81 = load i32, i32* %7
%82 = icmp sgt i32 %81, 1
br i1 %82, label %83, label %92
83: ; preds = %80
%84 = call i32 @acc_set_device_num(i32 1, i32 0)
%85 = load i32, i32* %9
%86 = call i32 @acc_get_device_num(i32 %85)
store i32 %86, i32* %8
%87 = load i32, i32* %8
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %83
%90 = call i32 (...) @abort()
unreachable
91: ; preds = %83
br label %92
92: ; preds = %91, %80
%93 = load i32, i32* %9
%94 = call i32 @acc_shutdown(i32 %93)
store i32 0, i32* %3
br label %95
95: ; preds = %92, %15
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@acc_device_host = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @acc_device_host
%4 = tail call i32 @acc_get_num_devices(i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %55, label %6
6: ; preds = %2
%7 = tail call i32 @acc_init(i32 %3)
%8 = icmp sgt i32 %4, 0
br i1 %8, label %11, label %19
9: ; preds = %11
%10 = icmp eq i32 %16, %4
br i1 %10, label %19, label %11
11: ; preds = %6, %9
%12 = phi i32 [ %16, %9 ], [ 0, %6 ]
%13 = tail call i32 @acc_set_device_num(i32 %12, i32 %3)
%14 = tail call i32 @acc_get_device_num(i32 %3)
%15 = icmp eq i32 %14, %12
%16 = add nuw nsw i32 %12, 1
br i1 %15, label %9, label %17
17: ; preds = %11
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %9, %6
%20 = tail call i32 @acc_shutdown(i32 %3)
%21 = tail call i32 @acc_get_num_devices(i32 %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = icmp sgt i32 %21, 0
br i1 %24, label %29, label %37
25: ; preds = %19
%26 = tail call i32 (...) @abort()
unreachable
27: ; preds = %29
%28 = icmp eq i32 %34, %21
br i1 %28, label %37, label %29
29: ; preds = %23, %27
%30 = phi i32 [ %34, %27 ], [ 0, %23 ]
%31 = tail call i32 @acc_set_device_num(i32 %30, i32 %3)
%32 = tail call i32 @acc_get_device_num(i32 %3)
%33 = icmp eq i32 %32, %30
%34 = add nuw nsw i32 %30, 1
br i1 %33, label %27, label %35
35: ; preds = %29
%36 = tail call i32 (...) @abort()
unreachable
37: ; preds = %27, %23
%38 = tail call i32 @acc_shutdown(i32 %3)
%39 = tail call i32 @acc_init(i32 %3)
%40 = tail call i32 @acc_set_device_num(i32 0, i32 %3)
%41 = tail call i32 @acc_get_device_num(i32 %3)
%42 = icmp eq i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %37
%44 = tail call i32 (...) @abort()
unreachable
45: ; preds = %37
%46 = icmp sgt i32 %21, 1
br i1 %46, label %47, label %53
47: ; preds = %45
%48 = tail call i32 @acc_set_device_num(i32 1, i32 0)
%49 = tail call i32 @acc_get_device_num(i32 %3)
%50 = icmp eq i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = tail call i32 (...) @abort()
unreachable
53: ; preds = %47, %45
%54 = tail call i32 @acc_shutdown(i32 %3)
br label %55
55: ; preds = %2, %53
ret i32 0
}