#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 */
double compare(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
char *random_string(int);
char **reproduce(int, char *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int main() {
  int i, finished = 0, generation = 1;
  int length = strlen("METHINKS IT IS LIKE A WEASEL");
  char *parent = random_string(length);
  char **children;
  srand(time(0));
  while (!finished) {
    int top_index = 0;
    float top_score = 0.0;
    children = reproduce(50, parent, length);
    printf("Generation #%i\n", generation);
    printf("Children:\n");
    for (i = 0; i < 50; i++) {
      float this_score = compare(children[i], "METHINKS IT IS LIKE A WEASEL");
      printf("%s\t", children[i]);
      if (this_score > top_score) {
        top_score = this_score;
        top_index = i;
      }
    }
    strncpy(parent, children[top_index], length + 1);
    printf("\n\nNew parent: %s\n\n", parent);
    for (i = 0; i < 50; i++) {
      free(children[i]);
    }
    if (compare(parent, "METHINKS IT IS LIKE A WEASEL") == 1.0) {
      finished = 1;
    }
    generation++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
lea    0xe54(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <random_string@plt>
mov    %rax,-0x20(%rbp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ec <main+0x5c>
jmp    1347 <main+0x1b7>
movl   $0x0,-0x2c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x32,%edi
call   10a0 <reproduce@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x32,-0x8(%rbp)
jge    12ad <main+0x11d>
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdb5(%rip),%rsi
call   10b0 <compare@plt>
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movss  -0x34(%rbp),%xmm0
ucomiss -0x30(%rbp),%xmm0
jbe    129a <main+0x10a>
movss  -0x34(%rbp),%xmm0
movss  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    129f <main+0x10f>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1236 <main+0xa6>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x32,-0x8(%rbp)
jge    130a <main+0x17a>
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12e1 <main+0x151>
mov    -0x20(%rbp),%rdi
lea    0xcf3(%rip),%rsi
call   10b0 <compare@plt>
movsd  0xcde(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1339 <main+0x1a9>
jp     1339 <main+0x1a9>
movl   $0x1,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d6 <main+0x46>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe53(%rip),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <random_string@plt>
mov    %rax,%r15
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
mov    %ebp,0x10(%rsp)
add    $0x1,%ebp
mov    %ebp,0xc(%rsp)
mov    $0x1,%ebp
lea    0xe47(%rip),%r13
mov    %r15,0x20(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x32,%edi
mov    %r15,%rsi
mov    0x10(%rsp),%edx
call   10b0 <reproduce@plt>
mov    %rax,%r14
lea    0xe0a(%rip),%rdi
mov    %ebp,0x14(%rsp)
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xe1d(%rip),%rdi
call   1050 <puts@plt>
xorps  %xmm0,%xmm0
xor    %r15d,%r15d
xor    %r12d,%r12d
nopl   (%rax)
movss  %xmm0,0x1c(%rsp)
mov    (%r14,%r15,8),%rdi
mov    %rbx,%rsi
call   10c0 <compare@plt>
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x18(%rsp)
mov    (%r14,%r15,8),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
movss  0x18(%rsp),%xmm0
movss  0x1c(%rsp),%xmm1
ucomiss %xmm1,%xmm0
cmova  %r15d,%r12d
maxss  %xmm1,%xmm0
add    $0x1,%r15
cmp    $0x32,%r15
jne    1240 <main+0xa0>
movslq %r12d,%rax
mov    (%r14,%rax,8),%rsi
mov    0x20(%rsp),%r15
mov    %r15,%rdi
mov    0xc(%rsp),%edx
call   1040 <strncpy@plt>
lea    0xd8f(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r14,%rbp,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbp
cmp    $0x32,%rbp
jne    12c0 <main+0x120>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10c0 <compare@plt>
mov    0x14(%rsp),%ebp
add    $0x1,%ebp
ucomisd 0xd13(%rip),%xmm0
jne    1200 <main+0x60>
jp     1200 <main+0x60>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"METHINKS IT IS LIKE A WEASEL\00"
@.str.1 = [16 x i8] c"Generation #%i\0A\00"
@.str.2 = [11 x i8] c"Children:\0A\00"
@.str.3 = [4 x i8] c"%s\09\00"
@.str.4 = [19 x i8] c"\0A\0ANew parent: %s\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca float
%10 = alloca float
store i32 0, i32* %1
store i32 0, i32* %3
store i32 1, i32* %4
%11 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = call i8* @random_string(i32 %12)
store i8* %13, i8** %6
%14 = call i32 @time(i32 0)
%15 = call i32 @srand(i32 %14)
br label %16
16: ; preds = %84, %0
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %87
20: ; preds = %16
store i32 0, i32* %8
store float 0.000000e+00, float* %9
%21 = load i8*, i8** %6
%22 = load i32, i32* %5
%23 = call i8** @reproduce(i32 50, i8* %21, i32 %22)
store i8** %23, i8*** %7
%24 = load i32, i32* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %27
27: ; preds = %51, %20
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 50
br i1 %29, label %30, label %54
30: ; preds = %27
%31 = load i8**, i8*** %7
%32 = load i32, i32* %2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = call double @compare(i8* %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%37 = fptrunc double %36 to float
store float %37, float* %10
%38 = load i8**, i8*** %7
%39 = load i32, i32* %2
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load float, float* %10
%45 = load float, float* %9
%46 = fcmp ogt float %44, %45
br i1 %46, label %47, label %50
47: ; preds = %30
%48 = load float, float* %10
store float %48, float* %9
%49 = load i32, i32* %2
store i32 %49, i32* %8
br label %50
50: ; preds = %47, %30
br label %51
51: ; preds = %50
%52 = load i32, i32* %2
%53 = add nsw i32 %52, 1
store i32 %53, i32* %2
br label %27
54: ; preds = %27
%55 = load i8*, i8** %6
%56 = load i8**, i8*** %7
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = load i32, i32* %5
%62 = add nsw i32 %61, 1
%63 = call i32 @strncpy(i8* %55, i8* %60, i32 %62)
%64 = load i8*, i8** %6
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %64)
store i32 0, i32* %2
br label %66
66: ; preds = %76, %54
%67 = load i32, i32* %2
%68 = icmp slt i32 %67, 50
br i1 %68, label %69, label %79
69: ; preds = %66
%70 = load i8**, i8*** %7
%71 = load i32, i32* %2
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
%74 = load i8*, i8** %73
%75 = call i32 @free(i8* %74)
br label %76
76: ; preds = %69
%77 = load i32, i32* %2
%78 = add nsw i32 %77, 1
store i32 %78, i32* %2
br label %66
79: ; preds = %66
%80 = load i8*, i8** %6
%81 = call double @compare(i8* %80, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%82 = fcmp oeq double %81, 1.000000e+00
br i1 %82, label %83, label %84
83: ; preds = %79
store i32 1, i32* %3
br label %84
84: ; preds = %83, %79
%85 = load i32, i32* %4
%86 = add nsw i32 %85, 1
store i32 %86, i32* %4
br label %16
87: ; preds = %16
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"METHINKS IT IS LIKE A WEASEL\00"
@.str.1 = [16 x i8] c"Generation #%i\0A\00"
@.str.3 = [4 x i8] c"%s\09\00"
@.str.4 = [19 x i8] c"\0A\0ANew parent: %s\0A\0A\00"
@str = [10 x i8] c"Children:\00"
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%2 = tail call i8* @random_string(i32 %1)
%3 = tail call i32 @time(i32 0)
%4 = tail call i32 @srand(i32 %3)
%5 = add nsw i32 %1, 1
br label %6
6: ; preds = %0, %40
%7 = phi i32 [ %43, %40 ], [ 1, %0 ]
%8 = tail call i8** @reproduce(i32 50, i8* %2, i32 %1)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %6, %11
%12 = phi i64 [ 0, %6 ], [ %25, %11 ]
%13 = phi float [ 0.000000e+00, %6 ], [ %24, %11 ]
%14 = phi i32 [ 0, %6 ], [ %23, %11 ]
%15 = getelementptr inbounds i8*, i8** %8, i64 %12
%16 = load i8*, i8** %15
%17 = tail call double @compare(i8* %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%18 = fptrunc double %17 to float
%19 = load i8*, i8** %15
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = fcmp olt float %13, %18
%22 = trunc i64 %12 to i32
%23 = select i1 %21, i32 %22, i32 %14
%24 = select i1 %21, float %18, float %13
%25 = add nuw nsw i64 %12, 1
%26 = icmp eq i64 %25, 50
br i1 %26, label %27, label %11
27: ; preds = %11
%28 = sext i32 %23 to i64
%29 = getelementptr inbounds i8*, i8** %8, i64 %28
%30 = load i8*, i8** %29
%31 = tail call i32 @strncpy(i8* %2, i8* %30, i32 %5)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %33
33: ; preds = %27, %33
%34 = phi i64 [ 0, %27 ], [ %38, %33 ]
%35 = getelementptr inbounds i8*, i8** %8, i64 %34
%36 = load i8*, i8** %35
%37 = tail call i32 @free(i8* %36)
%38 = add nuw nsw i64 %34, 1
%39 = icmp eq i64 %38, 50
br i1 %39, label %40, label %33
40: ; preds = %33
%41 = tail call double @compare(i8* %2, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%42 = fcmp oeq double %41, 1.000000e+00
%43 = add nuw nsw i32 %7, 1
br i1 %42, label %44, label %6
44: ; preds = %40
ret i32 0
}