#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__ malloc(int);
int /*<<< orphan*/ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                  void *(*)(void *), void *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/);
void start_thread_group(int nth, void (*f)(int tid)) {
  pthread_t *th = (pthread_t *)malloc(nth * sizeof(pthread_t));
  for (int i = 0; i < nth; i++)
    pthread_create(&th[i], 0, (void *(*)(void *))f, (void *)(long)i);
  for (int i = 0; i < nth; i++)
    pthread_join(th[i], 0);
}


x86 assembly (clang, -O0):
<start_thread_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1191 <start_thread_group+0x61>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
movslq -0x1c(%rbp),%rcx
xor    %esi,%esi
call   1040 <pthread_create@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1159 <start_thread_group+0x29>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11c4 <start_thread_group+0x94>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %esi,%esi
call   1050 <pthread_join@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1198 <start_thread_group+0x68>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_thread_group>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r14d
lea    0x0(,%r14,4),%edi
call   1030 <malloc@plt>
test   %r14d,%r14d
jle    11a4 <start_thread_group+0x74>
mov    %rax,%r15
mov    %r14d,%ebp
xor    %ebx,%ebx
mov    %rax,%r13
xchg   %ax,%ax
mov    %r13,%rdi
xor    %esi,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <pthread_create@plt>
add    $0x1,%rbx
add    $0x4,%r13
cmp    %rbx,%rbp
jne    1160 <start_thread_group+0x30>
test   %r14d,%r14d
jle    11a4 <start_thread_group+0x74>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15,%rbx,4),%edi
xor    %esi,%esi
call   1050 <pthread_join@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <start_thread_group+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @start_thread_group(i32 %0, void (i32)* %1) {
%3 = alloca i32
%4 = alloca void (i32)*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store void (i32)* %1, void (i32)** %4
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 4
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %5
store i32 0, i32* %6
br label %14
14: ; preds = %29, %2
%15 = load i32, i32* %6
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %32
18: ; preds = %14
%19 = load i32*, i32** %5
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load void (i32)*, void (i32)** %4
%24 = bitcast void (i32)* %23 to i8* (i8*)*
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = inttoptr i64 %26 to i8*
%28 = call i32 @pthread_create(i32* %22, i32 0, i8* (i8*)* %24, i8* %27)
br label %29
29: ; preds = %18
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %14
32: ; preds = %14
store i32 0, i32* %7
br label %33
33: ; preds = %44, %32
%34 = load i32, i32* %7
%35 = load i32, i32* %3
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %47
37: ; preds = %33
%38 = load i32*, i32** %5
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = call i32 @pthread_join(i32 %42, i32 0)
br label %44
44: ; preds = %37
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %33
47: ; preds = %33
ret void
}

LLVM IR (-O2):
define void @start_thread_group(i32 %0, void (i32)* %1) {
%3 = shl i32 %0, 2
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i32*
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %20
7: ; preds = %2
%8 = bitcast void (i32)* %1 to i8* (i8*)*
%9 = zext i32 %0 to i64
br label %13
10: ; preds = %13
br i1 %6, label %11, label %20
11: ; preds = %10
%12 = zext i32 %0 to i64
br label %21
13: ; preds = %7, %13
%14 = phi i64 [ 0, %7 ], [ %18, %13 ]
%15 = getelementptr inbounds i32, i32* %5, i64 %14
%16 = inttoptr i64 %14 to i8*
%17 = tail call i32 @pthread_create(i32* %15, i32 0, i8* (i8*)* %8, i8* %16)
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, %9
br i1 %19, label %10, label %13
20: ; preds = %21, %2, %10
ret void
21: ; preds = %11, %21
%22 = phi i64 [ 0, %11 ], [ %26, %21 ]
%23 = getelementptr inbounds i32, i32* %5, i64 %22
%24 = load i32, i32* %23
%25 = tail call i32 @pthread_join(i32 %24, i32 0)
%26 = add nuw nsw i64 %22, 1
%27 = icmp eq i64 %26, %12
br i1 %27, label %20, label %21
}