#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*/ perror(char *);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *, void *, int *);
int pthread_detach(int /*<<< orphan*/);
int spawn_thread(void (*f)(int *n)) {
  pthread_t thread;
  int iret;
  int iarg0 = 0;
  iret = pthread_create(&thread, NULL, (void *)f, &iarg0);
  if (iret) {
    perror("spawning new thread");
    return iret;
  }
  iret = pthread_detach(thread);
  if (iret) {
    perror("detaching thread");
    return iret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spawn_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x1c(%rbp),%rcx
call   1040 <pthread_create@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     117c <spawn_thread+0x4c>
lea    0xe94(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <spawn_thread+0x7f>
mov    -0x14(%rbp),%edi
call   1030 <pthread_detach@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11a8 <spawn_thread+0x78>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <spawn_thread+0x7f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_thread>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rdx
movl   $0x0,0x8(%rsp)
xor    %ebx,%ebx
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   1040 <pthread_create@plt>
test   %eax,%eax
jne    116c <spawn_thread+0x3c>
mov    0xc(%rsp),%edi
call   1030 <pthread_detach@plt>
test   %eax,%eax
jne    1175 <spawn_thread+0x45>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe8d(%rip),%rdi
jmp    117c <spawn_thread+0x4c>
lea    0xe98(%rip),%rdi
mov    %eax,%ebx
call   1050 <perror@plt>
jmp    1164 <spawn_thread+0x34>

LLVM IR (-O0):
@.str = [20 x i8] c"spawning new thread\00"
@.str.1 = [17 x i8] c"detaching thread\00"
define i32 @spawn_thread(void (i32*)* %0) {
%2 = alloca i32
%3 = alloca void (i32*)*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store void (i32*)* %0, void (i32*)** %3
store i32 0, i32* %6
%7 = load void (i32*)*, void (i32*)** %3
%8 = bitcast void (i32*)* %7 to i8*
%9 = call i32 @pthread_create(i32* %4, i32* null, i8* %8, i32* %6)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %5
store i32 %14, i32* %2
br label %24
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @pthread_detach(i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %5
store i32 %22, i32* %2
br label %24
23: ; preds = %15
store i32 0, i32* %2
br label %24
24: ; preds = %23, %20, %12
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [20 x i8] c"spawning new thread\00"
@.str.1 = [17 x i8] c"detaching thread\00"
define i32 @spawn_thread(void (i32*)* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast void (i32*)* %0 to i8*
%7 = call i32 @pthread_create(i32* %2, i32* null, i8* %6, i32* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i32 @pthread_detach(i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9, %1
%14 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %9 ]
%15 = phi i32 [ %7, %1 ], [ %11, %9 ]
%16 = call i32 @perror(i8* %14)
br label %17
17: ; preds = %13, %9
%18 = phi i32 [ 0, %9 ], [ %15, %13 ]
ret i32 %18
}