#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 scalar_t__ pthread_t;

/* Variables and functions */
intptr_t EXIT_SUCCESS;
int /*<<< orphan*/ pthread_join(scalar_t__, void **);
int thread_cleanup(pthread_t *thread) {
  intptr_t pret = EXIT_SUCCESS;
  if (*thread == 0)
    return 0;
  pthread_join(*thread, (void **)&pret);
  *thread = 0;
  if (pret != EXIT_SUCCESS)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<thread_cleanup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1144 <thread_cleanup+0x34>
movl   $0x0,-0x4(%rbp)
jmp    1186 <thread_cleanup+0x76>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <pthread_join@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
je     117f <thread_cleanup+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1186 <thread_cleanup+0x76>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_cleanup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ec2(%rip),%r14
mov    (%r14),%rax
mov    %rax,(%rsp)
mov    (%rdi),%rdi
test   %rdi,%rdi
je     114c <thread_cleanup+0x3c>
mov    %rsp,%rsi
call   1030 <pthread_join@plt>
movq   $0x0,(%rbx)
mov    (%rsp),%rcx
xor    %eax,%eax
cmp    (%r14),%rcx
setne  %al
neg    %eax
jmp    114e <thread_cleanup+0x3e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EXIT_SUCCESS = global i64 0
define i32 @thread_cleanup(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca i64
store i64* %0, i64** %3
%5 = load i64, i64* @EXIT_SUCCESS
store i64 %5, i64* %4
%6 = load i64*, i64** %3
%7 = load i64, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 0, i32* %2
br label %21
10: ; preds = %1
%11 = load i64*, i64** %3
%12 = load i64, i64* %11
%13 = bitcast i64* %4 to i8**
%14 = call i32 @pthread_join(i64 %12, i8** %13)
%15 = load i64*, i64** %3
store i64 0, i64* %15
%16 = load i64, i64* %4
%17 = load i64, i64* @EXIT_SUCCESS
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %10
store i32 -1, i32* %2
br label %21
20: ; preds = %10
store i32 0, i32* %2
br label %21
21: ; preds = %20, %19, %9
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i64 0
define i32 @thread_cleanup(i64* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = load i64, i64* @EXIT_SUCCESS
store i64 %4, i64* %2
%5 = load i64, i64* %0
%6 = icmp eq i64 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %1
%8 = bitcast i64* %2 to i8**
%9 = call i32 @pthread_join(i64 %5, i8** %8)
store i64 0, i64* %0
%10 = load i64, i64* %2
%11 = load i64, i64* @EXIT_SUCCESS
%12 = icmp ne i64 %10, %11
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %7, %1
%15 = phi i32 [ 0, %1 ], [ %13, %7 ]
ret i32 %15
}