#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 count_divs(int);
int triangle500(int lim) {
  int i = 3;
  int res = count_divs(i) * count_divs((i + 1) / 2);
  while (res <= lim) {
    i++;
    if (0 == i % 2) {
      res = count_divs(i / 2) * count_divs(i + 1);
    } else {
      res = count_divs(i) * count_divs((i + 1) / 2);
    }
  }
  return i * (i + 1) / 2;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x3,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <count_divs@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <count_divs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     11d7 <triangle500+0xc7>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
xor    %eax,%eax
cmp    %edx,%eax
jne    11a7 <triangle500+0x97>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <count_divs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1030 <count_divs@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    11d2 <triangle500+0xc2>
mov    -0x8(%rbp),%edi
call   1030 <count_divs@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <count_divs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    114d <triangle500+0x3d>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x3,%ebp
mov    $0x3,%edi
call   1030 <count_divs@plt>
mov    %eax,%ebx
mov    $0x2,%edi
call   1030 <count_divs@plt>
imul   %ebx,%eax
cmp    %r14d,%eax
jle    1150 <triangle500+0x40>
lea    0x1(%rbp),%eax
imul   %ebp,%eax
shr    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%ebx
jmp    117f <triangle500+0x6f>
nopw   0x0(%rax,%rax,1)
call   1030 <count_divs@plt>
mov    %eax,%r15d
add    $0x2,%ebx
shr    %ebx
mov    %ebx,%edi
call   1030 <count_divs@plt>
imul   %r15d,%eax
mov    %ebp,%ebx
cmp    %r14d,%eax
jg     113d <triangle500+0x2d>
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
test   $0x1,%bpl
jne    1160 <triangle500+0x50>
shr    %edi
call   1030 <count_divs@plt>
mov    %eax,%r15d
add    $0x2,%ebx
jmp    116d <triangle500+0x5d>

LLVM IR (-O0):
define i32 @triangle500(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 3, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @count_divs(i32 %5)
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = sdiv i32 %8, 2
%10 = call i32 @count_divs(i32 %9)
%11 = mul nsw i32 %6, %10
store i32 %11, i32* %4
br label %12
12: ; preds = %38, %1
%13 = load i32, i32* %4
%14 = load i32, i32* %2
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %39
16: ; preds = %12
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = srem i32 %19, 2
%21 = icmp eq i32 0, %20
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i32, i32* %3
%24 = sdiv i32 %23, 2
%25 = call i32 @count_divs(i32 %24)
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
%28 = call i32 @count_divs(i32 %27)
%29 = mul nsw i32 %25, %28
store i32 %29, i32* %4
br label %38
30: ; preds = %16
%31 = load i32, i32* %3
%32 = call i32 @count_divs(i32 %31)
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
%35 = sdiv i32 %34, 2
%36 = call i32 @count_divs(i32 %35)
%37 = mul nsw i32 %32, %36
store i32 %37, i32* %4
br label %38
38: ; preds = %30, %22
br label %12
39: ; preds = %12
%40 = load i32, i32* %3
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
%43 = mul nsw i32 %40, %42
%44 = sdiv i32 %43, 2
ret i32 %44
}

LLVM IR (-O2):
define i32 @triangle500(i32 %0) {
%2 = tail call i32 @count_divs(i32 3)
%3 = tail call i32 @count_divs(i32 2)
%4 = mul nsw i32 %3, %2
%5 = icmp sgt i32 %4, %0
br i1 %5, label %25, label %6
6: ; preds = %1, %19
%7 = phi i32 [ %8, %19 ], [ 3, %1 ]
%8 = add nuw nsw i32 %7, 1
%9 = and i32 %8, 1
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = lshr i32 %8, 1
%13 = tail call i32 @count_divs(i32 %12)
%14 = add nuw nsw i32 %7, 2
br label %19
15: ; preds = %6
%16 = tail call i32 @count_divs(i32 %8)
%17 = add nuw nsw i32 %7, 2
%18 = lshr i32 %17, 1
br label %19
19: ; preds = %15, %11
%20 = phi i32 [ %18, %15 ], [ %14, %11 ]
%21 = phi i32 [ %16, %15 ], [ %13, %11 ]
%22 = tail call i32 @count_divs(i32 %20)
%23 = mul nsw i32 %22, %21
%24 = icmp sgt i32 %23, %0
br i1 %24, label %25, label %6
25: ; preds = %19, %1
%26 = phi i32 [ 3, %1 ], [ %8, %19 ]
%27 = add nuw nsw i32 %26, 1
%28 = mul nsw i32 %27, %26
%29 = lshr i32 %28, 1
ret i32 %29
}