#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 Population {
  int dummy;
};
struct Individual {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ fight(struct Individual *, struct Individual *);
struct Individual *pick_random_individual(struct Population *);
struct Individual *tournament_selection(struct Population *city,
                                        int num_rounds) {
  assert(city != NULL);
  assert(num_rounds > 0);
  struct Individual *best = pick_random_individual(city);
  struct Individual *rival;
  int round;
  for (round = 1; round <= num_rounds; round += 1) {
    rival = pick_random_individual(city);
    if (fight(best, rival)) {
      best = rival;
    }
  }
  return best;
}


x86 assembly (clang, -O0):
<tournament_selection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <pick_random_individual@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11c1 <tournament_selection+0x91>
mov    -0x8(%rbp),%rdi
call   1030 <pick_random_individual@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fight@plt>
cmp    $0x0,%rax
je     11ae <tournament_selection+0x7e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11b3 <tournament_selection+0x83>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1176 <tournament_selection+0x46>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tournament_selection>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1040 <assert@plt>
mov    %r14,%rdi
call   1030 <pick_random_individual@plt>
mov    %rax,%r15
test   %ebx,%ebx
jg     1185 <tournament_selection+0x55>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
je     1167 <tournament_selection+0x37>
mov    %r14,%rdi
call   1030 <pick_random_individual@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <fight@plt>
test   %rax,%rax
je     1180 <tournament_selection+0x50>
mov    %r12,%r15
jmp    1180 <tournament_selection+0x50>

LLVM IR (-O0):
%struct.Individual = type { i32 }
%struct.Population = type { i32 }
define %struct.Individual* @tournament_selection(%struct.Population* %0, i32 %1) {
%3 = alloca %struct.Population*
%4 = alloca i32
%5 = alloca %struct.Individual*
%6 = alloca %struct.Individual*
%7 = alloca i32
store %struct.Population* %0, %struct.Population** %3
store i32 %1, i32* %4
%8 = load %struct.Population*, %struct.Population** %3
%9 = icmp ne %struct.Population* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load %struct.Population*, %struct.Population** %3
%17 = call %struct.Individual* @pick_random_individual(%struct.Population* %16)
store %struct.Individual* %17, %struct.Individual** %5
store i32 1, i32* %7
br label %18
18: ; preds = %32, %2
%19 = load i32, i32* %7
%20 = load i32, i32* %4
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load %struct.Population*, %struct.Population** %3
%24 = call %struct.Individual* @pick_random_individual(%struct.Population* %23)
store %struct.Individual* %24, %struct.Individual** %6
%25 = load %struct.Individual*, %struct.Individual** %5
%26 = load %struct.Individual*, %struct.Individual** %6
%27 = call i64 @fight(%struct.Individual* %25, %struct.Individual* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = load %struct.Individual*, %struct.Individual** %6
store %struct.Individual* %30, %struct.Individual** %5
br label %31
31: ; preds = %29, %22
br label %32
32: ; preds = %31
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %18
35: ; preds = %18
%36 = load %struct.Individual*, %struct.Individual** %5
ret %struct.Individual* %36
}

LLVM IR (-O2):
%struct.Individual = type { i32 }
%struct.Population = type { i32 }
define %struct.Individual* @tournament_selection(%struct.Population* %0, i32 %1) {
%3 = icmp ne %struct.Population* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp sgt i32 %1, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call %struct.Individual* @pick_random_individual(%struct.Population* %0)
%10 = icmp slt i32 %1, 1
br i1 %10, label %20, label %11
11: ; preds = %2, %11
%12 = phi i32 [ %18, %11 ], [ 1, %2 ]
%13 = phi %struct.Individual* [ %17, %11 ], [ %9, %2 ]
%14 = tail call %struct.Individual* @pick_random_individual(%struct.Population* %0)
%15 = tail call i64 @fight(%struct.Individual* %13, %struct.Individual* %14)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, %struct.Individual* %13, %struct.Individual* %14
%18 = add nuw i32 %12, 1
%19 = icmp eq i32 %12, %1
br i1 %19, label %20, label %11
20: ; preds = %11, %2
%21 = phi %struct.Individual* [ %9, %2 ], [ %17, %11 ]
ret %struct.Individual* %21
}