#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 omp_get_device_num();
int omp_get_initial_device();
int omp_get_num_devices();
int omp_get_num_teams();
int omp_get_num_threads();
int omp_get_team_num();
int omp_get_thread_num();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int num_devs = omp_get_num_devices();
  for (int device_num = 0; device_num < num_devs; device_num++) {
#pragma omp target device(device_num) nowait
#pragma omp teams num_teams(2) thread_limit(4)
#pragma omp parallel num_threads(2)
    {
      int num_threads = omp_get_num_threads();
      int num_teams = omp_get_num_teams();
      int num_devices = omp_get_num_devices();
      int thread_id = omp_get_thread_num();
      int team_id = omp_get_team_num();
      int device_id = omp_get_device_num();
      int total_threads = num_devices * num_teams * num_threads;
      int gthread_id = (device_id * num_teams * num_threads) +
                       (team_id * num_threads) + thread_id;
      printf("Hello OpenMP 5 from \n");
      printf(" Device num  %d of %d devices\n", device_id, num_devices);
      printf(" Team num    %d of %d teams  \n", team_id, num_teams);
      printf(" Thread num  %d of %d threads\n", thread_id, num_threads);
      printf(" Global thread %d of %d total threads\n", gthread_id,
             total_threads);
    };
  };
#pragma omp taskwait
  printf("The host device num is %d\n", omp_get_device_num());
  printf("The initial device num is %d\n", omp_get_initial_device());
  printf("The number of devices are %d\n", num_devs);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <omp_get_num_devices@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    127c <main+0xfc>
