#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 */

/* Variables and functions */
int arc4random_uniform(int);
int index_of_tournament_selection_winner(int tournament_size, double *array,
                                         int array_size) {
  int index = -1;
  for (int i = 0; i < tournament_size; i++) {
    int random_index = arc4random_uniform(array_size);
    if (index == -1 || array[random_index] > array[index]) {
      index = random_index;
    }
  }
  return index;
}


x86 assembly (clang, -O0):
<index_of_tournament_selection_winner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    118a <index_of_tournament_selection_winner+0x7a>
mov    -0x14(%rbp),%edi
call   1030 <arc4random_uniform@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     1171 <index_of_tournament_selection_winner+0x61>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
ucomisd (%rax,%rcx,8),%xmm0
jbe    1177 <index_of_tournament_selection_winner+0x67>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    117c <index_of_tournament_selection_winner+0x6c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1130 <index_of_tournament_selection_winner+0x20>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<index_of_tournament_selection_winner>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
jle    115a <index_of_tournament_selection_winner+0x4a>
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0xffffffff,%ebx
jmp    1137 <index_of_tournament_selection_winner+0x27>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebx
add    $0xffffffff,%ebp
je     115f <index_of_tournament_selection_winner+0x4f>
mov    %r14d,%edi
call   1030 <arc4random_uniform@plt>
cmp    $0xffffffff,%ebx
je     1130 <index_of_tournament_selection_winner+0x20>
movslq %eax,%rcx
movsd  (%r15,%rcx,8),%xmm0
movslq %ebx,%rcx
ucomisd (%r15,%rcx,8),%xmm0
ja     1130 <index_of_tournament_selection_winner+0x20>
jmp    1132 <index_of_tournament_selection_winner+0x22>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @index_of_tournament_selection_winner(i32 %0, double* %1, i32 %2) {
%4 = alloca i32
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store double* %1, double** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %34, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = load i32, i32* %6
%16 = call i32 @arc4random_uniform(i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %31, label %19
19: ; preds = %14
%20 = load double*, double** %5
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = load double, double* %23
%25 = load double*, double** %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %25, i64 %27
%29 = load double, double* %28
%30 = fcmp ogt double %24, %29
br i1 %30, label %31, label %33
31: ; preds = %19, %14
%32 = load i32, i32* %9
store i32 %32, i32* %7
br label %33
33: ; preds = %31, %19
br label %34
34: ; preds = %33
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %10
37: ; preds = %10
%38 = load i32, i32* %7
ret i32 %38
}

LLVM IR (-O2):
define i32 @index_of_tournament_selection_winner(i32 %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %0, 0
br i1 %4, label %7, label %5
5: ; preds = %21, %3
%6 = phi i32 [ -1, %3 ], [ %22, %21 ]
ret i32 %6
7: ; preds = %3, %21
%8 = phi i32 [ %22, %21 ], [ -1, %3 ]
%9 = phi i32 [ %23, %21 ], [ 0, %3 ]
%10 = tail call i32 @arc4random_uniform(i32 %2)
%11 = icmp eq i32 %8, -1
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds double, double* %1, i64 %13
%15 = load double, double* %14
%16 = sext i32 %8 to i64
%17 = getelementptr inbounds double, double* %1, i64 %16
%18 = load double, double* %17
%19 = fcmp ogt double %15, %18
br i1 %19, label %20, label %21
20: ; preds = %12, %7
br label %21
21: ; preds = %20, %12
%22 = phi i32 [ %10, %20 ], [ %8, %12 ]
%23 = add nuw nsw i32 %9, 1
%24 = icmp eq i32 %23, %0
br i1 %24, label %5, label %7
}