#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;
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_CREATE_DETACHED;
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int pthread_attr_init(int /*<<< orphan*/ *);
int pthread_attr_setdetachstate(int /*<<< orphan*/ *, int /*<<< orphan*/);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int makethread(void *(*thr_fn)(void *), void *arg) {
  pthread_attr_t attr;
  pthread_t tid;
  int err;
  err = pthread_attr_init(&attr);
  if (err)
    return err;
  err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if (err)
    return err;
  err = pthread_create(&tid, &attr, thr_fn, arg);
  if (err)
    return err;
  pthread_attr_destroy(&attr);
  return 0;
}


x86 assembly (clang, -O0):
<makethread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1171 <makethread+0x31>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <makethread+0x98>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     119b <makethread+0x5b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <makethread+0x98>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1050 <pthread_create@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c8 <makethread+0x88>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <makethread+0x98>
lea    -0x1c(%rbp),%rdi
call   1060 <pthread_attr_destroy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makethread>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1030 <pthread_attr_init@plt>
test   %eax,%eax
jne    118c <makethread+0x4c>
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1040 <pthread_attr_setdetachstate@plt>
test   %eax,%eax
jne    118c <makethread+0x4c>
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <pthread_create@plt>
test   %eax,%eax
jne    118c <makethread+0x4c>
mov    %rsp,%rdi
call   1060 <pthread_attr_destroy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PTHREAD_CREATE_DETACHED = global i32 0
define i32 @makethread(i8* (i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8* (i8*)*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* (i8*)* %0, i8* (i8*)** %4
store i8* %1, i8** %5
%9 = call i32 @pthread_attr_init(i32* %6)
store i32 %9, i32* %8
%10 = load i32, i32* %8
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %8
store i32 %13, i32* %3
br label %31
14: ; preds = %2
%15 = load i32, i32* @PTHREAD_CREATE_DETACHED
%16 = call i32 @pthread_attr_setdetachstate(i32* %6, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* %8
store i32 %20, i32* %3
br label %31
21: ; preds = %14
%22 = load i8* (i8*)*, i8* (i8*)** %4
%23 = load i8*, i8** %5
%24 = call i32 @pthread_create(i32* %7, i32* %6, i8* (i8*)* %22, i8* %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* %8
store i32 %28, i32* %3
br label %31
29: ; preds = %21
%30 = call i32 @pthread_attr_destroy(i32* %6)
store i32 0, i32* %3
br label %31
31: ; preds = %29, %27, %19, %12
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@PTHREAD_CREATE_DETACHED = global i32 0
define i32 @makethread(i8* (i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @pthread_attr_init(i32* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %2
%10 = load i32, i32* @PTHREAD_CREATE_DETACHED
%11 = call i32 @pthread_attr_setdetachstate(i32* %3, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = call i32 @pthread_create(i32* %4, i32* %3, i8* (i8*)* %0, i8* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @pthread_attr_destroy(i32* %3)
br label %18
18: ; preds = %13, %9, %2, %16
%19 = phi i32 [ 0, %16 ], [ %7, %2 ], [ %11, %9 ], [ %14, %13 ]
ret i32 %19
}