#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_equal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_self();
int /*<<< orphan*/ stdout;
void *thread_routine2(void *arg) {
  fprintf(stdout, "thread2: I'm running...\n");
  pthread_t main_thread = (pthread_t)arg;
  pthread_detach(pthread_self());
  if (!pthread_equal(main_thread, pthread_self())) {
    fprintf(stdout, "thread2: main thread id is not equal thread2\n");
  }
  pthread_join(main_thread, NULL);
  fprintf(stdout, "thread2: main thread exit!\n");
  fprintf(stdout, "thread2: exit!\n");
  fprintf(stdout, "thread2: process exit!\n");
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<thread_routine2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
call   1050 <pthread_detach@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%esi
call   1060 <pthread_equal@plt>
cmp    $0x0,%eax
jne    11cb <thread_routine2+0x6b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_join@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_routine2>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e72(%rip),%r14
mov    (%r14),%edi
lea    0xe88(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %eax,%edi
call   1050 <pthread_detach@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <pthread_equal@plt>
test   %eax,%eax
jne    11ae <thread_routine2+0x4e>
mov    (%r14),%edi
lea    0xe70(%rip),%rsi
call   1030 <fprintf@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <pthread_join@plt>
mov    (%r14),%edi
lea    0xe86(%rip),%rsi
call   1030 <fprintf@plt>
mov    (%r14),%edi
lea    0xe93(%rip),%rsi
call   1030 <fprintf@plt>
mov    (%r14),%edi
lea    0xe94(%rip),%rsi
call   1030 <fprintf@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <pthread_exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [25 x i8] c"thread2: I'm running...\0A\00"
@.str.1 = [46 x i8] c"thread2: main thread id is not equal thread2\0A\00"
@.str.2 = [28 x i8] c"thread2: main thread exit!\0A\00"
@.str.3 = [16 x i8] c"thread2: exit!\0A\00"
@.str.4 = [24 x i8] c"thread2: process exit!\0A\00"
define i8* @thread_routine2(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i32, i32* @stdout
%6 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i32
store i32 %8, i32* %4
%9 = call i32 (...) @pthread_self()
%10 = call i32 @pthread_detach(i32 %9)
%11 = load i32, i32* %4
%12 = call i32 (...) @pthread_self()
%13 = call i32 @pthread_equal(i32 %11, i32 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i32, i32* @stdout
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %15, %1
%19 = load i32, i32* %4
%20 = call i32 @pthread_join(i32 %19, i32* null)
%21 = load i32, i32* @stdout
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @stdout
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @stdout
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @pthread_exit(i32* null)
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [25 x i8] c"thread2: I'm running...\0A\00"
@.str.1 = [46 x i8] c"thread2: main thread id is not equal thread2\0A\00"
@.str.2 = [28 x i8] c"thread2: main thread exit!\0A\00"
@.str.3 = [16 x i8] c"thread2: exit!\0A\00"
@.str.4 = [24 x i8] c"thread2: process exit!\0A\00"
define noalias i8* @thread_routine2(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fprintf(i32 %2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%4 = ptrtoint i8* %0 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i32 (...) @pthread_self()
%7 = tail call i32 @pthread_detach(i32 %6)
%8 = tail call i32 (...) @pthread_self()
%9 = tail call i32 @pthread_equal(i32 %5, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stdout
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %11, %1
%15 = tail call i32 @pthread_join(i32 %5, i32* null)
%16 = load i32, i32* @stdout
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* @stdout
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @pthread_exit(i32* null)
ret i8* undef
}