#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 M_PI;
scalar_t__ RAND_MAX;
double cos(double);
int log(double);
double rand();
double sin(double);
double sqrt(int);
double boxmuller() {
  static unsigned short cont = 3;
  static double a, b;
  if (cont > 2) {
    a = sqrt(-2 * log((rand() / ((double)RAND_MAX))));
    b = 2 * M_PI * ((rand() / ((double)RAND_MAX)));
    cont = 1;
  }
  return (cont++ == 1 ? a * sin(b) : a * cos(b));
}


x86 assembly (clang, -O0):
<boxmuller>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movzwl 0x2ee9(%rip),%eax
cmp    $0x2,%eax
jle    11e0 <boxmuller+0x90>
mov    $0x0,%al
call   1070 <rand@plt>
mov    0x2e6a(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
call   1050 <log@plt>
imul   $0xfffffffe,%eax,%edi
call   1060 <sqrt@plt>
movsd  %xmm0,0x2ec1(%rip)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    $0x0,%al
call   1070 <rand@plt>
movaps %xmm0,%xmm1
movsd  -0x8(%rbp),%xmm0
mov    0x2e1e(%rip),%rax
cvtsi2sdq (%rax),%xmm2
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2e89(%rip)
movw   $0x1,0x2e68(%rip)
mov    0x2e61(%rip),%ax
mov    %ax,%cx
add    $0x1,%cx
mov    %cx,0x2e53(%rip)
movzwl %ax,%eax
cmp    $0x1,%eax
jne    1231 <boxmuller+0xe1>
movsd  0x2e4f(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0x2e4a(%rip),%xmm0
call   1030 <sin@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    125c <boxmuller+0x10c>
movsd  0x2e1f(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0x2e1a(%rip),%xmm0
call   1040 <cos@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<boxmuller>:
push   %rbx
sub    $0x10,%rsp
movzwl 0x2eec(%rip),%eax
cmp    $0x3,%ax
jb     11d4 <boxmuller+0x84>
xor    %eax,%eax
call   1070 <rand@plt>
mov    0x2e70(%rip),%rbx
cvtsi2sdq (%rbx),%xmm1
divsd  %xmm1,%xmm0
call   1050 <log@plt>
lea    (%rax,%rax,1),%edi
neg    %edi
call   1060 <sqrt@plt>
movsd  %xmm0,0x2ec8(%rip)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
add    %eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1070 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sdq (%rbx),%xmm1
divsd  %xmm1,%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x2e97(%rip)
movw   $0x2,0x2e76(%rip)
jmp    11ec <boxmuller+0x9c>
lea    0x1(%rax),%ecx
mov    %cx,0x2e6a(%rip)
cmp    $0x1,%ax
jne    1201 <boxmuller+0xb1>
movsd  0x2e74(%rip),%xmm0
movsd  0x2e64(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
call   1030 <sin@plt>
jmp    121c <boxmuller+0xcc>
movsd  0x2e4f(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  0x2e49(%rip),%xmm0
call   1040 <cos@plt>
mulsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@boxmuller.cont = internal global i16 3
@boxmuller.a = internal global double 0.000000e+00
@boxmuller.b = internal global double 0.000000e+00
@RAND_MAX = global i64 0
@M_PI = global i32 0
define double @boxmuller() {
%1 = load i16, i16* @boxmuller.cont
%2 = zext i16 %1 to i32
%3 = icmp sgt i32 %2, 2
br i1 %3, label %4, label %20
4: ; preds = %0
%5 = call double (...) @rand()
%6 = load i64, i64* @RAND_MAX
%7 = sitofp i64 %6 to double
%8 = fdiv double %5, %7
%9 = call i32 @log(double %8)
%10 = mul nsw i32 -2, %9
%11 = call double @sqrt(i32 %10)
store double %11, double* @boxmuller.a
%12 = load i32, i32* @M_PI
%13 = mul nsw i32 2, %12
%14 = sitofp i32 %13 to double
%15 = call double (...) @rand()
%16 = load i64, i64* @RAND_MAX
%17 = sitofp i64 %16 to double
%18 = fdiv double %15, %17
%19 = fmul double %14, %18
store double %19, double* @boxmuller.b
store i16 1, i16* @boxmuller.cont
br label %20
20: ; preds = %4, %0
%21 = load i16, i16* @boxmuller.cont
%22 = add i16 %21, 1
store i16 %22, i16* @boxmuller.cont
%23 = zext i16 %21 to i32
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load double, double* @boxmuller.a
%27 = load double, double* @boxmuller.b
%28 = call double @sin(double %27)
%29 = fmul double %26, %28
br label %35
30: ; preds = %20
%31 = load double, double* @boxmuller.a
%32 = load double, double* @boxmuller.b
%33 = call double @cos(double %32)
%34 = fmul double %31, %33
br label %35
35: ; preds = %30, %25
%36 = phi double [ %29, %25 ], [ %34, %30 ]
ret double %36
}

LLVM IR (-O2):
@boxmuller.cont = internal unnamed_addr global i16 3
@boxmuller.a = internal unnamed_addr global double 0.000000e+00
@boxmuller.b = internal unnamed_addr global double 0.000000e+00
@RAND_MAX = global i64 0
@M_PI = global i32 0
define double @boxmuller() {
%1 = load i16, i16* @boxmuller.cont
%2 = icmp ugt i16 %1, 2
br i1 %2, label %3, label %19
3: ; preds = %0
%4 = tail call double (...) @rand()
%5 = load i64, i64* @RAND_MAX
%6 = sitofp i64 %5 to double
%7 = fdiv double %4, %6
%8 = tail call i32 @log(double %7)
%9 = mul nsw i32 %8, -2
%10 = tail call double @sqrt(i32 %9)
store double %10, double* @boxmuller.a
%11 = load i32, i32* @M_PI
%12 = shl nsw i32 %11, 1
%13 = sitofp i32 %12 to double
%14 = tail call double (...) @rand()
%15 = load i64, i64* @RAND_MAX
%16 = sitofp i64 %15 to double
%17 = fdiv double %14, %16
%18 = fmul double %17, %13
store double %18, double* @boxmuller.b
store i16 2, i16* @boxmuller.cont
br label %24
19: ; preds = %0
%20 = add nuw nsw i16 %1, 1
store i16 %20, i16* @boxmuller.cont
%21 = icmp eq i16 %1, 1
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load double, double* @boxmuller.b
br label %24
24: ; preds = %22, %3
%25 = phi double [ %23, %22 ], [ %18, %3 ]
%26 = load double, double* @boxmuller.a
%27 = tail call double @sin(double %25)
%28 = fmul double %26, %27
br label %34
29: ; preds = %19
%30 = load double, double* @boxmuller.a
%31 = load double, double* @boxmuller.b
%32 = tail call double @cos(double %31)
%33 = fmul double %30, %32
br label %34
34: ; preds = %29, %24
%35 = phi double [ %28, %24 ], [ %33, %29 ]
ret double %35
}