#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 make_detached_thread(void *(*fn)(void *), void *arg) {
  int err;
  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) {
    err = pthread_create(&tid, &attr, fn, arg);
  }
  (void)pthread_attr_destroy(&attr);
  return err;
}


x86 assembly (clang, -O0):
<make_detached_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1171 <make_detached_thread+0x31>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <make_detached_thread+0x77>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a8 <make_detached_thread+0x68>
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):
<make_detached_thread>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1198 <make_detached_thread+0x58>
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    118e <make_detached_thread+0x4e>
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <pthread_create@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
call   1060 <pthread_attr_destroy@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PTHREAD_CREATE_DETACHED = global i32 0
define i32 @make_detached_thread(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* %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 %26
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 eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i8* (i8*)*, i8* (i8*)** %4
%21 = load i8*, i8** %5
%22 = call i32 @pthread_create(i32* %7, i32* %8, i8* (i8*)* %20, i8* %21)
store i32 %22, i32* %6
br label %23
23: ; preds = %19, %14
%24 = call i32 @pthread_attr_destroy(i32* %8)
%25 = load i32, i32* %6
store i32 %25, i32* %3
br label %26
26: ; preds = %23, %12
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@PTHREAD_CREATE_DETACHED = global i32 0
define i32 @make_detached_thread(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 %18
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 %15
13: ; preds = %9
%14 = call i32 @pthread_create(i32* %3, i32* %4, i8* (i8*)* %0, i8* %1)
br label %15
15: ; preds = %13, %9
%16 = phi i32 [ %14, %13 ], [ %11, %9 ]
%17 = call i32 @pthread_attr_destroy(i32* %4)
br label %18
18: ; preds = %2, %15
%19 = phi i32 [ %16, %15 ], [ %7, %2 ]
ret i32 %19
}