#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 */
scalar_t__ RAND_MAX;
scalar_t__ pow(double, int);
scalar_t__ rand();
double random_weight(int count_prev, int count_aft) {
  double randdouble = count_aft * (double)pow(-1.0, (int)rand()) *
                      (double)rand() / (count_prev * (double)RAND_MAX);
  if (randdouble == 0)
    randdouble = count_aft * (double)pow(-1.0, (int)rand()) * (double)rand() /
                 (count_prev * (double)RAND_MAX);
  return randdouble;
}


x86 assembly (clang, -O0):
<random_weight>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,%edi
movsd  0xeb7(%rip),%xmm0
call   1030 <pow@plt>
movsd  -0x20(%rbp),%xmm0
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
movsd  -0x18(%rbp),%xmm0
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
mov    0x2e66(%rip),%rax
cvtsi2sdq (%rax),%xmm2
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1212 <random_weight+0xf2>
jp     1212 <random_weight+0xf2>
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,%edi
movsd  0xe39(%rip),%xmm0
call   1030 <pow@plt>
movsd  -0x30(%rbp),%xmm0
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
movsd  -0x28(%rbp),%xmm0
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
mov    0x2de8(%rip),%rax
cvtsi2sdq (%rax),%xmm2
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_weight>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
cvtsi2sd %esi,%xmm0
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1040 <rand@plt>
movsd  0xec1(%rip),%xmm0
mov    %eax,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  (%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1040 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0x8(%rsp),%xmm0
cvtsi2sd %ebx,%xmm2
mov    0x2e6f(%rip),%rbx
cvtsi2sdq (%rbx),%xmm1
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11ee <random_weight+0xce>
jp     11ee <random_weight+0xce>
xor    %eax,%eax
movsd  %xmm2,0x8(%rsp)
call   1040 <rand@plt>
movsd  0xe59(%rip),%xmm0
mov    %eax,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xor    %eax,%eax
call   1040 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  (%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq (%rbx),%xmm1
movsd  0x8(%rsp),%xmm2
mulsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define double @random_weight(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = sitofp i32 %6 to double
%8 = call i64 (...) @rand()
%9 = trunc i64 %8 to i32
%10 = call i64 @pow(double -1.000000e+00, i32 %9)
%11 = sitofp i64 %10 to double
%12 = fmul double %7, %11
%13 = call i64 (...) @rand()
%14 = sitofp i64 %13 to double
%15 = fmul double %12, %14
%16 = load i32, i32* %3
%17 = sitofp i32 %16 to double
%18 = load i64, i64* @RAND_MAX
%19 = sitofp i64 %18 to double
%20 = fmul double %17, %19
%21 = fdiv double %15, %20
store double %21, double* %5
%22 = load double, double* %5
%23 = fcmp oeq double %22, 0.000000e+00
br i1 %23, label %24, label %41
24: ; preds = %2
%25 = load i32, i32* %4
%26 = sitofp i32 %25 to double
%27 = call i64 (...) @rand()
%28 = trunc i64 %27 to i32
%29 = call i64 @pow(double -1.000000e+00, i32 %28)
%30 = sitofp i64 %29 to double
%31 = fmul double %26, %30
%32 = call i64 (...) @rand()
%33 = sitofp i64 %32 to double
%34 = fmul double %31, %33
%35 = load i32, i32* %3
%36 = sitofp i32 %35 to double
%37 = load i64, i64* @RAND_MAX
%38 = sitofp i64 %37 to double
%39 = fmul double %36, %38
%40 = fdiv double %34, %39
store double %40, double* %5
br label %41
41: ; preds = %24, %2
%42 = load double, double* %5
ret double %42
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define double @random_weight(i32 %0, i32 %1) {
%3 = sitofp i32 %1 to double
%4 = tail call i64 (...) @rand()
%5 = trunc i64 %4 to i32
%6 = tail call i64 @pow(double -1.000000e+00, i32 %5)
%7 = sitofp i64 %6 to double
%8 = fmul double %3, %7
%9 = tail call i64 (...) @rand()
%10 = sitofp i64 %9 to double
%11 = fmul double %8, %10
%12 = sitofp i32 %0 to double
%13 = load i64, i64* @RAND_MAX
%14 = sitofp i64 %13 to double
%15 = fmul double %12, %14
%16 = fdiv double %11, %15
%17 = fcmp oeq double %16, 0.000000e+00
br i1 %17, label %18, label %31
18: ; preds = %2
%19 = tail call i64 (...) @rand()
%20 = trunc i64 %19 to i32
%21 = tail call i64 @pow(double -1.000000e+00, i32 %20)
%22 = sitofp i64 %21 to double
%23 = fmul double %3, %22
%24 = tail call i64 (...) @rand()
%25 = sitofp i64 %24 to double
%26 = fmul double %23, %25
%27 = load i64, i64* @RAND_MAX
%28 = sitofp i64 %27 to double
%29 = fmul double %12, %28
%30 = fdiv double %26, %29
br label %31
31: ; preds = %18, %2
%32 = phi double [ %30, %18 ], [ %16, %2 ]
ret double %32
}