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

/* Variables and functions */
int PTHREAD_KEYS_MAX;
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ spawn_thread();
int main(void) {
  pthread_t tid[(PTHREAD_KEYS_MAX + 1)];
  int i = 0;
  for (i = 0; i < (PTHREAD_KEYS_MAX + 1); i++) {
    tid[i] = spawn_thread();
  }
  for (i = 0; i < (PTHREAD_KEYS_MAX + 1); i++) {
    pthread_join(tid[i], NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11a6 <main+0x86>
mov    $0x0,%al
call   1030 <spawn_thread@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116d <main+0x4d>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11e6 <main+0xc6>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_join@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ad <main+0x8d>
movl   $0x0,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2eaf(%rip),%r14
mov    (%r14),%eax
lea    0x1(%rax),%ecx
mov    %rsp,%r15
lea    0xf(,%rcx,4),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
test   %eax,%eax
js     11a8 <main+0x88>
mov    $0xffffffffffffffff,%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <spawn_thread@plt>
mov    %eax,0x4(%r15,%rbx,4)
movslq (%r14),%rax
add    $0x1,%rbx
cmp    %rax,%rbx
jl     1160 <main+0x40>
test   %eax,%eax
js     11a8 <main+0x88>
mov    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x4(%r15,%rbx,4),%edi
xor    %esi,%esi
call   1040 <pthread_join@plt>
movslq (%r14),%rax
add    $0x1,%rbx
cmp    %rax,%rbx
jl     1190 <main+0x70>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PTHREAD_KEYS_MAX = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @PTHREAD_KEYS_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %2
%9 = alloca i32, i64 %7
store i64 %7, i64* %3
store i32 0, i32* %4
store i32 0, i32* %4
br label %10
10: ; preds = %20, %0
%11 = load i32, i32* %4
%12 = load i32, i32* @PTHREAD_KEYS_MAX
%13 = add nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %23
15: ; preds = %10
%16 = call i32 (...) @spawn_thread()
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %9, i64 %18
store i32 %16, i32* %19
br label %20
20: ; preds = %15
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %10
23: ; preds = %10
store i32 0, i32* %4
br label %24
24: ; preds = %35, %23
%25 = load i32, i32* %4
%26 = load i32, i32* @PTHREAD_KEYS_MAX
%27 = add nsw i32 %26, 1
%28 = icmp slt i32 %25, %27
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %9, i64 %31
%33 = load i32, i32* %32
%34 = call i32 @pthread_join(i32 %33, i32* null)
br label %35
35: ; preds = %29
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
store i32 %37, i32* %4
br label %24
38: ; preds = %24
store i32 0, i32* %1
%39 = load i8*, i8** %2
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@PTHREAD_KEYS_MAX = global i32 0
define i32 @main() {
%1 = load i32, i32* @PTHREAD_KEYS_MAX
%2 = add nsw i32 %1, 1
%3 = zext i32 %2 to i64
%4 = alloca i32, i64 %3
%5 = icmp slt i32 %1, 0
br i1 %5, label %25, label %8
6: ; preds = %8
%7 = icmp slt i32 %13, 0
br i1 %7, label %25, label %16
8: ; preds = %0, %8
%9 = phi i64 [ %12, %8 ], [ 0, %0 ]
%10 = tail call i32 (...) @spawn_thread()
%11 = getelementptr inbounds i32, i32* %4, i64 %9
store i32 %10, i32* %11
%12 = add nuw nsw i64 %9, 1
%13 = load i32, i32* @PTHREAD_KEYS_MAX
%14 = sext i32 %13 to i64
%15 = icmp slt i64 %9, %14
br i1 %15, label %8, label %6
16: ; preds = %6, %16
%17 = phi i64 [ %21, %16 ], [ 0, %6 ]
%18 = getelementptr inbounds i32, i32* %4, i64 %17
%19 = load i32, i32* %18
%20 = tail call i32 @pthread_join(i32 %19, i32* null)
%21 = add nuw nsw i64 %17, 1
%22 = load i32, i32* @PTHREAD_KEYS_MAX
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %17, %23
br i1 %24, label %16, label %25
25: ; preds = %16, %0, %6
ret i32 0
}