#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *malloc(unsigned int);
scalar_t__ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          void *(*)(void *), void *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
pthread_t *manufacture_threads(unsigned n_threads, void *(*func)(void *),
                               void *func_arg) {
  if (n_threads < 1) {
    fprintf(stderr, "Number of threads, %u, is not valid\n", n_threads);
    exit(EXIT_FAILURE);
  }
  pthread_t *threads = malloc(n_threads * sizeof(pthread_t));
  for (int i = 0; i < n_threads; ++i) {
    if (pthread_create(threads + i, NULL, func, func_arg)) {
      fprintf(stderr, "Creating thread failed: %s", strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  return threads;
}


x86 assembly (clang, -O0):
<manufacture_threads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x1,-0x4(%rbp)
jae    1195 <manufacture_threads+0x45>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1233 <manufacture_threads+0xe3>
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_create@plt>
cmp    $0x0,%rax
je     1220 <manufacture_threads+0xd0>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1225 <manufacture_threads+0xd5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ae <manufacture_threads+0x5e>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manufacture_threads>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %edi,%edi
je     11f4 <manufacture_threads+0xa4>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
lea    0x0(,%r12,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r13
mov    %r12d,%r12d
shl    $0x2,%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rbx,%r13,1),%rdi
xor    %esi,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <pthread_create@plt>
test   %rax,%rax
jne    11bc <manufacture_threads+0x6c>
add    $0x4,%rbx
cmp    %rbx,%r12
jne    1190 <manufacture_threads+0x40>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2e1d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11e6 <manufacture_threads+0x96>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Number of threads, %u, is not valid\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Creating thread failed: %s\00"
@errno = global i32 0
define i32* @manufacture_threads(i32 %0, i8* (i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8* (i8*)*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i8* (i8*)* %1, i8* (i8*)** %5
store i8* %2, i8** %6
%9 = load i32, i32* %4
%10 = icmp ult i32 %9, 1
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = load i32, i32* %4
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %3
%18 = load i32, i32* %4
%19 = zext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = trunc i64 %20 to i32
%22 = call i32* @malloc(i32 %21)
store i32* %22, i32** %7
store i32 0, i32* %8
br label %23
23: ; preds = %44, %17
%24 = load i32, i32* %8
%25 = load i32, i32* %4
%26 = icmp ult i32 %24, %25
br i1 %26, label %27, label %47
27: ; preds = %23
%28 = load i32*, i32** %7
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i8* (i8*)*, i8* (i8*)** %5
%33 = load i8*, i8** %6
%34 = call i64 @pthread_create(i32* %31, i32* null, i8* (i8*)* %32, i8* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %27
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %27
br label %44
44: ; preds = %43
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %23
47: ; preds = %23
%48 = load i32*, i32** %7
ret i32* %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Number of threads, %u, is not valid\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Creating thread failed: %s\00"
@errno = global i32 0
define i32* @manufacture_threads(i32 %0, i8* (i8*)* %1, i8* %2) {
%4 = icmp eq i32 %0, 0
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %3
%11 = shl i32 %0, 2
%12 = tail call i32* @malloc(i32 %11)
%13 = zext i32 %0 to i64
br label %17
14: ; preds = %17
%15 = icmp eq i64 %22, %13
br i1 %15, label %16, label %17
16: ; preds = %14
ret i32* %12
17: ; preds = %10, %14
%18 = phi i64 [ 0, %10 ], [ %22, %14 ]
%19 = getelementptr inbounds i32, i32* %12, i64 %18
%20 = tail call i64 @pthread_create(i32* %19, i32* null, i8* (i8*)* %1, i8* %2)
%21 = icmp eq i64 %20, 0
%22 = add nuw nsw i64 %18, 1
br i1 %21, label %14, label %23
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
}