#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 */
scalar_t__ isSquare(int);
int getMaxNumBallsCanPlaced(int numPeg) {
  int topPeg[50], ball, numPegUsed, peg, placed;
  numPegUsed = 0;
  ball = 1;
  while (1) {
    placed = 0;
    for (peg = 0; peg < numPegUsed; peg++)
      if (isSquare(ball + topPeg[peg])) {
        placed = 1;
        topPeg[peg] = ball;
      }
    if (!placed) {
      if (numPegUsed < numPeg) {
        topPeg[numPegUsed] = ball;
        numPegUsed++;
      } else
        return ball - 1;
    }
    ball++;
  }
}


x86 assembly (clang, -O0):
<getMaxNumBallsCanPlaced>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xd8(%rbp)
movl   $0x1,-0xd4(%rbp)
movl   $0x0,-0xe0(%rbp)
movl   $0x0,-0xdc(%rbp)
mov    -0xdc(%rbp),%eax
cmp    -0xd8(%rbp),%eax
jge    11b2 <getMaxNumBallsCanPlaced+0xa2>
mov    -0xd4(%rbp),%edi
movslq -0xdc(%rbp),%rax
add    -0xd0(%rbp,%rax,4),%edi
call   1030 <isSquare@plt>
cmp    $0x0,%rax
je     1199 <getMaxNumBallsCanPlaced+0x89>
movl   $0x1,-0xe0(%rbp)
mov    -0xd4(%rbp),%ecx
movslq -0xdc(%rbp),%rax
mov    %ecx,-0xd0(%rbp,%rax,4)
jmp    119e <getMaxNumBallsCanPlaced+0x8e>
mov    -0xdc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xdc(%rbp)
jmp    1146 <getMaxNumBallsCanPlaced+0x36>
cmpl   $0x0,-0xe0(%rbp)
jne    120d <getMaxNumBallsCanPlaced+0xfd>
mov    -0xd8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11f6 <getMaxNumBallsCanPlaced+0xe6>
mov    -0xd4(%rbp),%ecx
movslq -0xd8(%rbp),%rax
mov    %ecx,-0xd0(%rbp,%rax,4)
mov    -0xd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd8(%rbp)
jmp    1208 <getMaxNumBallsCanPlaced+0xf8>
mov    -0xd4(%rbp),%eax
sub    $0x1,%eax
add    $0xe0,%rsp
pop    %rbp
ret
jmp    120d <getMaxNumBallsCanPlaced+0xfd>
mov    -0xd4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd4(%rbp)
jmp    1132 <getMaxNumBallsCanPlaced+0x22>

x86 assembly (clang, -O2):
<getMaxNumBallsCanPlaced>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    %edi,%r14d
xor    %r15d,%r15d
mov    $0x1,%ebx
jmp    1138 <getMaxNumBallsCanPlaced+0x28>
xchg   %ax,%ax
test   %r13d,%r13d
je     1180 <getMaxNumBallsCanPlaced+0x70>
add    $0x1,%ebx
test   %r15d,%r15d
jle    1180 <getMaxNumBallsCanPlaced+0x70>
mov    %r15d,%r12d
xor    %ebp,%ebp
xor    %r13d,%r13d
jmp    1159 <getMaxNumBallsCanPlaced+0x49>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%r12
je     1130 <getMaxNumBallsCanPlaced+0x20>
mov    (%rsp,%rbp,4),%edi
add    %ebx,%edi
call   1030 <isSquare@plt>
test   %rax,%rax
je     1150 <getMaxNumBallsCanPlaced+0x40>
mov    %ebx,(%rsp,%rbp,4)
mov    $0x1,%r13d
jmp    1150 <getMaxNumBallsCanPlaced+0x40>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %r14d,%r15d
jge    1191 <getMaxNumBallsCanPlaced+0x81>
movslq %r15d,%rax
mov    %ebx,(%rsp,%rax,4)
add    $0x1,%r15d
jmp    1135 <getMaxNumBallsCanPlaced+0x25>
add    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getMaxNumBallsCanPlaced(i32 %0) {
%2 = alloca i32
%3 = alloca [50 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
store i32 1, i32* %4
br label %8
8: ; preds = %1, %49
store i32 0, i32* %7
store i32 0, i32* %6
br label %9
9: ; preds = %28, %8
%10 = load i32, i32* %6
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %31
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [50 x i32], [50 x i32]* %3, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = add nsw i32 %14, %18
%20 = call i64 @isSquare(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %13
store i32 1, i32* %7
%23 = load i32, i32* %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [50 x i32], [50 x i32]* %3, i64 0, i64 %25
store i32 %23, i32* %26
br label %27
27: ; preds = %22, %13
br label %28
28: ; preds = %27
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %9
31: ; preds = %9
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
br i1 %33, label %49, label %34
34: ; preds = %31
%35 = load i32, i32* %5
%36 = load i32, i32* %2
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %45
38: ; preds = %34
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [50 x i32], [50 x i32]* %3, i64 0, i64 %41
store i32 %39, i32* %42
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %48
45: ; preds = %34
%46 = load i32, i32* %4
%47 = sub nsw i32 %46, 1
ret i32 %47
48: ; preds = %38
br label %49
49: ; preds = %48, %31
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
br label %8
}

LLVM IR (-O2):
define i32 @getMaxNumBallsCanPlaced(i32 %0) {
%2 = alloca [50 x i32]
%3 = bitcast [50 x i32]* %2 to i8*
br label %4
4: ; preds = %33, %1
%5 = phi i32 [ 1, %1 ], [ %35, %33 ]
%6 = phi i32 [ 0, %1 ], [ %34, %33 ]
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %25
8: ; preds = %4
%9 = zext i32 %6 to i64
br label %10
10: ; preds = %8, %19
%11 = phi i64 [ 0, %8 ], [ %21, %19 ]
%12 = phi i32 [ 0, %8 ], [ %20, %19 ]
%13 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 %11
%14 = load i32, i32* %13
%15 = add nsw i32 %14, %5
%16 = tail call i64 @isSquare(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %10
store i32 %5, i32* %13
br label %19
19: ; preds = %10, %18
%20 = phi i32 [ 1, %18 ], [ %12, %10 ]
%21 = add nuw nsw i64 %11, 1
%22 = icmp eq i64 %21, %9
br i1 %22, label %23, label %10
23: ; preds = %19
%24 = icmp eq i32 %20, 0
br i1 %24, label %25, label %33
25: ; preds = %4, %23
%26 = icmp slt i32 %6, %0
br i1 %26, label %27, label %31
27: ; preds = %25
%28 = sext i32 %6 to i64
%29 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 %28
store i32 %5, i32* %29
%30 = add nsw i32 %6, 1
br label %33
31: ; preds = %25
%32 = add nsw i32 %5, -1
ret i32 %32
33: ; preds = %27, %23
%34 = phi i32 [ %6, %23 ], [ %30, %27 ]
%35 = add nuw nsw i32 %5, 1
br label %4
}