#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*/ abort();
int errno;
int /*<<< orphan*/ printf(char *);
int pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
void join_threads(pthread_t *threads, size_t count) {
  for (size_t i = 0; i < count; ++i) {
    int ret = pthread_join(threads[i], NULL);
    if (ret != 0) {
      errno = ret;
      printf("error: pthread_join: %m\n");
      abort();
    }
  }
}


x86 assembly (clang, -O0):
<join_threads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ac <join_threads+0x7c>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_join@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1196 <join_threads+0x66>
mov    -0x1c(%rbp),%ecx
mov    0x2e5f(%rip),%rax
mov    %ecx,(%rax)
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    119b <join_threads+0x6b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1148 <join_threads+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_threads>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1168 <join_threads+0x38>
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14,%rbx,4),%edi
xor    %esi,%esi
call   1050 <pthread_join@plt>
test   %eax,%eax
jne    116e <join_threads+0x3e>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <join_threads+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e6b(%rip),%rcx
mov    %eax,(%rcx)
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [25 x i8] c"error: pthread_join: %m\0A\00"
define void @join_threads(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
br label %7
7: ; preds = %24, %2
%8 = load i64, i64* %5
%9 = load i64, i64* %4
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = load i32*, i32** %3
%13 = load i64, i64* %5
%14 = getelementptr inbounds i32, i32* %12, i64 %13
%15 = load i32, i32* %14
%16 = call i32 @pthread_join(i32 %15, i32* null)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i32, i32* %6
store i32 %20, i32* @errno
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%22 = call i32 (...) @abort()
unreachable
23: ; preds = %11
br label %24
24: ; preds = %23
%25 = load i64, i64* %5
%26 = add i64 %25, 1
store i64 %26, i64* %5
br label %7
27: ; preds = %7
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [25 x i8] c"error: pthread_join: %m\0A\00"
define void @join_threads(i32* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %6, label %7
4: ; preds = %7
%5 = icmp eq i64 %13, %1
br i1 %5, label %6, label %7
6: ; preds = %4, %2
ret void
7: ; preds = %2, %4
%8 = phi i64 [ %13, %4 ], [ 0, %2 ]
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = load i32, i32* %9
%11 = tail call i32 @pthread_join(i32 %10, i32* null)
%12 = icmp eq i32 %11, 0
%13 = add nuw i64 %8, 1
br i1 %12, label %4, label %14
14: ; preds = %7
store i32 %11, i32* @errno
%15 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 (...) @abort()
unreachable
}