#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 scalar_t__ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned int);
scalar_t__ pthread_self();
int get_thread_index(pthread_t threads[]) {
  pthread_t tid;
  int id;
  int i;
  tid = pthread_self();
  id = -1;
  for (i = 0; i < 4; i++) {
    if (tid == threads[i]) {
      id = i;
      break;
    }
  }
  if (id == -1) {
    printf("cannot find thread index for thread %x\n", (unsigned int)tid);
    exit(EXIT_FAILURE);
  }
  return id;
}


x86 assembly (clang, -O0):
<get_thread_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <pthread_self@plt>
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    1193 <get_thread_index+0x63>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x18(%rbp),%rdx
cmp    (%rcx,%rdx,8),%rax
jne    1180 <get_thread_index+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1193 <get_thread_index+0x63>
jmp    1185 <get_thread_index+0x55>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1155 <get_thread_index+0x25>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bd <get_thread_index+0x8d>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_thread_index>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <pthread_self@plt>
cmp    (%rbx),%rax
je     1165 <get_thread_index+0x35>
mov    $0x1,%ebp
cmp    0x8(%rbx),%rax
je     1165 <get_thread_index+0x35>
mov    $0x2,%ebp
cmp    0x10(%rbx),%rax
je     1165 <get_thread_index+0x35>
mov    $0x3,%ebp
cmp    0x18(%rbx),%rax
jne    116e <get_thread_index+0x3e>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"cannot find thread index for thread %x\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @get_thread_index(i64* %0) {
%2 = alloca i64*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i64* %0, i64** %2
%6 = call i64 (...) @pthread_self()
store i64 %6, i64* %3
store i32 -1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %21, %1
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 4
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = load i64, i64* %3
%12 = load i64*, i64** %2
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i64, i64* %12, i64 %14
%16 = load i64, i64* %15
%17 = icmp eq i64 %11, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = load i32, i32* %5
store i32 %19, i32* %4
br label %24
20: ; preds = %10
br label %21
21: ; preds = %20
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
br label %7
24: ; preds = %18, %7
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i64, i64* %3
%29 = trunc i64 %28 to i32
%30 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [40 x i8] c"cannot find thread index for thread %x\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @get_thread_index(i64* nocapture %0) {
%2 = tail call i64 (...) @pthread_self()
%3 = load i64, i64* %0
%4 = icmp eq i64 %2, %3
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = getelementptr inbounds i64, i64* %0, i64 1
%7 = load i64, i64* %6
%8 = icmp eq i64 %2, %7
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = getelementptr inbounds i64, i64* %0, i64 2
%11 = load i64, i64* %10
%12 = icmp eq i64 %2, %11
br i1 %12, label %22, label %13
13: ; preds = %9
%14 = getelementptr inbounds i64, i64* %0, i64 3
%15 = load i64, i64* %14
%16 = icmp eq i64 %2, %15
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = trunc i64 %2 to i32
%19 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %13, %9, %5, %1
%23 = phi i32 [ 0, %1 ], [ 1, %5 ], [ 2, %9 ], [ 3, %13 ]
ret i32 %23
}