#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 memtouse(size_t maxmem, double maxmemfrac, size_t memlimit_min,
             size_t *memlimit) {
  size_t memavail;
  if ((maxmemfrac > 0.5) || (maxmemfrac == 0.0))
    maxmemfrac = 0.5;
  memavail = (size_t)(maxmemfrac * (double)memlimit_min);
  if ((maxmem > 0) && (memavail > maxmem))
    memavail = maxmem;
  if (memavail < 1048576)
    memavail = 1048576;
  *memlimit = memavail;
  return (0);
}


x86 assembly (clang, -O0):
<memtouse>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xede(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     1144 <memtouse+0x44>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1151 <memtouse+0x51>
jp     1151 <memtouse+0x51>
movsd  0xeb4(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movaps 0xeb3(%rip),%xmm2
movq   -0x18(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xeb2(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xe70(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
jbe    11cc <memtouse+0xcc>
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11cc <memtouse+0xcc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x100000,-0x28(%rbp)
jae    11e2 <memtouse+0xe2>
movq   $0x100000,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<memtouse>:
xorpd  %xmm1,%xmm1
cmpeqsd %xmm0,%xmm1
movsd  0xeef(%rip),%xmm2
movapd %xmm1,%xmm3
andpd  %xmm2,%xmm3
andnpd %xmm0,%xmm1
orpd   %xmm3,%xmm1
movapd %xmm2,%xmm3
cmpltsd %xmm0,%xmm3
movapd %xmm3,%xmm0
andnpd %xmm1,%xmm0
andpd  %xmm2,%xmm3
movq   %rsi,%xmm1
punpckldq 0xecd(%rip),%xmm1
orpd   %xmm0,%xmm3
subpd  0xed1(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  %xmm3,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xe99(%rip),%xmm0
cvttsd2si %xmm0,%rsi
sar    $0x3f,%rcx
and    %rcx,%rsi
or     %rax,%rsi
cmp    %rdi,%rsi
mov    %rsi,%rax
cmova  %rdi,%rax
test   %rdi,%rdi
cmove  %rsi,%rax
cmp    $0x100001,%rax
mov    $0x100000,%ecx
cmovae %rax,%rcx
mov    %rcx,(%rdx)
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @memtouse(i64 %0, double %1, i64 %2, i64* %3) {
%5 = alloca i64
%6 = alloca double
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i64
store i64 %0, i64* %5
store double %1, double* %6
store i64 %2, i64* %7
store i64* %3, i64** %8
%10 = load double, double* %6
%11 = fcmp ogt double %10, 5.000000e-01
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = load double, double* %6
%14 = fcmp oeq double %13, 0.000000e+00
br i1 %14, label %15, label %16
15: ; preds = %12, %4
store double 5.000000e-01, double* %6
br label %16
16: ; preds = %15, %12
%17 = load double, double* %6
%18 = load i64, i64* %7
%19 = uitofp i64 %18 to double
%20 = fmul double %17, %19
%21 = fptoui double %20 to i64
store i64 %21, i64* %9
%22 = load i64, i64* %5
%23 = icmp ugt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %16
%25 = load i64, i64* %9
%26 = load i64, i64* %5
%27 = icmp ugt i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i64, i64* %5
store i64 %29, i64* %9
br label %30
30: ; preds = %28, %24, %16
%31 = load i64, i64* %9
%32 = icmp ult i64 %31, 1048576
br i1 %32, label %33, label %34
33: ; preds = %30
store i64 1048576, i64* %9
br label %34
34: ; preds = %33, %30
%35 = load i64, i64* %9
%36 = load i64*, i64** %8
store i64 %35, i64* %36
ret i32 0
}

LLVM IR (-O2):
define i32 @memtouse(i64 %0, double %1, i64 %2, i64* nocapture writeonly %3) {
%5 = fcmp ogt double %1, 5.000000e-01
%6 = fcmp oeq double %1, 0.000000e+00
%7 = or i1 %5, %6
%8 = select i1 %7, double 5.000000e-01, double %1
%9 = uitofp i64 %2 to double
%10 = fmul double %8, %9
%11 = fptoui double %10 to i64
%12 = icmp ne i64 %0, 0
%13 = icmp ugt i64 %11, %0
%14 = select i1 %12, i1 %13, i1 false
%15 = select i1 %14, i64 %0, i64 %11
%16 = icmp ugt i64 %15, 1048576
%17 = select i1 %16, i64 %15, i64 1048576
store i64 %17, i64* %3
ret i32 0
}