#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*/ PR_SET_NAME;
int /*<<< orphan*/ PTHREAD_CREATE_DETACHED;
int /*<<< orphan*/ prctl(int /*<<< orphan*/, char const *, int /*<<< orphan*/,
                         int /*<<< orphan*/, int /*<<< orphan*/);
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 *);
int thread(const char *name, void *cb, void *args) {
  int err;
  pthread_t thr;
  pthread_attr_t thr_attr;
  err = pthread_attr_init(&thr_attr);
  if (err)
    return err;
  err = pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
  if (err)
    goto exit;
  if (name)
    prctl(PR_SET_NAME, name, 0, 0, 0);
  err = pthread_create(&thr, &thr_attr, cb, args);
exit:
  pthread_attr_destroy(&thr_attr);
  return err;
}


x86 assembly (clang, -O0):
<thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1185 <thread+0x35>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <thread+0xa6>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    -0x2c(%rbp),%rdi
call   1050 <pthread_attr_setdetachstate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11a9 <thread+0x59>
jmp    11e7 <thread+0x97>
cmpq   $0x0,-0x10(%rbp)
je     11cf <thread+0x7f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1040 <prctl@plt>
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1060 <pthread_create@plt>
mov    %eax,-0x24(%rbp)
lea    -0x2c(%rbp),%rdi
call   1070 <pthread_attr_destroy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11bf <thread+0x6f>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1050 <pthread_attr_setdetachstate@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11b7 <thread+0x67>
test   %rbx,%rbx
je     11a2 <thread+0x52>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <prctl@plt>
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <pthread_create@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1070 <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
@PR_SET_NAME = global i32 0
define i32 @thread(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = call i32 @pthread_attr_init(i32* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* %8
store i32 %15, i32* %4
br label %36
16: ; preds = %3
%17 = load i32, i32* @PTHREAD_CREATE_DETACHED
%18 = call i32 @pthread_attr_setdetachstate(i32* %10, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %33
22: ; preds = %16
%23 = load i8*, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @PR_SET_NAME
%27 = load i8*, i8** %5
%28 = call i32 @prctl(i32 %26, i8* %27, i32 0, i32 0, i32 0)
br label %29
29: ; preds = %25, %22
%30 = load i8*, i8** %6
%31 = load i8*, i8** %7
%32 = call i32 @pthread_create(i32* %9, i32* %10, i8* %30, i8* %31)
store i32 %32, i32* %8
br label %33
33: ; preds = %29, %21
%34 = call i32 @pthread_attr_destroy(i32* %10)
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %33, %14
%37 = load i32, i32* %4
ret i32 %37
}

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