#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*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ perror(char *);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *, void *, void *);
pthread_t *createThread(void *func, void *arg) {
  pthread_t *thread = malloc(sizeof(pthread_t));
  if (thread == NULL) {
    perror("malloc");
    return NULL;
  }
  int res = pthread_create(thread, NULL, func, arg);
  if (res != 0) {
    perror("pthread_create");
    free(thread);
    return NULL;
  }
  return thread;
}


x86 assembly (clang, -O0):
<createThread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1182 <createThread+0x42>
lea    0xe90(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ce <createThread+0x8e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_create@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c6 <createThread+0x86>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ce <createThread+0x8e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createThread>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x4,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     117a <createThread+0x3a>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <pthread_create@plt>
test   %eax,%eax
jne    1188 <createThread+0x48>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
jmp    119c <createThread+0x5c>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1171 <createThread+0x31>

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

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