#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_max_threads();
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  int MaxThreadsL1 = -1, MaxThreadsL2 = -1;
#pragma omp declare reduction(unique:int                                       \
                              : omp_out = (omp_in == 1 ? omp_in : omp_out))    \
    initializer(omp_priv = -1)
#pragma omp target teams map(MaxThreadsL1, MaxThreadsL2) thread_limit(32)      \
    num_teams(1)
  {
    MaxThreadsL1 = omp_get_max_threads();
#pragma omp parallel reduction(unique : MaxThreadsL2)
    { MaxThreadsL2 = omp_get_max_threads(); }
  }
  printf("Non-SPMD MaxThreadsL1 = %d\n", MaxThreadsL1);
  printf("Non-SPMD MaxThreadsL2 = %d\n", MaxThreadsL2);
  MaxThreadsL2 = -1;
#pragma omp target parallel reduction(unique : MaxThreadsL2)
  { MaxThreadsL2 = omp_get_max_threads(); }
  printf("SPMD with full runtime MaxThreadsL2 = %d\n", MaxThreadsL2);
  MaxThreadsL2 = -1;
#pragma omp target parallel for reduction(unique : MaxThreadsL2)
  for (int I = 0; I < 2; ++I) {
    MaxThreadsL2 = omp_get_max_threads();
  }
  printf("SPMD without runtime MaxThreadsL2 = %d\n", MaxThreadsL2);
  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)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    $0x0,%al
call   1040 <omp_get_max_threads@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <omp_get_max_threads@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe9e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xeab(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    $0x0,%al
call   1040 <omp_get_max_threads@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xea7(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    11c6 <main+0xa6>
mov    $0x0,%al
call   1040 <omp_get_max_threads@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a4 <main+0x84>
mov    -0x18(%rbp),%esi
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <omp_get_max_threads@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <omp_get_max_threads@plt>
mov    %eax,%ebp
lea    0xec4(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
lea    0xed2(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <omp_get_max_threads@plt>
lea    0xed9(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <omp_get_max_threads@plt>
xor    %eax,%eax
call   1040 <omp_get_max_threads@plt>
lea    0xee7(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Non-SPMD MaxThreadsL1 = %d\0A\00"
@.str.1 = [28 x i8] c"Non-SPMD MaxThreadsL2 = %d\0A\00"
@.str.2 = [42 x i8] c"SPMD with full runtime MaxThreadsL2 = %d\0A\00"
@.str.3 = [40 x i8] c"SPMD without runtime MaxThreadsL2 = %d\0A\00"
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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
%9 = call i32 (...) @omp_get_max_threads()
store i32 %9, i32* %6
%10 = call i32 (...) @omp_get_max_threads()
store i32 %10, i32* %7
%11 = load i32, i32* %6
%12 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %7
%14 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 -1, i32* %7
%15 = call i32 (...) @omp_get_max_threads()
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 -1, i32* %7
store i32 0, i32* %8
br label %18
18: ; preds = %23, %2
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = call i32 (...) @omp_get_max_threads()
store i32 %22, i32* %7
br label %23
23: ; preds = %21
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
br label %18
26: ; preds = %18
%27 = load i32, i32* %7
%28 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %27)
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"Non-SPMD MaxThreadsL1 = %d\0A\00"
@.str.1 = [28 x i8] c"Non-SPMD MaxThreadsL2 = %d\0A\00"
@.str.2 = [42 x i8] c"SPMD with full runtime MaxThreadsL2 = %d\0A\00"
@.str.3 = [40 x i8] c"SPMD without runtime MaxThreadsL2 = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @omp_get_max_threads()
%4 = tail call i32 (...) @omp_get_max_threads()
%5 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%7 = tail call i32 (...) @omp_get_max_threads()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = tail call i32 (...) @omp_get_max_threads()
%10 = tail call i32 (...) @omp_get_max_threads()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %10)
ret i32 0
}