#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__ memlimit_rlimit(size_t *);
int memtouse(size_t maxmem, double maxmemfrac, size_t *memlimit) {
  size_t sysctl_memlimit, sysinfo_memlimit, rlimit_memlimit;
  size_t sysconf_memlimit;
  size_t memlimit_min;
  size_t memavail;
  sysctl_memlimit = (size_t)(-1);
  sysinfo_memlimit = (size_t)(-1);
  if (memlimit_rlimit(&rlimit_memlimit))
    return (1);
  sysconf_memlimit = (size_t)(-1);
  memlimit_min = (size_t)(-1);
  if (memlimit_min > sysctl_memlimit)
    memlimit_min = sysctl_memlimit;
  if (memlimit_min > sysinfo_memlimit)
    memlimit_min = sysinfo_memlimit;
  if (memlimit_min > rlimit_memlimit)
    memlimit_min = rlimit_memlimit;
  if (memlimit_min > sysconf_memlimit)
    memlimit_min = sysconf_memlimit;
  if ((maxmemfrac > 0.5) || (maxmemfrac == 0.0))
    maxmemfrac = 0.5;
  memavail = maxmemfrac * 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
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
lea    -0x38(%rbp),%rdi
call   1030 <memlimit_rlimit@plt>
cmp    $0x0,%rax
je     1154 <memtouse+0x44>
movl   $0x1,-0x4(%rbp)
jmp    129b <memtouse+0x18b>
movq   $0xffffffffffffffff,-0x40(%rbp)
ff
movq   $0xffffffffffffffff,-0x48(%rbp)
ff
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    117a <memtouse+0x6a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1190 <memtouse+0x80>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    11a6 <memtouse+0x96>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
jbe    11bc <memtouse+0xac>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe37(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     11eb <memtouse+0xdb>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11f8 <memtouse+0xe8>
jp     11f8 <memtouse+0xe8>
movsd  0xe0d(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movaps 0xe0c(%rip),%xmm2
movq   -0x48(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xe0b(%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  0xdc9(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    1273 <memtouse+0x163>
mov    -0x50(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    1273 <memtouse+0x163>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x100000,-0x50(%rbp)
jae    1289 <memtouse+0x179>
movq   $0x100000,-0x50(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memtouse>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
movapd %xmm0,0x10(%rsp)
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1030 <memlimit_rlimit@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11e7 <memtouse+0xd7>
movsd  0x8(%rsp),%xmm0
unpcklps 0xeb5(%rip),%xmm0
subpd  0xebd(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm4
cmpeqsd %xmm4,%xmm0
movsd  0xeaa(%rip),%xmm2
movapd %xmm0,%xmm3
andpd  %xmm2,%xmm3
andnpd %xmm4,%xmm0
orpd   %xmm3,%xmm0
movapd %xmm2,%xmm3
cmpltsd %xmm4,%xmm3
movapd %xmm3,%xmm4
andnpd %xmm0,%xmm4
andpd  %xmm2,%xmm3
orpd   %xmm4,%xmm3
mulsd  %xmm1,%xmm3
cvttsd2si %xmm3,%rax
mov    %rax,%rcx
subsd  0xe75(%rip),%xmm3
cvttsd2si %xmm3,%rdx
sar    $0x3f,%rcx
and    %rcx,%rdx
or     %rax,%rdx
cmp    %rbx,%rdx
mov    %rdx,%rax
cmova  %rbx,%rax
test   %rbx,%rbx
cmove  %rdx,%rax
cmp    $0x100001,%rax
mov    $0x100000,%ecx
cmovae %rax,%rcx
mov    %rcx,(%r14)
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @memtouse(i64 %0, double %1, i64* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca double
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i64 %0, i64* %5
store double %1, double* %6
store i64* %2, i64** %7
store i64 -1, i64* %8
store i64 -1, i64* %9
%14 = call i64 @memlimit_rlimit(i64* %10)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 1, i32* %4
br label %69
17: ; preds = %3
store i64 -1, i64* %11
store i64 -1, i64* %12
%18 = load i64, i64* %12
%19 = load i64, i64* %8
%20 = icmp ugt i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i64, i64* %8
store i64 %22, i64* %12
br label %23
23: ; preds = %21, %17
%24 = load i64, i64* %12
%25 = load i64, i64* %9
%26 = icmp ugt i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i64, i64* %9
store i64 %28, i64* %12
br label %29
29: ; preds = %27, %23
%30 = load i64, i64* %12
%31 = load i64, i64* %10
%32 = icmp ugt i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i64, i64* %10
store i64 %34, i64* %12
br label %35
35: ; preds = %33, %29
%36 = load i64, i64* %12
%37 = load i64, i64* %11
%38 = icmp ugt i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i64, i64* %11
store i64 %40, i64* %12
br label %41
41: ; preds = %39, %35
%42 = load double, double* %6
%43 = fcmp ogt double %42, 5.000000e-01
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = load double, double* %6
%46 = fcmp oeq double %45, 0.000000e+00
br i1 %46, label %47, label %48
47: ; preds = %44, %41
store double 5.000000e-01, double* %6
br label %48
48: ; preds = %47, %44
%49 = load double, double* %6
%50 = load i64, i64* %12
%51 = uitofp i64 %50 to double
%52 = fmul double %49, %51
%53 = fptoui double %52 to i64
store i64 %53, i64* %13
%54 = load i64, i64* %5
%55 = icmp ugt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %48
%57 = load i64, i64* %13
%58 = load i64, i64* %5
%59 = icmp ugt i64 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = load i64, i64* %5
store i64 %61, i64* %13
br label %62
62: ; preds = %60, %56, %48
%63 = load i64, i64* %13
%64 = icmp ult i64 %63, 1048576
br i1 %64, label %65, label %66
65: ; preds = %62
store i64 1048576, i64* %13
br label %66
66: ; preds = %65, %62
%67 = load i64, i64* %13
%68 = load i64*, i64** %7
store i64 %67, i64* %68
store i32 0, i32* %4
br label %69
69: ; preds = %66, %16
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
define i32 @memtouse(i64 %0, double %1, i64* nocapture writeonly %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = call i64 @memlimit_rlimit(i64* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %3
%9 = load i64, i64* %4
%10 = uitofp i64 %9 to double
%11 = fcmp ogt double %1, 5.000000e-01
%12 = fcmp oeq double %1, 0.000000e+00
%13 = or i1 %11, %12
%14 = select i1 %13, double 5.000000e-01, double %1
%15 = fmul double %14, %10
%16 = fptoui double %15 to i64
%17 = icmp ne i64 %0, 0
%18 = icmp ugt i64 %16, %0
%19 = select i1 %17, i1 %18, i1 false
%20 = select i1 %19, i64 %0, i64 %16
%21 = icmp ugt i64 %20, 1048576
%22 = select i1 %21, i64 %20, i64 1048576
store i64 %22, i64* %2
br label %23
23: ; preds = %3, %8
%24 = phi i32 [ 0, %8 ], [ 1, %3 ]
ret i32 %24
}