#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 /*<<< orphan*/ pthread_cancel(int /*<<< orphan*/);
scalar_t__ pthread_equal(int /*<<< orphan*/, int /*<<< orphan*/);
int pthread_join(int /*<<< orphan*/, void **);
void *pthread_cancel_and_join_if_started(pthread_t tid, pthread_t none) {
  void *ret;
  if (pthread_equal(tid, none))
    return NULL;
  else {
    pthread_cancel(tid);
    if (-1 != pthread_join(tid, &ret))
      return ret;
    else
      return NULL;
  }
}


x86 assembly (clang, -O0):
<pthread_cancel_and_join_if_started>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <pthread_equal@plt>
cmp    $0x0,%rax
je     1160 <pthread_cancel_and_join_if_started+0x30>
movq   $0x0,-0x8(%rbp)
jmp    1198 <pthread_cancel_and_join_if_started+0x68>
mov    -0xc(%rbp),%edi
call   1050 <pthread_cancel@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <pthread_join@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1190 <pthread_cancel_and_join_if_started+0x60>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1198 <pthread_cancel_and_join_if_started+0x68>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_cancel_and_join_if_started>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
call   1030 <pthread_equal@plt>
test   %rax,%rax
jne    1160 <pthread_cancel_and_join_if_started+0x30>
mov    %ebx,%edi
call   1050 <pthread_cancel@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1040 <pthread_join@plt>
cmp    $0xffffffff,%eax
je     1160 <pthread_cancel_and_join_if_started+0x30>
mov    0x8(%rsp),%rax
jmp    1162 <pthread_cancel_and_join_if_started+0x32>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @pthread_cancel_and_join_if_started(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i64 @pthread_equal(i32 %0, i32 %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = tail call i32 @pthread_cancel(i32 %0)
%9 = call i32 @pthread_join(i32 %0, i8** %3)
%10 = icmp eq i32 %9, -1
%11 = load i8*, i8** %3
%12 = select i1 %10, i8* null, i8* %11
br label %13
13: ; preds = %7, %2
%14 = phi i8* [ null, %2 ], [ %12, %7 ]
ret i8* %14
}