#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 */
scalar_t__ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          void *(*)(void *), void *);
int create_threads(int n, void *(*thr_main)(void *arg)) {
  long i;
  pthread_t tid;
  for (i = 0; i < n; i++) {
    if (0 != pthread_create(&tid, NULL, thr_main, (void *)i)) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_threads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jge    117d <create_threads+0x6d>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_create@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1167 <create_threads+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1184 <create_threads+0x74>
jmp    116c <create_threads+0x5c>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1127 <create_threads+0x17>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_threads>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
jle    114e <create_threads+0x3e>
mov    %rsi,%r14
movslq %edi,%r12
xor    %ebx,%ebx
lea    0x4(%rsp),%r15
nopl   0x0(%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <pthread_create@plt>
test   %rax,%rax
jne    1152 <create_threads+0x42>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <create_threads+0x20>
xor    %eax,%eax
jmp    1157 <create_threads+0x47>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @create_threads(i32 %0, i8* (i8*)* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8* (i8*)*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8* (i8*)* %1, i8* (i8*)** %5
store i64 0, i64* %6
br label %8
8: ; preds = %21, %2
%9 = load i64, i64* %6
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = icmp slt i64 %9, %11
br i1 %12, label %13, label %24
13: ; preds = %8
%14 = load i8* (i8*)*, i8* (i8*)** %5
%15 = load i64, i64* %6
%16 = inttoptr i64 %15 to i8*
%17 = call i64 @pthread_create(i32* %7, i32* null, i8* (i8*)* %14, i8* %16)
%18 = icmp ne i64 0, %17
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %3
br label %25
20: ; preds = %13
br label %21
21: ; preds = %20
%22 = load i64, i64* %6
%23 = add nsw i64 %22, 1
store i64 %23, i64* %6
br label %8
24: ; preds = %8
store i32 0, i32* %3
br label %25
25: ; preds = %24, %19
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
define i32 @create_threads(i32 %0, i8* (i8*)* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = sext i32 %0 to i64
%6 = icmp sgt i32 %0, 0
br i1 %6, label %9, label %15
7: ; preds = %9
%8 = icmp eq i64 %14, %5
br i1 %8, label %15, label %9
9: ; preds = %2, %7
%10 = phi i64 [ %14, %7 ], [ 0, %2 ]
%11 = inttoptr i64 %10 to i8*
%12 = call i64 @pthread_create(i32* %3, i32* null, i8* (i8*)* %1, i8* %11)
%13 = icmp eq i64 %12, 0
%14 = add nuw nsw i64 %10, 1
br i1 %13, label %7, label %15
15: ; preds = %9, %7, %2
%16 = phi i32 [ 0, %2 ], [ 0, %7 ], [ -1, %9 ]
ret i32 %16
}