#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__ calloc(int, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  void *(*)(void *), int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
void test_multi_thread(int thread_num, void *(*p)(void *)) {
  pthread_t *id = (pthread_t *)calloc(1, sizeof(pthread_t) * thread_num);
  for (int i = 0; i < thread_num; i++) {
    pthread_create(id + i, NULL, p, NULL);
  }
  for (int i = 0; i < thread_num; i++) {
    pthread_join(id[i], NULL);
  }
  free(id);
}


x86 assembly (clang, -O0):
<test_multi_thread>:
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,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a7 <test_multi_thread+0x67>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
call   1050 <pthread_create@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <test_multi_thread+0x2e>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11dc <test_multi_thread+0x9c>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <pthread_join@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ae <test_multi_thread+0x6e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_multi_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r14d
lea    0x0(,%r14,4),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    11b4 <test_multi_thread+0x74>
mov    %r14d,%r13d
mov    %r13,%rbp
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1050 <pthread_create@plt>
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
jne    1180 <test_multi_thread+0x40>
test   %r14d,%r14d
jle    11b4 <test_multi_thread+0x74>
xor    %ebx,%ebx
mov    (%r15,%rbx,4),%edi
xor    %esi,%esi
call   1060 <pthread_join@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11a0 <test_multi_thread+0x60>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

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