#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 unsigned long pthread_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
scalar_t__ pthread_create(unsigned long *, int /*<<< orphan*/ *,
                          void *(*)(void *), void *);
int /*<<< orphan*/ pthread_detach(unsigned long);
unsigned long _beginthread(void (*start_address)(), unsigned stack_size,
                           void *arglist) {
  pthread_t thread;
  if (pthread_create(&thread, NULL, (void *(*)(void *))start_address,
                     (void *)arglist) != 0)
    perror("New Thread");
  else
    pthread_detach(thread);
  return thread;
}


x86 assembly (clang, -O0):
<_beginthread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_create@plt>
cmp    $0x0,%rax
je     1173 <_beginthread+0x43>
lea    0xe97(%rip),%rdi
call   1050 <perror@plt>
jmp    117c <_beginthread+0x4c>
mov    -0x20(%rbp),%rdi
call   1030 <pthread_detach@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_beginthread>:
push   %rax
mov    %rdx,%rcx
mov    %rdi,%rdx
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <pthread_create@plt>
test   %rax,%rax
jne    1155 <_beginthread+0x25>
mov    (%rsp),%rdi
call   1030 <pthread_detach@plt>
mov    (%rsp),%rax
pop    %rcx
ret
lea    0xea4(%rip),%rdi
call   1050 <perror@plt>
jmp    114f <_beginthread+0x1f>

LLVM IR (-O0):
@.str = [11 x i8] c"New Thread\00"
define i64 @_beginthread(void (...)* %0, i32 %1, i8* %2) {
%4 = alloca void (...)*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store void (...)* %0, void (...)** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load void (...)*, void (...)** %4
%9 = bitcast void (...)* %8 to i8* (i8*)*
%10 = load i8*, i8** %6
%11 = call i64 @pthread_create(i64* %7, i32* null, i8* (i8*)* %9, i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %18
15: ; preds = %3
%16 = load i64, i64* %7
%17 = call i32 @pthread_detach(i64 %16)
br label %18
18: ; preds = %15, %13
%19 = load i64, i64* %7
ret i64 %19
}

LLVM IR (-O2):
@.str = [11 x i8] c"New Thread\00"
define i64 @_beginthread(void (...)* %0, i32 %1, i8* %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = bitcast void (...)* %0 to i8* (i8*)*
%7 = call i64 @pthread_create(i64* %4, i32* null, i8* (i8*)* %6, i8* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %14
11: ; preds = %3
%12 = load i64, i64* %4
%13 = call i32 @pthread_detach(i64 %12)
br label %14
14: ; preds = %11, %9
%15 = load i64, i64* %4
ret i64 %15
}