#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 *(*fn)(void *), void *arg) {
  int err = 0;
  pthread_t tid;
  pthread_attr_t attr;
  err = pthread_attr_init(&attr);
  if (err != 0) {
    return (err);
  }
  err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if (err != 0) {
    return (err);
  }
  err = pthread_create(&tid, &attr, fn, arg);
  pthread_attr_destroy(&attr);
  return err;
}


x86 assembly (clang, -O0):
<makethread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1178 <makethread+0x38>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c9 <makethread+0x89>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11a2 <makethread+0x62>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c9 <makethread+0x89>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <pthread_create@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <pthread_attr_destroy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makethread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1192 <makethread+0x52>
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1192 <makethread+0x52>
lea    0x4(%rsp),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <pthread_create@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1060 <pthread_attr_destroy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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
store i32 0, i32* %6
%9 = call i32 @pthread_attr_init(i32* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %6
store i32 %13, i32* %3
br label %27
14: ; preds = %2
%15 = load i32, i32* @PTHREAD_CREATE_DETACHED
%16 = call i32 @pthread_attr_setdetachstate(i32* %8, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %27
21: ; preds = %14
%22 = load i8* (i8*)*, i8* (i8*)** %4
%23 = load i8*, i8** %5
%24 = call i32 @pthread_create(i32* %7, i32* %8, i8* (i8*)* %22, i8* %23)
store i32 %24, i32* %6
%25 = call i32 @pthread_attr_destroy(i32* %8)
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %21, %19, %12
%28 = load i32, i32* %3
ret i32 %28
}

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* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32, i32* @PTHREAD_CREATE_DETACHED
%11 = call i32 @pthread_attr_setdetachstate(i32* %4, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = call i32 @pthread_create(i32* %3, i32* %4, i8* (i8*)* %0, i8* %1)
%15 = call i32 @pthread_attr_destroy(i32* %4)
br label %16
16: ; preds = %9, %2, %13
%17 = phi i32 [ %14, %13 ], [ %7, %2 ], [ %11, %9 ]
ret i32 %17
}