#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 /*<<< orphan*/ mpfr_t;
typedef scalar_t__ mpfr_prec_t;

/* Variables and functions */
scalar_t__ const MPFR_PREC_MIN;
int /*<<< orphan*/ MPFR_RNDD;
int /*<<< orphan*/ MPFR_RNDN;
int /*<<< orphan*/ MPFR_RNDU;
int /*<<< orphan*/ getLastDigits(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                 scalar_t__ const, scalar_t__ const);
int /*<<< orphan*/ m_clear(int /*<<< orphan*/);
int /*<<< orphan*/ m_init2(int /*<<< orphan*/, int /*<<< orphan*/);
int m_setPrecision(int /*<<< orphan*/ *, scalar_t__ const);
int m_setPrecisionWithRoundingMode(int /*<<< orphan*/ *, scalar_t__ const,
                                   int /*<<< orphan*/);
int /*<<< orphan*/ mpfr_abs(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int /*<<< orphan*/ mpfr_get_default_prec();
scalar_t__ mpfr_get_prec(int /*<<< orphan*/);
scalar_t__ mpfr_greater_p(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mpfr_less_p(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setRandomValue(int /*<<< orphan*/ *);
int stochasticRounding(mpfr_t *value, const mpfr_prec_t pre) {
  int roundingDirection = 0;
  const mpfr_prec_t currentPrecision = mpfr_get_prec(*value);
  if (currentPrecision <= pre) {
    roundingDirection = m_setPrecision(value, pre);
  } else {
    const long int lastDigitPrecision = (currentPrecision - pre) > MPFR_PREC_MIN
                                            ? (currentPrecision - pre)
                                            : MPFR_PREC_MIN;
    mpfr_t lastDigits;
    getLastDigits(&lastDigits, *value, currentPrecision, pre);
    mpfr_abs(lastDigits, lastDigits, MPFR_RNDN);
    mpfr_t randomValue;
    m_init2(randomValue, mpfr_get_default_prec());
    setRandomValue(&randomValue);
    if (mpfr_greater_p(lastDigits, randomValue)) {
      roundingDirection = m_setPrecisionWithRoundingMode(value, pre, MPFR_RNDU);
    } else if (mpfr_less_p(lastDigits, randomValue)) {
      roundingDirection = m_setPrecisionWithRoundingMode(value, pre, MPFR_RNDD);
    }
    m_clear(lastDigits);
    m_clear(randomValue);
  }
  return roundingDirection;
}


x86 assembly (clang, -O0):
<stochasticRounding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   10a0 <mpfr_get_prec@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jg     11f9 <stochasticRounding+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <m_setPrecision@plt>
mov    %eax,-0x14(%rbp)
jmp    12f9 <stochasticRounding+0x149>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jle    1222 <stochasticRounding+0x72>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1230 <stochasticRounding+0x80>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
call   10d0 <getLastDigits@plt>
mov    -0x2c(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
call   1030 <mpfr_abs@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1060 <mpfr_get_default_prec@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
call   10c0 <m_init2@plt>
lea    -0x30(%rbp),%rdi
call   1050 <setRandomValue@plt>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1070 <mpfr_greater_p@plt>
cmp    $0x0,%rax
je     12b6 <stochasticRounding+0x106>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
call   1040 <m_setPrecisionWithRoundingMode@plt>
mov    %eax,-0x14(%rbp)
jmp    12e9 <stochasticRounding+0x139>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1090 <mpfr_less_p@plt>
cmp    $0x0,%rax
je     12e4 <stochasticRounding+0x134>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d06(%rip),%rax
mov    (%rax),%edx
call   1040 <m_setPrecisionWithRoundingMode@plt>
mov    %eax,-0x14(%rbp)
jmp    12e9 <stochasticRounding+0x139>
mov    -0x2c(%rbp),%edi
call   10b0 <m_clear@plt>
mov    -0x30(%rbp),%edi
call   10b0 <m_clear@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stochasticRounding>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%edi
call   10a0 <mpfr_get_prec@plt>
cmp    %r14,%rax
jle    1225 <stochasticRounding+0x75>
mov    (%rbx),%esi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
mov    %r14,%rcx
call   10d0 <getLastDigits@plt>
mov    0x8(%rsp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    %edi,%esi
call   1030 <mpfr_abs@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <mpfr_get_default_prec@plt>
mov    %eax,%esi
call   10c0 <m_init2@plt>
lea    0xc(%rsp),%rdi
call   1050 <setRandomValue@plt>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
call   1070 <mpfr_greater_p@plt>
test   %rax,%rax
je     1238 <stochasticRounding+0x88>
mov    0x2dad(%rip),%rax
jmp    1251 <stochasticRounding+0xa1>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <m_setPrecision@plt>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
call   1090 <mpfr_less_p@plt>
test   %rax,%rax
je     1260 <stochasticRounding+0xb0>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <m_setPrecisionWithRoundingMode@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
call   10b0 <m_clear@plt>
mov    0xc(%rsp),%edi
call   10b0 <m_clear@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@MPFR_PREC_MIN = constant i64 0
@MPFR_RNDN = global i32 0
@MPFR_RNDU = global i32 0
@MPFR_RNDD = global i32 0
define i32 @stochasticRounding(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
store i32 0, i32* %5
%10 = load i32*, i32** %3
%11 = load i32, i32* %10
%12 = call i64 @mpfr_get_prec(i32 %11)
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = load i64, i64* %4
%15 = icmp sle i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32*, i32** %3
%18 = load i64, i64* %4
%19 = call i32 @m_setPrecision(i32* %17, i64 %18)
store i32 %19, i32* %5
br label %72
20: ; preds = %2
%21 = load i64, i64* %6
%22 = load i64, i64* %4
%23 = sub nsw i64 %21, %22
%24 = load i64, i64* @MPFR_PREC_MIN
%25 = icmp sgt i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i64, i64* %6
%28 = load i64, i64* %4
%29 = sub nsw i64 %27, %28
br label %32
30: ; preds = %20
%31 = load i64, i64* @MPFR_PREC_MIN
br label %32
32: ; preds = %30, %26
%33 = phi i64 [ %29, %26 ], [ %31, %30 ]
store i64 %33, i64* %7
%34 = load i32*, i32** %3
%35 = load i32, i32* %34
%36 = load i64, i64* %6
%37 = load i64, i64* %4
%38 = call i32 @getLastDigits(i32* %8, i32 %35, i64 %36, i64 %37)
%39 = load i32, i32* %8
%40 = load i32, i32* %8
%41 = load i32, i32* @MPFR_RNDN
%42 = call i32 @mpfr_abs(i32 %39, i32 %40, i32 %41)
%43 = load i32, i32* %9
%44 = call i32 (...) @mpfr_get_default_prec()
%45 = call i32 @m_init2(i32 %43, i32 %44)
%46 = call i32 @setRandomValue(i32* %9)
%47 = load i32, i32* %8
%48 = load i32, i32* %9
%49 = call i64 @mpfr_greater_p(i32 %47, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %32
%52 = load i32*, i32** %3
%53 = load i64, i64* %4
%54 = load i32, i32* @MPFR_RNDU
%55 = call i32 @m_setPrecisionWithRoundingMode(i32* %52, i64 %53, i32 %54)
store i32 %55, i32* %5
br label %67
56: ; preds = %32
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = call i64 @mpfr_less_p(i32 %57, i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = load i32*, i32** %3
%63 = load i64, i64* %4
%64 = load i32, i32* @MPFR_RNDD
%65 = call i32 @m_setPrecisionWithRoundingMode(i32* %62, i64 %63, i32 %64)
store i32 %65, i32* %5
br label %66
66: ; preds = %61, %56
br label %67
67: ; preds = %66, %51
%68 = load i32, i32* %8
%69 = call i32 @m_clear(i32 %68)
%70 = load i32, i32* %9
%71 = call i32 @m_clear(i32 %70)
br label %72
72: ; preds = %67, %16
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
@MPFR_PREC_MIN = constant i64 0
@MPFR_RNDN = global i32 0
@MPFR_RNDU = global i32 0
@MPFR_RNDD = global i32 0
define i32 @stochasticRounding(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* %0
%6 = tail call i64 @mpfr_get_prec(i32 %5)
%7 = icmp sgt i64 %6, %1
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @m_setPrecision(i32* %0, i64 %1)
br label %40
10: ; preds = %2
%11 = bitcast i32* %3 to i8*
%12 = load i32, i32* %0
%13 = call i32 @getLastDigits(i32* %3, i32 %12, i64 %6, i64 %1)
%14 = load i32, i32* %3
%15 = load i32, i32* @MPFR_RNDN
%16 = call i32 @mpfr_abs(i32 %14, i32 %14, i32 %15)
%17 = bitcast i32* %4 to i8*
%18 = call i32 (...) @mpfr_get_default_prec()
%19 = call i32 @m_init2(i32 undef, i32 %18)
%20 = call i32 @setRandomValue(i32* %4)
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = call i64 @mpfr_greater_p(i32 %21, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %10
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = call i64 @mpfr_less_p(i32 %26, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %25, %10
%31 = phi i32* [ @MPFR_RNDU, %10 ], [ @MPFR_RNDD, %25 ]
%32 = load i32, i32* %31
%33 = call i32 @m_setPrecisionWithRoundingMode(i32* %0, i64 %1, i32 %32)
br label %34
34: ; preds = %30, %25
%35 = phi i32 [ 0, %25 ], [ %33, %30 ]
%36 = load i32, i32* %3
%37 = call i32 @m_clear(i32 %36)
%38 = load i32, i32* %4
%39 = call i32 @m_clear(i32 %38)
br label %40
40: ; preds = %34, %8
%41 = phi i32 [ %9, %8 ], [ %35, %34 ]
ret i32 %41
}