#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 */
typedef int /*<<< orphan*/ sq_root;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memset(unsigned int *, int /*<<< orphan*/, int);
int perfect_square(unsigned n, unsigned *sqrt) {
  assert(0 < n && n < 10000);
  static int initialized = 0;
  static unsigned sq_root[100000];
  if (!initialized) {
    memset(sq_root, 0, sizeof(sq_root));
    size_t i;
    for (i = 1; i < 100; i++) {
      sq_root[i * i] = i;
    }
    initialized = 1;
  }
  *sqrt = sq_root[n];
  return *sqrt != 0;
}


x86 assembly (clang, -O0):
<perfect_square>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
xor    %ecx,%ecx
cmp    -0x4(%rbp),%ecx
mov    %al,-0x19(%rbp)
jae    114c <perfect_square+0x2c>
cmpl   $0x2710,-0x4(%rbp)
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,0x2ee0(%rip)
jne    11c0 <perfect_square+0xa0>
lea    0x2ee3(%rip),%rdi
xor    %esi,%esi
mov    $0x61a80,%edx
call   1030 <memset@plt>
movq   $0x1,-0x18(%rbp)
cmpq   $0x64,-0x18(%rbp)
jae    11b6 <perfect_square+0x96>
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
imul   -0x18(%rbp),%rcx
lea    0x2eae(%rip),%rax
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1181 <perfect_square+0x61>
movl   $0x1,0x2e80(%rip)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
lea    0x2e84(%rip),%rax
mov    (%rax,%rcx,4),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perfect_square>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    -0x1(%r15),%eax
xor    %edi,%edi
cmp    $0x270f,%eax
setb   %dil
call   1040 <assert@plt>
cmpb   $0x0,0x2efa(%rip)
jne    11a7 <perfect_square+0x87>
lea    0x2f01(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x61a80,%edx
call   1030 <memset@plt>
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rcx
imul   %rax,%rcx
mov    %eax,(%rbx,%rcx,4)
lea    0x1(%rax),%rcx
mov    %rcx,%rdx
imul   %rcx,%rdx
mov    %ecx,(%rbx,%rdx,4)
lea    0x2(%rax),%rdx
imul   %rdx,%rdx
add    $0x1,%ecx
mov    %ecx,(%rbx,%rdx,4)
add    $0x3,%rax
cmp    $0x64,%rax
jne    1170 <perfect_square+0x50>
movb   $0x1,0x2e99(%rip)
mov    %r15d,%eax
lea    0x2e9f(%rip),%rcx
mov    (%rcx,%rax,4),%ecx
mov    %ecx,(%r14)
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@perfect_square.initialized = internal global i32 0
@perfect_square.sq_root = internal global [100000 x i32] zeroinitializer
define i32 @perfect_square(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
store i32 %0, i32* %3
store i32* %1, i32** %4
%6 = load i32, i32* %3
%7 = icmp ult i32 0, %6
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* %3
%10 = icmp ult i32 %9, 10000
br label %11
11: ; preds = %8, %2
%12 = phi i1 [ false, %2 ], [ %10, %8 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @perfect_square.initialized
%16 = icmp ne i32 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %11
%18 = call i32 @memset(i32* getelementptr inbounds ([100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 0), i32 0, i32 400000)
store i64 1, i64* %5
br label %19
19: ; preds = %29, %17
%20 = load i64, i64* %5
%21 = icmp ult i64 %20, 100
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = load i64, i64* %5
%24 = trunc i64 %23 to i32
%25 = load i64, i64* %5
%26 = load i64, i64* %5
%27 = mul i64 %25, %26
%28 = getelementptr inbounds [100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 %27
store i32 %24, i32* %28
br label %29
29: ; preds = %22
%30 = load i64, i64* %5
%31 = add i64 %30, 1
store i64 %31, i64* %5
br label %19
32: ; preds = %19
store i32 1, i32* @perfect_square.initialized
br label %33
33: ; preds = %32, %11
%34 = load i32, i32* %3
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = load i32*, i32** %4
store i32 %37, i32* %38
%39 = load i32*, i32** %4
%40 = load i32, i32* %39
%41 = icmp ne i32 %40, 0
%42 = zext i1 %41 to i32
ret i32 %42
}

LLVM IR (-O2):
@perfect_square.initialized = internal unnamed_addr global i1 false
@perfect_square.sq_root = internal global [100000 x i32] zeroinitializer
define i32 @perfect_square(i32 %0, i32* nocapture writeonly %1) {
%3 = add i32 %0, -1
%4 = icmp ult i32 %3, 9999
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load i1, i1* @perfect_square.initialized
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = tail call i32 @memset(i32* getelementptr inbounds ([100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 0), i32 0, i32 400000)
br label %10
10: ; preds = %10, %8
%11 = phi i64 [ 1, %8 ], [ %23, %10 ]
%12 = trunc i64 %11 to i32
%13 = mul nuw nsw i64 %11, %11
%14 = getelementptr inbounds [100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 %13
store i32 %12, i32* %14
%15 = add nuw nsw i64 %11, 1
%16 = trunc i64 %15 to i32
%17 = mul nuw nsw i64 %15, %15
%18 = getelementptr inbounds [100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 %17
store i32 %16, i32* %18
%19 = add nuw nsw i64 %11, 2
%20 = trunc i64 %19 to i32
%21 = mul nuw nsw i64 %19, %19
%22 = getelementptr inbounds [100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 %21
store i32 %20, i32* %22
%23 = add nuw nsw i64 %11, 3
%24 = icmp eq i64 %23, 100
br i1 %24, label %25, label %10
25: ; preds = %10
store i1 true, i1* @perfect_square.initialized
br label %26
26: ; preds = %25, %2
%27 = zext i32 %0 to i64
%28 = getelementptr inbounds [100000 x i32], [100000 x i32]* @perfect_square.sq_root, i64 0, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %1
%30 = icmp ne i32 %29, 0
%31 = zext i1 %30 to i32
ret i32 %31
}