#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*/ free(void *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  void *(*)(void *), int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_self();
void *thread_fn(void *arg) {
  pthread_t t, p, *self;
  if (arg) {
    p = *(pthread_t *)arg;
    pthread_join(p, NULL);
    free(arg);
  }
  self = malloc(sizeof(*self));
  *self = pthread_self();
  pthread_create(&t, NULL, thread_fn, self);
  return NULL;
}


x86 assembly (clang, -O0):
<thread_fn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1185 <thread_fn+0x35>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <pthread_join@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rcx
lea    -0xc(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    0x2e33(%rip),%rdx
call   1050 <pthread_create@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_fn>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     116e <thread_fn+0x1e>
mov    %rdi,%rbx
mov    (%rdi),%edi
xor    %esi,%esi
call   1070 <pthread_join@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <pthread_self@plt>
mov    %eax,(%rbx)
mov    0x2e5d(%rip),%rdx
lea    0xc(%rsp),%rdi
xor    %esi,%esi
mov    %rbx,%rcx
call   1050 <pthread_create@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define noalias i8* @thread_fn(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = bitcast i8* %0 to i32*
%7 = load i32, i32* %6
%8 = tail call i32 @pthread_join(i32 %7, i32* null)
%9 = tail call i32 @free(i8* %0)
br label %10
10: ; preds = %5, %1
%11 = tail call i32* @malloc(i32 4)
%12 = tail call i32 (...) @pthread_self()
store i32 %12, i32* %11
%13 = call i32 @pthread_create(i32* %2, i32* null, i8* (i8*)* @thread_fn, i32* %11)
ret i8* null
}