#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*/ perror(char *);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ const *, void *,
                   void *);
unsigned long unix_hal_start_thread(void (*thread)(void *arg), void *arg) {
  unsigned long tid;
  const pthread_attr_t *attr = 0;
  int rc = pthread_create((pthread_t *)&tid, attr, (void *)thread, arg);
  if (rc) {
    perror("unix_hal_start_thread");
    return 0;
  }
  return tid;
}


x86 assembly (clang, -O0):
<unix_hal_start_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <pthread_create@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1173 <unix_hal_start_thread+0x53>
lea    0xe9f(%rip),%rdi
call   1040 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    117b <unix_hal_start_thread+0x5b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_hal_start_thread>:
push   %rax
mov    %rsi,%rcx
mov    %rdi,%rdx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <pthread_create@plt>
test   %eax,%eax
jne    113b <unix_hal_start_thread+0x1b>
mov    (%rsp),%rax
pop    %rcx
ret
lea    0xebe(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
pop    %rcx
ret

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

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