#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 log(double);
int mandelbrot(double cx, double cy, int max_iterations) {
  double x = 0;
  double y = 0;
  int iter = 0;
  double abs_squared = x * x + y * y;
  while (iter < max_iterations && abs_squared <= 8.0) {
    x = x * x - y * y + cx;
    y = 2 * x * y + cy;
    ++iter;
  }
  return iter - log((log(abs_squared) / log(4.0))) / log(2.0);
}


x86 assembly (clang, -O0):
<mandelbrot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x39(%rbp)
jge    1181 <mandelbrot+0x71>
movsd  0xe8a(%rip),%xmm0
ucomisd -0x38(%rbp),%xmm0
setae  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1191 <mandelbrot+0x81>
jmp    1200 <mandelbrot+0xf0>
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe32(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    115d <mandelbrot+0x4d>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1030 <log@plt>
mov    %eax,-0x48(%rbp)
movsd  0xdf5(%rip),%xmm0
call   1030 <log@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
call   1030 <log@plt>
mov    %eax,-0x44(%rbp)
movsd  0xdcc(%rip),%xmm0
call   1030 <log@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mandelbrot>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %edi,%edi
cmovns %edi,%ebx
xorps  %xmm0,%xmm0
call   1030 <log@plt>
mov    %eax,%ebp
movsd  0xed4(%rip),%xmm0
call   1030 <log@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
call   1030 <log@plt>
mov    %eax,%ebp
movsd  0xeba(%rip),%xmm0
call   1030 <log@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
sub    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mandelbrot(double %0, double %1, i32 %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca double
store double %0, double* %4
store double %1, double* %5
store i32 %2, i32* %6
store double 0.000000e+00, double* %7
store double 0.000000e+00, double* %8
store i32 0, i32* %9
%11 = load double, double* %7
%12 = load double, double* %7
%13 = load double, double* %8
%14 = load double, double* %8
%15 = fmul double %13, %14
%16 = call double @llvm.fmuladd.f64(double %11, double %12, double %15)
store double %16, double* %10
br label %17
17: ; preds = %26, %3
%18 = load i32, i32* %9
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load double, double* %10
%23 = fcmp ole double %22, 8.000000e+00
br label %24
24: ; preds = %21, %17
%25 = phi i1 [ false, %17 ], [ %23, %21 ]
br i1 %25, label %26, label %43
26: ; preds = %24
%27 = load double, double* %7
%28 = load double, double* %7
%29 = load double, double* %8
%30 = load double, double* %8
%31 = fmul double %29, %30
%32 = fneg double %31
%33 = call double @llvm.fmuladd.f64(double %27, double %28, double %32)
%34 = load double, double* %4
%35 = fadd double %33, %34
store double %35, double* %7
%36 = load double, double* %7
%37 = fmul double 2.000000e+00, %36
%38 = load double, double* %8
%39 = load double, double* %5
%40 = call double @llvm.fmuladd.f64(double %37, double %38, double %39)
store double %40, double* %8
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %17
43: ; preds = %24
%44 = load i32, i32* %9
%45 = load double, double* %10
%46 = call i32 @log(double %45)
%47 = call i32 @log(double 4.000000e+00)
%48 = sdiv i32 %46, %47
%49 = sitofp i32 %48 to double
%50 = call i32 @log(double %49)
%51 = call i32 @log(double 2.000000e+00)
%52 = sdiv i32 %50, %51
%53 = sub nsw i32 %44, %52
ret i32 %53
}

LLVM IR (-O2):
define i32 @mandelbrot(double %0, double %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
%5 = select i1 %4, i32 %2, i32 0
%6 = tail call i32 @log(double 0.000000e+00)
%7 = tail call i32 @log(double 4.000000e+00)
%8 = sdiv i32 %6, %7
%9 = sitofp i32 %8 to double
%10 = tail call i32 @log(double %9)
%11 = tail call i32 @log(double 2.000000e+00)
%12 = sdiv i32 %10, %11
%13 = sub nsw i32 %5, %12
ret i32 %13
}