#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 */
int abs(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ init();
int nrects(int, int);
int nrects2(int, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int square = 0;
  int delta = 1000 * 1000 * 10;
  int i, j;
  int nr = nrects(3, 2);
  if (nr != 18) {
    fprintf(stderr, "nrects(3,2)=%d\n", nr);
    return -1;
  }
  init();
  for (i = 1; i < 1000; ++i) {
    for (j = 0; j < 1000; ++j) {
      int ldelta;
      nr = nrects2(i, j);
      ldelta = abs(2000000 - nr);
      if (delta > ldelta) {
        delta = ldelta;
        square = i * j;
      }
    }
    printf(">>%d\n", i);
  }
  printf("DELTA=%d SQ=%d\n", delta, square);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x989680,-0x18(%rbp)
mov    $0x3,%edi
mov    $0x2,%esi
call   1050 <nrects@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x12,-0x24(%rbp)
je     11c4 <main+0x64>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe4d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <main+0x11a>
mov    $0x0,%al
call   1070 <init@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jge    125f <main+0xff>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3e8,-0x20(%rbp)
jge    1240 <main+0xe0>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1080 <nrects2@plt>
mov    %eax,-0x24(%rbp)
mov    $0x1e8480,%edi
sub    -0x24(%rbp),%edi
call   1030 <abs@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    122d <main+0xcd>
mov    -0x28(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1232 <main+0xd2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e6 <main+0x86>
mov    -0x1c(%rbp),%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d2 <main+0x72>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x3,%edi
mov    $0x2,%esi
call   1040 <nrects@plt>
cmp    $0x12,%eax
jne    1209 <main+0xb9>
xor    %eax,%eax
call   1060 <init@plt>
mov    $0x989680,%ebp
mov    $0x1,%r15d
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
xor    %r13d,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%r14d
mov    %r15d,%edi
mov    %ebx,%esi
call   1070 <nrects2@plt>
mov    $0x1e8480,%ecx
sub    %eax,%ecx
mov    %ecx,%ebp
neg    %ebp
cmovs  %ecx,%ebp
cmp    %ebp,%r14d
cmovg  %r13d,%r12d
cmovle %r14d,%ebp
add    $0x1,%ebx
add    %r15d,%r13d
cmp    $0x3e8,%ebx
jne    11a0 <main+0x50>
lea    0xe35(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r15d
cmp    $0x3e8,%r15d
jne    1190 <main+0x40>
lea    0xe1d(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1225 <main+0xd5>
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%edi
lea    0xde7(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"nrects(3,2)=%d\0A\00"
@.str.1 = [6 x i8] c">>%d\0A\00"
@.str.2 = [16 x i8] c"DELTA=%d SQ=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 10000000, i32* %7
%12 = call i32 @nrects(i32 3, i32 2)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp ne i32 %13, 18
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* %10
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 -1, i32* %3
br label %57
19: ; preds = %2
%20 = call i32 (...) @init()
store i32 1, i32* %8
br label %21
21: ; preds = %50, %19
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 1000
br i1 %23, label %24, label %53
24: ; preds = %21
store i32 0, i32* %9
br label %25
25: ; preds = %44, %24
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 1000
br i1 %27, label %28, label %47
28: ; preds = %25
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = call i32 @nrects2(i32 %29, i32 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = sub nsw i32 2000000, %32
%34 = call i32 @abs(i32 %33)
store i32 %34, i32* %11
%35 = load i32, i32* %7
%36 = load i32, i32* %11
%37 = icmp sgt i32 %35, %36
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = load i32, i32* %11
store i32 %39, i32* %7
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = mul nsw i32 %40, %41
store i32 %42, i32* %6
br label %43
43: ; preds = %38, %28
br label %44
44: ; preds = %43
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %25
47: ; preds = %25
%48 = load i32, i32* %8
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %47
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %21
53: ; preds = %21
%54 = load i32, i32* %7
%55 = load i32, i32* %6
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %54, i32 %55)
store i32 0, i32* %3
br label %57
57: ; preds = %53, %15
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"nrects(3,2)=%d\0A\00"
@.str.1 = [6 x i8] c">>%d\0A\00"
@.str.2 = [16 x i8] c"DELTA=%d SQ=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @nrects(i32 3, i32 2)
%4 = icmp eq i32 %3, 18
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %33
8: ; preds = %2
%9 = tail call i32 (...) @init()
br label %10
10: ; preds = %8, %27
%11 = phi i32 [ 1, %8 ], [ %29, %27 ]
%12 = phi i32 [ 10000000, %8 ], [ %24, %27 ]
%13 = phi i32 [ 0, %8 ], [ %23, %27 ]
br label %14
14: ; preds = %10, %14
%15 = phi i32 [ 0, %10 ], [ %25, %14 ]
%16 = phi i32 [ %12, %10 ], [ %24, %14 ]
%17 = phi i32 [ %13, %10 ], [ %23, %14 ]
%18 = tail call i32 @nrects2(i32 %11, i32 %15)
%19 = sub nsw i32 2000000, %18
%20 = tail call i32 @llvm.abs.i32(i32 %19, i1 true)
%21 = icmp sgt i32 %16, %20
%22 = mul nuw nsw i32 %15, %11
%23 = select i1 %21, i32 %22, i32 %17
%24 = select i1 %21, i32 %20, i32 %16
%25 = add nuw nsw i32 %15, 1
%26 = icmp eq i32 %25, 1000
br i1 %26, label %27, label %14
27: ; preds = %14
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%29 = add nuw nsw i32 %11, 1
%30 = icmp eq i32 %29, 1000
br i1 %30, label %31, label %10
31: ; preds = %27
%32 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %23)
br label %33
33: ; preds = %31, %5
%34 = phi i32 [ -1, %5 ], [ 0, %31 ]
ret i32 %34
}