#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;
  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);
  }
  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)
lea    -0x24(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1171 <makethread+0x31>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c3 <makethread+0x83>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
test   $0x1,%al
jne    119c <makethread+0x5c>
jmp    11b4 <makethread+0x74>
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   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1162 <makethread+0x32>
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <pthread_attr_setdetachstate@plt>
mov    %r14,%rdi
call   1050 <pthread_attr_destroy@plt>
mov    %ebx,%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* %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 %24
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
store i32 0, i32* %6
br i1 false, label %17, label %21
17: ; preds = %14
%18 = load i8* (i8*)*, i8* (i8*)** %4
%19 = load i8*, i8** %5
%20 = call i32 @pthread_create(i32* %7, i32* %8, i8* (i8*)* %18, i8* %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %17, %14
%22 = call i32 @pthread_attr_destroy(i32* %8)
%23 = load i32, i32* %6
store i32 %23, i32* %3
br label %24
24: ; preds = %21, %12
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@PTHREAD_CREATE_DETACHED = global i32 0
define i32 @makethread(i8* (i8*)* nocapture readnone %0, i8* nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @pthread_attr_init(i32* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @PTHREAD_CREATE_DETACHED
%9 = call i32 @pthread_attr_setdetachstate(i32* %3, i32 %8)
%10 = call i32 @pthread_attr_destroy(i32* %3)
br label %11
11: ; preds = %2, %7
ret i32 %5
}