#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 */
struct parameters {
  int dummy;
};
struct chromosome {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ copyChromosome(struct chromosome *, struct chromosome *);
scalar_t__ getChromosomeFitness(struct chromosome *);
int rand();
void tournament(struct parameters *params, struct chromosome **parents,
                struct chromosome **candidateChromos, int numParents,
                int numCandidateChromos) {
  int i;
  struct chromosome *candidateA;
  struct chromosome *candidateB;
  for (i = 0; i < numParents; i++) {
    candidateA = candidateChromos[rand() % numCandidateChromos];
    candidateB = candidateChromos[rand() % numCandidateChromos];
    if (getChromosomeFitness(candidateA) <= getChromosomeFitness(candidateB)) {
      copyChromosome(parents[i], candidateA);
    } else {
      copyChromosome(parents[i], candidateB);
    }
  }
}


x86 assembly (clang, -O0):
<tournament>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    120a <tournament+0xda>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0x20(%rbp)
mov    -0x50(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0x20(%rbp)
mov    -0x48(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <getChromosomeFitness@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <getChromosomeFitness@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jg     11e2 <tournament+0xb2>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <copyChromosome@plt>
jmp    11f7 <tournament+0xc7>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <copyChromosome@plt>
jmp    11fc <tournament+0xcc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1152 <tournament+0x22>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tournament>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %ecx,%ecx
jle    11bd <tournament+0x8d>
mov    %r8d,%ebx
mov    %rdx,%r12
mov    %rsi,%r13
mov    %ecx,%eax
mov    %rax,0x10(%rsp)
xor    %r14d,%r14d
mov    %r8d,0xc(%rsp)
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idiv   %ebx
movslq %edx,%rax
mov    (%r12,%rax,8),%rbp
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idiv   %ebx
movslq %edx,%rax
mov    (%r12,%rax,8),%rbx
mov    %rbp,%rdi
call   1040 <getChromosomeFitness@plt>
mov    %r13,%r15
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <getChromosomeFitness@plt>
cmp    %rax,%r13
mov    %r15,%r13
mov    (%r15,%r14,8),%rdi
cmovg  %rbx,%rbp
mov    0xc(%rsp),%ebx
mov    %rbp,%rsi
call   1030 <copyChromosome@plt>
add    $0x1,%r14
cmp    %r14,0x10(%rsp)
jne    1160 <tournament+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.parameters = type { i32 }
%struct.chromosome = type { i32 }
define void @tournament(%struct.parameters* %0, %struct.chromosome** %1, %struct.chromosome** %2, i32 %3, i32 %4) {
%6 = alloca %struct.parameters*
%7 = alloca %struct.chromosome**
%8 = alloca %struct.chromosome**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.chromosome*
%13 = alloca %struct.chromosome*
store %struct.parameters* %0, %struct.parameters** %6
store %struct.chromosome** %1, %struct.chromosome*** %7
store %struct.chromosome** %2, %struct.chromosome*** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %11
br label %14
14: ; preds = %55, %5
%15 = load i32, i32* %11
%16 = load i32, i32* %9
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %58
18: ; preds = %14
%19 = load %struct.chromosome**, %struct.chromosome*** %8
%20 = call i32 (...) @rand()
%21 = load i32, i32* %10
%22 = srem i32 %20, %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %19, i64 %23
%25 = load %struct.chromosome*, %struct.chromosome** %24
store %struct.chromosome* %25, %struct.chromosome** %12
%26 = load %struct.chromosome**, %struct.chromosome*** %8
%27 = call i32 (...) @rand()
%28 = load i32, i32* %10
%29 = srem i32 %27, %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %26, i64 %30
%32 = load %struct.chromosome*, %struct.chromosome** %31
store %struct.chromosome* %32, %struct.chromosome** %13
%33 = load %struct.chromosome*, %struct.chromosome** %12
%34 = call i64 @getChromosomeFitness(%struct.chromosome* %33)
%35 = load %struct.chromosome*, %struct.chromosome** %13
%36 = call i64 @getChromosomeFitness(%struct.chromosome* %35)
%37 = icmp sle i64 %34, %36
br i1 %37, label %38, label %46
38: ; preds = %18
%39 = load %struct.chromosome**, %struct.chromosome*** %7
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %39, i64 %41
%43 = load %struct.chromosome*, %struct.chromosome** %42
%44 = load %struct.chromosome*, %struct.chromosome** %12
%45 = call i32 @copyChromosome(%struct.chromosome* %43, %struct.chromosome* %44)
br label %54
46: ; preds = %18
%47 = load %struct.chromosome**, %struct.chromosome*** %7
%48 = load i32, i32* %11
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %47, i64 %49
%51 = load %struct.chromosome*, %struct.chromosome** %50
%52 = load %struct.chromosome*, %struct.chromosome** %13
%53 = call i32 @copyChromosome(%struct.chromosome* %51, %struct.chromosome* %52)
br label %54
54: ; preds = %46, %38
br label %55
55: ; preds = %54
%56 = load i32, i32* %11
%57 = add nsw i32 %56, 1
store i32 %57, i32* %11
br label %14
58: ; preds = %14
ret void
}

LLVM IR (-O2):
%struct.parameters = type { i32 }
%struct.chromosome = type { i32 }
define void @tournament(%struct.parameters* nocapture readnone %0, %struct.chromosome** nocapture %1, %struct.chromosome** nocapture %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %30
7: ; preds = %5
%8 = zext i32 %3 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %28, %9 ]
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %2, i64 %13
%15 = load %struct.chromosome*, %struct.chromosome** %14
%16 = tail call i32 (...) @rand()
%17 = srem i32 %16, %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %2, i64 %18
%20 = load %struct.chromosome*, %struct.chromosome** %19
%21 = tail call i64 @getChromosomeFitness(%struct.chromosome* %15)
%22 = tail call i64 @getChromosomeFitness(%struct.chromosome* %20)
%23 = icmp sgt i64 %21, %22
%24 = getelementptr inbounds %struct.chromosome*, %struct.chromosome** %1, i64 %10
%25 = load %struct.chromosome*, %struct.chromosome** %24
%26 = select i1 %23, %struct.chromosome* %20, %struct.chromosome* %15
%27 = tail call i32 @copyChromosome(%struct.chromosome* %25, %struct.chromosome* %26)
%28 = add nuw nsw i64 %10, 1
%29 = icmp eq i64 %28, %8
br i1 %29, label %30, label %9
30: ; preds = %9, %5
ret void
}