#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*/ sem_t;
typedef int /*<<< orphan*/ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ initializeSemaphores(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ runAllThreads(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ *, int);
int main(int argc, char *args[]) {
  int nP = 500;
  sem_t lock;
  sem_t forks[nP];
  pthread_t P[nP];
  initializeSemaphores(&lock, forks, nP);
  runAllThreads(P, forks, &lock, nP);
  pthread_exit(NULL);
  printf("All threads completed");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1f4,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1030 <initializeSemaphores@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%ecx
lea    -0x18(%rbp),%rdx
call   1040 <runAllThreads@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <pthread_exit@plt>
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xfb0,%rsp
mov    %rsp,%r15
lea    0xc(%rsp),%r14
lea    0x7e0(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1f4,%edx
call   1030 <initializeSemaphores@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    $0x1f4,%ecx
call   1040 <runAllThreads@plt>
xor    %edi,%edi
call   1060 <pthread_exit@plt>
lea    0xe71(%rip),%rdi
call   1050 <printf@plt>
mov    %r15,%rsp
xor    %eax,%eax
add    $0xfb0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"All threads completed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 500, i32* %5
%10 = load i32, i32* %5
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i32, i64 %11
store i64 %11, i64* %8
%14 = load i32, i32* %5
%15 = zext i32 %14 to i64
%16 = alloca i32, i64 %15
store i64 %15, i64* %9
%17 = load i32, i32* %5
%18 = call i32 @initializeSemaphores(i32* %6, i32* %13, i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @runAllThreads(i32* %16, i32* %13, i32* %6, i32 %19)
%21 = call i32 @pthread_exit(i32* null)
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%23 = load i8*, i8** %7
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"All threads completed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i8* @llvm.stacksave()
%6 = alloca [500 x i32]
%7 = alloca [500 x i32]
%8 = getelementptr inbounds [500 x i32], [500 x i32]* %7, i64 0, i64 0
%9 = getelementptr inbounds [500 x i32], [500 x i32]* %6, i64 0, i64 0
%10 = call i32 @initializeSemaphores(i32* %3, i32* %9, i32 500)
%11 = call i32 @runAllThreads(i32* %8, i32* %9, i32* %3, i32 500)
%12 = call i32 @pthread_exit(i32* null)
%13 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
ret i32 0
}