#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 */
char *calloc(int, unsigned long);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
char *pthread_getspecific(int /*<<< orphan*/);
int pthread_setspecific(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
char *get_thread_buf(pthread_key_t key, unsigned long bufsize) {
  char *buf = pthread_getspecific(key);
  if (buf != 0)
    return buf;
  int ret;
  buf = calloc(1, bufsize);
  if ((ret = pthread_setspecific(key, buf)) != 0)
    fprintf(stderr, "Failed to init thread buffer: %d", ret);
  return buf;
}


x86 assembly (clang, -O0):
<get_thread_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <pthread_getspecific@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1173 <get_thread_buf+0x33>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bd <get_thread_buf+0x7d>
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <pthread_setspecific@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11b5 <get_thread_buf+0x75>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_thread_buf>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <pthread_getspecific@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118b <get_thread_buf+0x4b>
mov    $0x1,%edi
mov    %r14,%rsi
call   1040 <calloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
call   1060 <pthread_setspecific@plt>
test   %eax,%eax
je     118b <get_thread_buf+0x4b>
mov    0x2e65(%rip),%rcx
mov    (%rcx),%edi
lea    0xe7c(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Failed to init thread buffer: %d\00"
define i8* @get_thread_buf(i32 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
%8 = load i32, i32* %4
%9 = call i8* @pthread_getspecific(i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i8*, i8** %6
store i8* %13, i8** %3
br label %27
14: ; preds = %2
%15 = load i64, i64* %5
%16 = call i8* @calloc(i32 1, i64 %15)
store i8* %16, i8** %6
%17 = load i32, i32* %4
%18 = load i8*, i8** %6
%19 = call i32 @pthread_setspecific(i32 %17, i8* %18)
store i32 %19, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = load i32, i32* %7
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %21, %14
%26 = load i8*, i8** %6
store i8* %26, i8** %3
br label %27
27: ; preds = %25, %12
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Failed to init thread buffer: %d\00"
define i8* @get_thread_buf(i32 %0, i64 %1) {
%3 = tail call i8* @pthread_getspecific(i32 %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = tail call i8* @calloc(i32 1, i64 %1)
%7 = tail call i32 @pthread_setspecific(i32 %0, i8* %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %12
12: ; preds = %5, %9, %2
%13 = phi i8* [ %3, %2 ], [ %6, %9 ], [ %6, %5 ]
ret i8* %13
}