#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 */
struct rlimit {
  size_t rlim_cur;
};

/* Variables and functions */
size_t MAX(size_t, int);
int /*<<< orphan*/ RLIMIT_DATA;
size_t SIZE_MAX;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
double physmem_available();
double physmem_total();
size_t default_sponge_size(void) {
  double avail = physmem_available();
  double total = physmem_total();
  double mem = MAX(avail, total / 8);
  struct rlimit rlimit;
  size_t size = SIZE_MAX;
  if (mem < size)
    size = mem;
  if (getrlimit(RLIMIT_DATA, &rlimit) == 0 && rlimit.rlim_cur < size)
    size = rlimit.rlim_cur;
  size /= 2;
  return MAX(size, 8192);
}


x86 assembly (clang, -O0):
<default_sponge_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %eax,%eax
mov    %al,-0x29(%rbp)
call   1050 <physmem_available@plt>
mov    -0x29(%rbp),%al
movsd  %xmm0,-0x8(%rbp)
call   1040 <physmem_total@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xe8f(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rax
cvttsd2si %xmm0,%rdi
mov    %rdi,%rcx
sar    $0x3f,%rcx
and    %rcx,%rax
or     %rax,%rdi
movsd  -0x10(%rbp),%xmm0
movsd  0xe6c(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%esi
call   1030 <MAX@plt>
movq   %rax,%xmm0
movaps 0xe5b(%rip),%xmm3
punpckldq %xmm3,%xmm0
movapd 0xe5f(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm1
movq   -0x28(%rbp),%xmm0
punpckldq %xmm3,%xmm0
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm2
unpckhpd %xmm0,%xmm0
addsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    1239 <default_sponge_size+0xf9>
movsd  -0x18(%rbp),%xmm0
movsd  0xde9(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <getrlimit@plt>
cmp    $0x0,%rax
jne    126b <default_sponge_size+0x12b>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    126b <default_sponge_size+0x12b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2000,%esi
call   1030 <MAX@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<default_sponge_size>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   1050 <physmem_available@plt>
movapd %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1040 <physmem_total@plt>
movapd 0x10(%rsp),%xmm1
cvttsd2si %xmm1,%rax
subsd  0xe94(%rip),%xmm1
cvttsd2si %xmm1,%rcx
mov    %rax,%rdi
sar    $0x3f,%rdi
and    %rcx,%rdi
or     %rax,%rdi
mulsd  0xe82(%rip),%xmm0
cvttsd2si %xmm0,%esi
call   1030 <MAX@plt>
movq   %rax,%xmm0
movdqa 0xe74(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xe78(%rip),%xmm2
subpd  %xmm2,%xmm0
movapd %xmm0,%xmm3
unpckhpd %xmm0,%xmm3
addsd  %xmm0,%xmm3
mov    0x2e11(%rip),%rax
mov    (%rax),%rax
movq   %rax,%xmm0
punpckldq %xmm1,%xmm0
subpd  %xmm2,%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
cvttsd2si %xmm3,%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
movapd %xmm3,%xmm0
subsd  0xe0d(%rip),%xmm0
cvttsd2si %xmm0,%rbx
and    %rdx,%rbx
or     %rcx,%rbx
ucomisd %xmm3,%xmm1
cmovbe %rax,%rbx
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <getrlimit@plt>
mov    0x8(%rsp),%rdi
cmp    %rbx,%rdi
cmovae %rbx,%rdi
test   %rax,%rax
cmovne %rbx,%rdi
shr    %rdi
mov    $0x2000,%esi
call   1030 <MAX@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i64 }
@SIZE_MAX = global i64 0
@RLIMIT_DATA = global i32 0
define i64 @default_sponge_size() {
%1 = alloca double
%2 = alloca double
%3 = alloca double
%4 = alloca %struct.rlimit
%5 = alloca i64
%6 = call double (...) @physmem_available()
store double %6, double* %1
%7 = call double (...) @physmem_total()
store double %7, double* %2
%8 = load double, double* %1
%9 = fptoui double %8 to i64
%10 = load double, double* %2
%11 = fdiv double %10, 8.000000e+00
%12 = fptosi double %11 to i32
%13 = call i64 @MAX(i64 %9, i32 %12)
%14 = uitofp i64 %13 to double
store double %14, double* %3
%15 = load i64, i64* @SIZE_MAX
store i64 %15, i64* %5
%16 = load double, double* %3
%17 = load i64, i64* %5
%18 = uitofp i64 %17 to double
%19 = fcmp olt double %16, %18
br i1 %19, label %20, label %23
20: ; preds = %0
%21 = load double, double* %3
%22 = fptoui double %21 to i64
store i64 %22, i64* %5
br label %23
23: ; preds = %20, %0
%24 = load i32, i32* @RLIMIT_DATA
%25 = call i64 @getrlimit(i32 %24, %struct.rlimit* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %23
%28 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* %5
%31 = icmp ult i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%34 = load i64, i64* %33
store i64 %34, i64* %5
br label %35
35: ; preds = %32, %27, %23
%36 = load i64, i64* %5
%37 = udiv i64 %36, 2
store i64 %37, i64* %5
%38 = load i64, i64* %5
%39 = call i64 @MAX(i64 %38, i32 8192)
ret i64 %39
}

LLVM IR (-O2):
%struct.rlimit = type { i64 }
@SIZE_MAX = global i64 0
@RLIMIT_DATA = global i32 0
define i64 @default_sponge_size() {
%1 = alloca %struct.rlimit
%2 = tail call double (...) @physmem_available()
%3 = tail call double (...) @physmem_total()
%4 = fptoui double %2 to i64
%5 = fmul double %3, 1.250000e-01
%6 = fptosi double %5 to i32
%7 = tail call i64 @MAX(i64 %4, i32 %6)
%8 = uitofp i64 %7 to double
%9 = bitcast %struct.rlimit* %1 to i8*
%10 = load i64, i64* @SIZE_MAX
%11 = uitofp i64 %10 to double
%12 = fcmp olt double %8, %11
%13 = fptoui double %8 to i64
%14 = select i1 %12, i64 %13, i64 %10
%15 = load i32, i32* @RLIMIT_DATA
%16 = call i64 @getrlimit(i32 %15, %struct.rlimit* %1)
%17 = icmp eq i64 %16, 0
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp ult i64 %19, %14
%21 = select i1 %17, i1 %20, i1 false
%22 = select i1 %21, i64 %19, i64 %14
%23 = lshr i64 %22, 1
%24 = call i64 @MAX(i64 %23, i32 8192)
ret i64 %24
}