#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 complex;

/* Variables and functions */
double cabs(int);
int mandelbrot(complex p0, int max_step) {
  complex p = p0;
  int step = 0;
  while (cabs(p) < 2.0 && ++step < max_step) {
    p = p * p + p0;
  }
  return step;
}


x86 assembly (clang, -O0):
<mandelbrot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <cabs@plt>
movaps %xmm0,%xmm1
xor    %eax,%eax
movsd  0xec0(%rip),%xmm0
ucomisd %xmm1,%xmm0
mov    %al,-0x11(%rbp)
jbe    115f <mandelbrot+0x4f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmp    -0x8(%rbp),%eax
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    116f <mandelbrot+0x5f>
jmp    1181 <mandelbrot+0x71>
mov    -0xc(%rbp),%eax
imul   -0xc(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    112b <mandelbrot+0x1b>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mandelbrot>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
call   1030 <cabs@plt>
xor    %ebp,%ebp
movsd  0xed4(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1178 <mandelbrot+0x68>
cmp    $0x2,%ebx
mov    $0x1,%r15d
cmovge %ebx,%r15d
lea    -0x1(%r15),%r12d
xor    %ebp,%ebp
mov    %r14d,%ebx
nopl   0x0(%rax,%rax,1)
cmp    %ebp,%r12d
je     1175 <mandelbrot+0x65>
imul   %ebx,%ebx
add    %r14d,%ebx
mov    %ebx,%edi
call   1030 <cabs@plt>
add    $0x1,%ebp
movsd  0xe93(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     1150 <mandelbrot+0x40>
jmp    1178 <mandelbrot+0x68>
mov    %r15d,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mandelbrot(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
store i32 %7, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %19, %2
%9 = load i32, i32* %5
%10 = call double @cabs(i32 %9)
%11 = fcmp olt double %10, 2.000000e+00
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
store i32 %14, i32* %6
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br label %17
17: ; preds = %12, %8
%18 = phi i1 [ false, %8 ], [ %16, %12 ]
br i1 %18, label %19, label %25
19: ; preds = %17
%20 = load i32, i32* %5
%21 = load i32, i32* %5
%22 = mul nsw i32 %20, %21
%23 = load i32, i32* %3
%24 = add nsw i32 %22, %23
store i32 %24, i32* %5
br label %8
25: ; preds = %17
%26 = load i32, i32* %6
ret i32 %26
}

LLVM IR (-O2):
define i32 @mandelbrot(i32 %0, i32 %1) {
%3 = tail call double @cabs(i32 %0)
%4 = fcmp olt double %3, 2.000000e+00
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = call i32 @llvm.smax.i32(i32 %1, i32 1)
%7 = add nsw i32 %6, -1
br label %8
8: ; preds = %5, %13
%9 = phi i32 [ %11, %13 ], [ 0, %5 ]
%10 = phi i32 [ %15, %13 ], [ %0, %5 ]
%11 = add nuw nsw i32 %9, 1
%12 = icmp eq i32 %9, %7
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = mul nsw i32 %10, %10
%15 = add nsw i32 %14, %0
%16 = tail call double @cabs(i32 %15)
%17 = fcmp olt double %16, 2.000000e+00
br i1 %17, label %8, label %18
18: ; preds = %8, %13, %2
%19 = phi i32 [ 0, %2 ], [ %6, %8 ], [ %11, %13 ]
ret i32 %19
}