#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*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int pthread_attr_init(int /*<<< orphan*/ *);
int pthread_attr_setstacksize(int /*<<< orphan*/ *, int);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int create_thread(pthread_t *thread, void *(*start_routine)(void *),
                  void *arg) {
  pthread_attr_t attr;
  int ret;
  if (0 != (ret = pthread_attr_init(&attr)))
    goto ERR;
  if (0 != (ret = pthread_attr_setstacksize(&attr, 1024 * 512))) {
    pthread_attr_destroy(&attr);
    goto ERR;
  }
  ret = pthread_create(thread, &attr, start_routine, arg);
  pthread_attr_destroy(&attr);
  return ret;
ERR:
  errno = EINVAL;
  return ret;
}


x86 assembly (clang, -O0):
<create_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     1171 <create_thread+0x31>
jmp    11c8 <create_thread+0x88>
lea    -0x24(%rbp),%rdi
mov    $0x80000,%esi
call   1060 <pthread_attr_setstacksize@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     119c <create_thread+0x5c>
lea    -0x24(%rbp),%rdi
call   1050 <pthread_attr_destroy@plt>
jmp    11c8 <create_thread+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rsi
call   1040 <pthread_create@plt>
mov    %eax,-0x28(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <pthread_attr_destroy@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <create_thread+0xa0>
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_thread>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xc(%rsp),%rdi
call   1030 <pthread_attr_init@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1185 <create_thread+0x45>
lea    0xc(%rsp),%rdi
mov    $0x80000,%esi
call   1060 <pthread_attr_setstacksize@plt>
test   %eax,%eax
je     11a7 <create_thread+0x67>
mov    %eax,%ebx
lea    0xc(%rsp),%rdi
call   1050 <pthread_attr_destroy@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    0x2e4b(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xc(%rsp),%r13
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <pthread_create@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1050 <pthread_attr_destroy@plt>
jmp    1197 <create_thread+0x57>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @create_thread(i32* %0, i8* (i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8* (i8*)*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i8* (i8*)* %1, i8* (i8*)** %6
store i8* %2, i8** %7
%10 = call i32 @pthread_attr_init(i32* %8)
store i32 %10, i32* %9
%11 = icmp ne i32 0, %10
br i1 %11, label %12, label %13
12: ; preds = %3
br label %25
13: ; preds = %3
%14 = call i32 @pthread_attr_setstacksize(i32* %8, i32 524288)
store i32 %14, i32* %9
%15 = icmp ne i32 0, %14
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @pthread_attr_destroy(i32* %8)
br label %25
18: ; preds = %13
%19 = load i32*, i32** %5
%20 = load i8* (i8*)*, i8* (i8*)** %6
%21 = load i8*, i8** %7
%22 = call i32 @pthread_create(i32* %19, i32* %8, i8* (i8*)* %20, i8* %21)
store i32 %22, i32* %9
%23 = call i32 @pthread_attr_destroy(i32* %8)
%24 = load i32, i32* %9
store i32 %24, i32* %4
br label %28
25: ; preds = %16, %12
%26 = load i32, i32* @EINVAL
store i32 %26, i32* @errno
%27 = load i32, i32* %9
store i32 %27, i32* %4
br label %28
28: ; preds = %25, %18
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @create_thread(i32* %0, i8* (i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @pthread_attr_init(i32* %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %3
%9 = call i32 @pthread_attr_setstacksize(i32* %4, i32 524288)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @pthread_attr_destroy(i32* %4)
br label %16
13: ; preds = %8
%14 = call i32 @pthread_create(i32* %0, i32* %4, i8* (i8*)* %1, i8* %2)
%15 = call i32 @pthread_attr_destroy(i32* %4)
br label %19
16: ; preds = %3, %11
%17 = phi i32 [ %6, %3 ], [ %9, %11 ]
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
br label %19
19: ; preds = %16, %13
%20 = phi i32 [ %17, %16 ], [ %14, %13 ]
ret i32 %20
}