#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;

/* Variables and functions */
int /*<<< orphan*/ log_msg(char *, int /*<<< orphan*/);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int);
int spawn_detached_thread(void *(*start_proc)(void *), void *arg) {
  pthread_t my_thread;
  int ret;
  ret = pthread_create(&my_thread, NULL, start_proc, arg);
  if (ret == 0)
    ret = pthread_detach(my_thread);
  if (ret != 0)
    log_msg("SPAWN THREAD: %s\n", strerror(ret));
  return ret;
}


x86 assembly (clang, -O0):
<spawn_detached_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_create@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    117d <spawn_detached_thread+0x3d>
mov    -0x14(%rbp),%edi
call   1030 <pthread_detach@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     119d <spawn_detached_thread+0x5d>
mov    -0x18(%rbp),%edi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xe68(%rip),%rdi
call   1040 <log_msg@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_detached_thread>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rcx
mov    %rdi,%rdx
xor    %ebx,%ebx
lea    0x4(%rsp),%rdi
xor    %esi,%esi
call   1050 <pthread_create@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    116c <spawn_detached_thread+0x2c>
mov    0x4(%rsp),%edi
call   1030 <pthread_detach@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1183 <spawn_detached_thread+0x43>
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
call   1040 <log_msg@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"SPAWN THREAD: %s\0A\00"
define i32 @spawn_detached_thread(i8* (i8*)* %0, i8* %1) {
%3 = alloca i8* (i8*)*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* (i8*)* %0, i8* (i8*)** %3
store i8* %1, i8** %4
%7 = load i8* (i8*)*, i8* (i8*)** %3
%8 = load i8*, i8** %4
%9 = call i32 @pthread_create(i32* %5, i32* null, i8* (i8*)* %7, i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %5
%14 = call i32 @pthread_detach(i32 %13)
store i32 %14, i32* %6
br label %15
15: ; preds = %12, %2
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* %6
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @log_msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %15
%23 = load i32, i32* %6
ret i32 %23
}

LLVM IR (-O2):
@.str = [18 x i8] c"SPAWN THREAD: %s\0A\00"
define i32 @spawn_detached_thread(i8* (i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @pthread_create(i32* %3, i32* null, i8* (i8*)* %0, i8* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* %3
%9 = call i32 @pthread_detach(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %2, %7
%12 = phi i32 [ %9, %7 ], [ %5, %2 ]
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @log_msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %7
%16 = phi i32 [ %12, %11 ], [ 0, %7 ]
ret i32 %16
}