#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 init_pthread_attr(int /*<<< orphan*/ *, int const);
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int create_work_threads(int *count, void *(*start_func)(void *), void *arg,
                        pthread_t *tids, const int stack_size) {
  int result;
  pthread_attr_t thread_attr;
  pthread_t *ptid;
  pthread_t *ptid_end;
  if ((result = init_pthread_attr(&thread_attr, stack_size)) != 0) {
    return result;
  }
  result = 0;
  ptid_end = tids + (*count);
  for (ptid = tids; ptid < ptid_end; ptid++) {
    if ((result = pthread_create(ptid, &thread_attr, start_func, arg)) != 0) {
      *count = ptid - tids;
      break;
    }
  }
  pthread_attr_destroy(&thread_attr);
  return result;
}


x86 assembly (clang, -O0):
<create_work_threads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x34(%rbp),%rdi
call   1030 <init_pthread_attr@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     116f <create_work_threads+0x3f>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <create_work_threads+0xd4>
movl   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x48(%rbp),%rax
jae    11f5 <create_work_threads+0xc5>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x34(%rbp),%rsi
call   1040 <pthread_create@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     11df <create_work_threads+0xaf>
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f5 <create_work_threads+0xc5>
jmp    11e4 <create_work_threads+0xb4>
mov    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
jmp    1194 <create_work_threads+0x64>
lea    -0x34(%rbp),%rdi
call   1050 <pthread_attr_destroy@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_work_threads>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
mov    %r8d,%esi
call   1030 <init_pthread_attr@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11bc <create_work_threads+0x8c>
movslq (%r14),%rax
test   %rax,%rax
jle    11a4 <create_work_threads+0x74>
mov    %r14,0x10(%rsp)
lea    (%rbx,%rax,4),%r13
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
lea    0xc(%rsp),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <pthread_create@plt>
test   %eax,%eax
jne    11a8 <create_work_threads+0x78>
add    $0x4,%rbx
add    $0x1,%r14d
cmp    %r13,%rbx
jb     1180 <create_work_threads+0x50>
xor    %ebp,%ebp
jmp    11b2 <create_work_threads+0x82>
mov    %eax,%ebp
mov    0x10(%rsp),%rax
mov    %r14d,(%rax)
lea    0xc(%rsp),%rdi
call   1050 <pthread_attr_destroy@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @create_work_threads(i32* %0, i8* (i8*)* %1, i8* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8* (i8*)*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32*
store i32* %0, i32** %7
store i8* (i8*)* %1, i8* (i8*)** %8
store i8* %2, i8** %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%16 = load i32, i32* %11
%17 = call i32 @init_pthread_attr(i32* %13, i32 %16)
store i32 %17, i32* %12
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = load i32, i32* %12
store i32 %20, i32* %6
br label %54
21: ; preds = %5
store i32 0, i32* %12
%22 = load i32*, i32** %10
%23 = load i32*, i32** %7
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %22, i64 %25
store i32* %26, i32** %15
%27 = load i32*, i32** %10
store i32* %27, i32** %14
br label %28
28: ; preds = %48, %21
%29 = load i32*, i32** %14
%30 = load i32*, i32** %15
%31 = icmp ult i32* %29, %30
br i1 %31, label %32, label %51
32: ; preds = %28
%33 = load i32*, i32** %14
%34 = load i8* (i8*)*, i8* (i8*)** %8
%35 = load i8*, i8** %9
%36 = call i32 @pthread_create(i32* %33, i32* %13, i8* (i8*)* %34, i8* %35)
store i32 %36, i32* %12
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %47
38: ; preds = %32
%39 = load i32*, i32** %14
%40 = load i32*, i32** %10
%41 = ptrtoint i32* %39 to i64
%42 = ptrtoint i32* %40 to i64
%43 = sub i64 %41, %42
%44 = sdiv exact i64 %43, 4
%45 = trunc i64 %44 to i32
%46 = load i32*, i32** %7
store i32 %45, i32* %46
br label %51
47: ; preds = %32
br label %48
48: ; preds = %47
%49 = load i32*, i32** %14
%50 = getelementptr inbounds i32, i32* %49, i32 1
store i32* %50, i32** %14
br label %28
51: ; preds = %38, %28
%52 = call i32 @pthread_attr_destroy(i32* %13)
%53 = load i32, i32* %12
store i32 %53, i32* %6
br label %54
54: ; preds = %51, %19
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
define i32 @create_work_threads(i32* nocapture %0, i8* (i8*)* %1, i8* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i32 @init_pthread_attr(i32* %6, i32 %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %5
%11 = load i32, i32* %0
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i32, i32* %3, i64 %12
%14 = icmp sgt i32 %11, 0
br i1 %14, label %15, label %28
15: ; preds = %10, %25
%16 = phi i32* [ %26, %25 ], [ %3, %10 ]
%17 = call i32 @pthread_create(i32* %16, i32* %6, i8* (i8*)* %1, i8* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = ptrtoint i32* %16 to i64
%21 = ptrtoint i32* %3 to i64
%22 = sub i64 %20, %21
%23 = lshr exact i64 %22, 2
%24 = trunc i64 %23 to i32
store i32 %24, i32* %0
br label %28
25: ; preds = %15
%26 = getelementptr inbounds i32, i32* %16, i64 1
%27 = icmp ult i32* %26, %13
br i1 %27, label %15, label %28
28: ; preds = %25, %10, %19
%29 = phi i32 [ %17, %19 ], [ 0, %10 ], [ 0, %25 ]
%30 = call i32 @pthread_attr_destroy(i32* %6)
br label %31
31: ; preds = %5, %28
%32 = phi i32 [ %29, %28 ], [ %8, %5 ]
ret i32 %32
}