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

/* Variables and functions */
int _POSIX_THREAD_KEYS_MAX;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ *alloca(int);
int /*<<< orphan*/ printf(char *, int);
void *pthread_getspecific(int /*<<< orphan*/);
scalar_t__ pthread_key_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_key_delete(int /*<<< orphan*/);
scalar_t__ pthread_setspecific(int /*<<< orphan*/, void const *);
int /*<<< orphan*/ write(int, char *, int);
int do_test(void) {
  int max;
  max = _POSIX_THREAD_KEYS_MAX;
  pthread_key_t *keys = alloca(max * sizeof(pthread_key_t));
  int i;
  for (i = 0; i < max; ++i)
    if (pthread_key_create(&keys[i], NULL) != 0) {
      write(2, "key_create failed\n", 18);
      _exit(1);
    } else {
      printf("created key %d\n", i);
      if (pthread_setspecific(keys[i], (const void *)(i + 100l)) != 0) {
        write(2, "setspecific failed\n", 19);
        _exit(1);
      }
    }
  for (i = 0; i < max; ++i) {
    if (pthread_getspecific(keys[i]) != (void *)(i + 100l)) {
      write(2, "getspecific failed\n", 19);
      _exit(1);
    }
    if (pthread_key_delete(keys[i]) != 0) {
      write(2, "key_delete failed\n", 18);
      _exit(1);
    }
  }
  if (pthread_key_create(&keys[0], NULL) != 0) {
    write(2, "2nd key_create failed\n", 22);
    _exit(1);
  }
  if (pthread_key_delete(keys[0]) != 0) {
    write(2, "2nd key_delete failed\n", 22);
    _exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <alloca@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1265 <do_test+0xe5>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <pthread_key_create@plt>
cmp    $0x0,%rax
je     11fc <do_test+0x7c>
mov    $0x2,%edi
lea    0xe54(%rip),%rsi
mov    $0x12,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x14(%rbp),%esi
lea    0xdfa(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
movslq -0x14(%rbp),%rsi
add    $0x64,%rsi
call   10a0 <pthread_setspecific@plt>
cmp    $0x0,%rax
je     124d <do_test+0xcd>
mov    $0x2,%edi
lea    0xdd7(%rip),%rsi
mov    $0x13,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    1252 <do_test+0xd2>
jmp    1257 <do_test+0xd7>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ae <do_test+0x2e>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1306 <do_test+0x186>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <pthread_getspecific@plt>
movslq -0x14(%rbp),%rcx
add    $0x64,%rcx
cmp    %rcx,%rax
je     12b9 <do_test+0x139>
mov    $0x2,%edi
lea    0xd7f(%rip),%rsi
mov    $0x13,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1080 <pthread_key_delete@plt>
cmp    $0x0,%rax
je     12f3 <do_test+0x173>
mov    $0x2,%edi
lea    0xd74(%rip),%rsi
mov    $0x12,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    12f8 <do_test+0x178>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    126c <do_test+0xec>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <pthread_key_create@plt>
cmp    $0x0,%rax
je     133d <do_test+0x1bd>
mov    $0x2,%edi
lea    0xd0f(%rip),%rsi
mov    $0x16,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1080 <pthread_key_delete@plt>
cmp    $0x0,%rax
je     1372 <do_test+0x1f2>
mov    $0x2,%edi
lea    0xcf1(%rip),%rsi
mov    $0x16,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%r13d
lea    0x0(,%r13,4),%edi
call   1090 <alloca@plt>
mov    %rax,%r14
test   %r13d,%r13d
jle    1246 <do_test+0xc6>
xor    %ebx,%ebx
lea    0xe4b(%rip),%r15
mov    %r14,%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <pthread_key_create@plt>
test   %rax,%rax
jne    126e <do_test+0xee>
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    (%r12),%edi
lea    0x64(%rbx),%rsi
call   10a0 <pthread_setspecific@plt>
test   %rax,%rax
jne    1277 <do_test+0xf7>
add    $0x1,%rbx
add    $0x4,%r12
cmp    %r13,%rbx
jne    11c0 <do_test+0x40>
test   %r13d,%r13d
jle    1246 <do_test+0xc6>
neg    %r13
mov    $0x64,%ebx
nopl   (%rax)
mov    -0x190(%r14,%rbx,4),%edi
ff
call   1040 <pthread_getspecific@plt>
cmp    %rbx,%rax
jne    1280 <do_test+0x100>
mov    -0x190(%r14,%rbx,4),%edi
ff
call   1080 <pthread_key_delete@plt>
test   %rax,%rax
jne    1293 <do_test+0x113>
lea    (%rbx,%r13,1),%rax
add    $0x1,%rax
add    $0x1,%rbx
cmp    $0x64,%rax
jne    1210 <do_test+0x90>
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <pthread_key_create@plt>
test   %rax,%rax
jne    12b3 <do_test+0x133>
mov    (%r14),%edi
call   1080 <pthread_key_delete@plt>
test   %rax,%rax
jne    12bc <do_test+0x13c>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdc7(%rip),%rsi
jmp    129a <do_test+0x11a>
lea    0xd92(%rip),%rsi
jmp    1287 <do_test+0x107>
lea    0xd9d(%rip),%rsi
mov    $0x2,%edi
mov    $0x13,%edx
jmp    12a4 <do_test+0x124>
lea    0xdb9(%rip),%rsi
mov    $0x2,%edi
mov    $0x12,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
lea    0xd7e(%rip),%rsi
jmp    12c3 <do_test+0x143>
lea    0xd8c(%rip),%rsi
mov    $0x2,%edi
mov    $0x16,%edx
jmp    12a4 <do_test+0x124>

LLVM IR (-O0):
@_POSIX_THREAD_KEYS_MAX = global i32 0
@.str = [19 x i8] c"key_create failed\0A\00"
@.str.1 = [16 x i8] c"created key %d\0A\00"
@.str.2 = [20 x i8] c"setspecific failed\0A\00"
@.str.3 = [20 x i8] c"getspecific failed\0A\00"
@.str.4 = [19 x i8] c"key_delete failed\0A\00"
@.str.5 = [23 x i8] c"2nd key_create failed\0A\00"
@.str.6 = [23 x i8] c"2nd key_delete failed\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = load i32, i32* @_POSIX_THREAD_KEYS_MAX
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = sext i32 %5 to i64
%7 = mul i64 %6, 4
%8 = trunc i64 %7 to i32
%9 = call i32* @alloca(i32 %8)
store i32* %9, i32** %2
store i32 0, i32* %3
br label %10
10: ; preds = %43, %0
%11 = load i32, i32* %3
%12 = load i32, i32* %1
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %46
14: ; preds = %10
%15 = load i32*, i32** %2
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %15, i64 %17
%19 = call i64 @pthread_key_create(i32* %18, i32* null)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 18)
%23 = call i32 @_exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i32, i32* %3
%26 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %2
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = add nsw i64 %33, 100
%35 = inttoptr i64 %34 to i8*
%36 = call i64 @pthread_setspecific(i32 %31, i8* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %24
%39 = call i32 @write(i32 2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 19)
%40 = call i32 @_exit(i32 1)
unreachable
41: ; preds = %24
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
store i32 %45, i32* %3
br label %10
46: ; preds = %10
store i32 0, i32* %3
br label %47
47: ; preds = %78, %46
%48 = load i32, i32* %3
%49 = load i32, i32* %1
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %81
51: ; preds = %47
%52 = load i32*, i32** %2
%53 = load i32, i32* %3
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = call i8* @pthread_getspecific(i32 %56)
%58 = load i32, i32* %3
%59 = sext i32 %58 to i64
%60 = add nsw i64 %59, 100
%61 = inttoptr i64 %60 to i8*
%62 = icmp ne i8* %57, %61
br i1 %62, label %63, label %66
63: ; preds = %51
%64 = call i32 @write(i32 2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 19)
%65 = call i32 @_exit(i32 1)
unreachable
66: ; preds = %51
%67 = load i32*, i32** %2
%68 = load i32, i32* %3
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i32, i32* %67, i64 %69
%71 = load i32, i32* %70
%72 = call i64 @pthread_key_delete(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %66
%75 = call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 18)
%76 = call i32 @_exit(i32 1)
unreachable
77: ; preds = %66
br label %78
78: ; preds = %77
%79 = load i32, i32* %3
%80 = add nsw i32 %79, 1
store i32 %80, i32* %3
br label %47
81: ; preds = %47
%82 = load i32*, i32** %2
%83 = getelementptr inbounds i32, i32* %82, i64 0
%84 = call i64 @pthread_key_create(i32* %83, i32* null)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %81
%87 = call i32 @write(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 22)
%88 = call i32 @_exit(i32 1)
unreachable
89: ; preds = %81
%90 = load i32*, i32** %2
%91 = getelementptr inbounds i32, i32* %90, i64 0
%92 = load i32, i32* %91
%93 = call i64 @pthread_key_delete(i32 %92)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %89
%96 = call i32 @write(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 22)
%97 = call i32 @_exit(i32 1)
unreachable
98: ; preds = %89
ret i32 0
}

LLVM IR (-O2):
@_POSIX_THREAD_KEYS_MAX = global i32 0
@.str = [19 x i8] c"key_create failed\0A\00"
@.str.1 = [16 x i8] c"created key %d\0A\00"
@.str.2 = [20 x i8] c"setspecific failed\0A\00"
@.str.3 = [20 x i8] c"getspecific failed\0A\00"
@.str.4 = [19 x i8] c"key_delete failed\0A\00"
@.str.5 = [23 x i8] c"2nd key_create failed\0A\00"
@.str.6 = [23 x i8] c"2nd key_delete failed\0A\00"
define i32 @do_test() {
%1 = load i32, i32* @_POSIX_THREAD_KEYS_MAX
%2 = shl i32 %1, 2
%3 = tail call i32* @alloca(i32 %2)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %53
5: ; preds = %0
%6 = zext i32 %1 to i64
br label %12
7: ; preds = %20
%8 = icmp eq i64 %28, %6
br i1 %8, label %9, label %12
9: ; preds = %7
br i1 %4, label %10, label %53
10: ; preds = %9
%11 = zext i32 %1 to i64
br label %34
12: ; preds = %5, %7
%13 = phi i64 [ 0, %5 ], [ %28, %7 ]
%14 = getelementptr inbounds i32, i32* %3, i64 %13
%15 = tail call i64 @pthread_key_create(i32* %14, i32* null)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 18)
%19 = tail call i32 @_exit(i32 1)
unreachable
20: ; preds = %12
%21 = trunc i64 %13 to i32
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %14
%24 = add nuw nsw i64 %13, 100
%25 = inttoptr i64 %24 to i8*
%26 = tail call i64 @pthread_setspecific(i32 %23, i8* %25)
%27 = icmp eq i64 %26, 0
%28 = add nuw nsw i64 %13, 1
br i1 %27, label %7, label %29
29: ; preds = %20
%30 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 19)
%31 = tail call i32 @_exit(i32 1)
unreachable
32: ; preds = %45
%33 = icmp eq i64 %49, %11
br i1 %33, label %53, label %34
34: ; preds = %10, %32
%35 = phi i64 [ 0, %10 ], [ %49, %32 ]
%36 = getelementptr inbounds i32, i32* %3, i64 %35
%37 = load i32, i32* %36
%38 = tail call i8* @pthread_getspecific(i32 %37)
%39 = add nuw nsw i64 %35, 100
%40 = inttoptr i64 %39 to i8*
%41 = icmp eq i8* %38, %40
br i1 %41, label %45, label %42
42: ; preds = %34
%43 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 19)
%44 = tail call i32 @_exit(i32 1)
unreachable
45: ; preds = %34
%46 = load i32, i32* %36
%47 = tail call i64 @pthread_key_delete(i32 %46)
%48 = icmp eq i64 %47, 0
%49 = add nuw nsw i64 %35, 1
br i1 %48, label %32, label %50
50: ; preds = %45
%51 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 18)
%52 = tail call i32 @_exit(i32 1)
unreachable
53: ; preds = %32, %0, %9
%54 = tail call i64 @pthread_key_create(i32* %3, i32* null)
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 22)
%58 = tail call i32 @_exit(i32 1)
unreachable
59: ; preds = %53
%60 = load i32, i32* %3
%61 = tail call i64 @pthread_key_delete(i32 %60)
%62 = icmp eq i64 %61, 0
br i1 %62, label %66, label %63
63: ; preds = %59
%64 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 22)
%65 = tail call i32 @_exit(i32 1)
unreachable
66: ; preds = %59
ret i32 0
}