mov    $0x0,%al
call   1060 <omp_get_num_threads@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <omp_get_num_teams@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <omp_get_num_devices@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <omp_get_thread_num@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <omp_get_team_num@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <omp_get_device_num@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
imul   -0x14(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
imul   -0x14(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    -0x20(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    %ecx,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a0 <main+0x20>
mov    $0x0,%al
call   1030 <omp_get_device_num@plt>
mov    %eax,%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <omp_get_initial_device@plt>
mov    %eax,%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1090 <omp_get_num_devices@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
jle    128a <main+0xfa>
mov    0x8(%rsp),%eax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,0x14(%rsp)
xor    %eax,%eax
call   1070 <omp_get_num_threads@plt>
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   10b0 <omp_get_num_teams@plt>
mov    %eax,%r13d
xor    %eax,%eax
call   1090 <omp_get_num_devices@plt>
mov    %eax,%ebx
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1050 <omp_get_thread_num@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10a0 <omp_get_team_num@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <omp_get_device_num@plt>
mov    %eax,%r14d
mov    %r13d,%r15d
mov    0xc(%rsp),%eax
imul   %eax,%r15d
imul   %ebx,%r15d
mov    %r14d,%ebx
imul   %r13d,%ebx
add    %ebp,%ebx
imul   %eax,%ebx
add    %r12d,%ebx
lea    0xeb3(%rip),%rdi
call   1040 <puts@plt>
lea    0xdcc(%rip),%rdi
mov    %r14d,%esi
mov    0x10(%rsp),%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdd6(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xde2(%rip),%rdi
mov    %r12d,%esi
mov    0xc(%rsp),%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdec(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x14(%rsp),%eax
add    $0xffffffff,%eax
jne    11c0 <main+0x30>
xor    %eax,%eax
call   1030 <omp_get_device_num@plt>
lea    0xdec(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <omp_get_initial_device@plt>
lea    0xdf0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdfe(%rip),%rdi
mov    0x8(%rsp),%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Hello OpenMP 5 from \0A\00"
@.str.1 = [31 x i8] c" Device num %d of %d devices\0A\00"
@.str.2 = [31 x i8] c" Team num %d of %d teams \0A\00"
@.str.3 = [31 x i8] c" Thread num %d of %d threads\0A\00"
@.str.4 = [39 x i8] c" Global thread %d of %d total threads\0A\00"
@.str.5 = [27 x i8] c"The host device num is %d\0A\00"
@.str.6 = [30 x i8] c"The initial device num is %d\0A\00"
@.str.7 = [30 x i8] c"The number of devices are %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
%12 = call i32 (...) @omp_get_num_devices()
store i32 %12, i32* %2
store i32 0, i32* %3
br label %13
13: ; preds = %53, %0
%14 = load i32, i32* %3
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %56
17: ; preds = %13
%18 = call i32 (...) @omp_get_num_threads()
store i32 %18, i32* %4
%19 = call i32 (...) @omp_get_num_teams()
store i32 %19, i32* %5
%20 = call i32 (...) @omp_get_num_devices()
store i32 %20, i32* %6
%21 = call i32 (...) @omp_get_thread_num()
store i32 %21, i32* %7
%22 = call i32 (...) @omp_get_team_num()
store i32 %22, i32* %8
%23 = call i32 (...) @omp_get_device_num()
store i32 %23, i32* %9
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = mul nsw i32 %24, %25
%27 = load i32, i32* %4
%28 = mul nsw i32 %26, %27
store i32 %28, i32* %10
%29 = load i32, i32* %9
%30 = load i32, i32* %5
%31 = mul nsw i32 %29, %30
%32 = load i32, i32* %4
%33 = mul nsw i32 %31, %32
%34 = load i32, i32* %8
%35 = load i32, i32* %4
%36 = mul nsw i32 %34, %35
%37 = add nsw i32 %33, %36
%38 = load i32, i32* %7
%39 = add nsw i32 %37, %38
store i32 %39, i32* %11
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%41 = load i32, i32* %9
%42 = load i32, i32* %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i32, i32* %8
%45 = load i32, i32* %5
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
%47 = load i32, i32* %7
%48 = load i32, i32* %4
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 %48)
%50 = load i32, i32* %11
%51 = load i32, i32* %10
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %50, i32 %51)
br label %53
53: ; preds = %17
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 1
store i32 %55, i32* %3
br label %13
56: ; preds = %13
%57 = call i32 (...) @omp_get_device_num()
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = call i32 (...) @omp_get_initial_device()
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %2
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i32 %61)
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [31 x i8] c" Device num %d of %d devices\0A\00"
@.str.2 = [31 x i8] c" Team num %d of %d teams \0A\00"
@.str.3 = [31 x i8] c" Thread num %d of %d threads\0A\00"
@.str.4 = [39 x i8] c" Global thread %d of %d total threads\0A\00"
@.str.5 = [27 x i8] c"The host device num is %d\0A\00"
@.str.6 = [30 x i8] c"The initial device num is %d\0A\00"
@.str.7 = [30 x i8] c"The number of devices are %d\0A\00"
@str = [21 x i8] c"Hello OpenMP 5 from \00"
define i32 @main() {
%1 = tail call i32 (...) @omp_get_num_devices()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %9, label %3
3: ; preds = %9, %0
%4 = tail call i32 (...) @omp_get_device_num()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @omp_get_initial_device()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i32 %1)
ret i32 0
9: ; preds = %0, %9
%10 = phi i32 [ %28, %9 ], [ 0, %0 ]
%11 = tail call i32 (...) @omp_get_num_threads()
%12 = tail call i32 (...) @omp_get_num_teams()
%13 = tail call i32 (...) @omp_get_num_devices()
%14 = tail call i32 (...) @omp_get_thread_num()
%15 = tail call i32 (...) @omp_get_team_num()
%16 = tail call i32 (...) @omp_get_device_num()
%17 = mul i32 %12, %11
%18 = mul i32 %17, %13
%19 = mul nsw i32 %16, %12
%20 = add i32 %19, %15
%21 = mul i32 %20, %11
%22 = add nsw i32 %21, %14
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %13)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %12)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %11)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %22, i32 %18)
%28 = add nuw nsw i32 %10, 1
%29 = icmp eq i32 %28, %1
br i1 %29, label %3, label %9
}