#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 rusage {
  long ru_maxrss;
};

/* Variables and functions */
int /*<<< orphan*/ RUSAGE_SELF;
scalar_t__ getrusage(int /*<<< orphan*/, struct rusage *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, long);
char *mem_use(char *buf, size_t size) {
  struct rusage usage;
  *buf = '\0';
  if (0 == getrusage(RUSAGE_SELF, &usage)) {
    long mem = usage.ru_maxrss;
    if (1024 * 1024 * 10 < mem) {
      snprintf(buf, size, "%ldGB", (mem + (1024 * 1024) / 2) / (1024 * 1024));
    } else if (1024 * 1024 < mem) {
      snprintf(buf, size, "%0.1fGB",
               ((double)mem + (1024.0 * 1024.0) / 20.0) / (1024.0 * 1024.0));
    } else if (1024 * 10 < mem) {
      snprintf(buf, size, "%ldMB", (mem + 512) / 1024);
    } else if (1024 < mem) {
      snprintf(buf, size, "%0.1fMB", ((double)mem + 51.2) / 1024.0);
    } else {
      snprintf(buf, size, "%ldKB", mem);
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<mem_use>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2ea2(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <getrusage@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    129f <mem_use+0x17f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0xa00000,%eax
cmp    -0x20(%rbp),%rax
jge    119e <mem_use+0x7e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    $0x80000,%rax
mov    $0x100000,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
lea    0xe8c(%rip),%rdx
call   1030 <snprintf@plt>
jmp    129a <mem_use+0x17a>
mov    $0x100000,%eax
cmp    -0x20(%rbp),%rax
jge    11e9 <mem_use+0xc9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
cvtsi2sdq -0x20(%rbp),%xmm0
movsd  0xe55(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe41(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
lea    0xe47(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1295 <mem_use+0x175>
mov    $0x2800,%eax
cmp    -0x20(%rbp),%rax
jge    1228 <mem_use+0x108>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    $0x200,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
lea    0xe10(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1290 <mem_use+0x170>
mov    $0x400,%eax
cmp    -0x20(%rbp),%rax
jge    1273 <mem_use+0x153>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
cvtsi2sdq -0x20(%rbp),%xmm0
movsd  0xdbb(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xda7(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
lea    0xdcb(%rip),%rdx
call   1030 <snprintf@plt>
jmp    128b <mem_use+0x16b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0xdb6(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1290 <mem_use+0x170>
jmp    1295 <mem_use+0x175>
jmp    129a <mem_use+0x17a>
jmp    129f <mem_use+0x17f>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_use>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,(%rdi)
mov    0x2eac(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1040 <getrusage@plt>
test   %rax,%rax
jne    11ef <mem_use+0xcf>
mov    (%rsp),%rcx
cmp    $0xa00001,%rcx
jl     1168 <mem_use+0x48>
add    $0x80000,%rcx
shr    $0x14,%rcx
lea    0xeba(%rip),%rdx
jmp    11e4 <mem_use+0xc4>
cmp    $0x100001,%rcx
jl     1194 <mem_use+0x74>
cvtsi2sd %rcx,%xmm0
addsd  0xe92(%rip),%xmm0
mulsd  0xe92(%rip),%xmm0
cvttsd2si %xmm0,%rcx
lea    0xe94(%rip),%rdx
jmp    11e4 <mem_use+0xc4>
cmp    $0x2801,%rcx
jl     11b1 <mem_use+0x91>
add    $0x200,%rcx
shr    $0xa,%rcx
lea    0xe7f(%rip),%rdx
jmp    11e4 <mem_use+0xc4>
cmp    $0x401,%rcx
jl     11dd <mem_use+0xbd>
cvtsi2sd %rcx,%xmm0
addsd  0xe39(%rip),%xmm0
mulsd  0xe39(%rip),%xmm0
cvttsd2si %xmm0,%rcx
lea    0xe59(%rip),%rdx
jmp    11e4 <mem_use+0xc4>
lea    0xe58(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rusage = type { i64 }
@RUSAGE_SELF = global i32 0
@.str = [6 x i8] c"%ldGB\00"
@.str.1 = [8 x i8] c"%0.1fGB\00"
@.str.2 = [6 x i8] c"%ldMB\00"
@.str.3 = [8 x i8] c"%0.1fMB\00"
@.str.4 = [6 x i8] c"%ldKB\00"
define i8* @mem_use(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.rusage
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
store i8 0, i8* %7
%8 = load i32, i32* @RUSAGE_SELF
%9 = call i64 @getrusage(i32 %8, %struct.rusage* %5)
%10 = icmp eq i64 0, %9
br i1 %10, label %11, label %66
11: ; preds = %2
%12 = getelementptr inbounds %struct.rusage, %struct.rusage* %5, i32 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp slt i64 10485760, %14
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = load i64, i64* %4
%19 = load i64, i64* %6
%20 = add nsw i64 %19, 524288
%21 = sdiv i64 %20, 1048576
%22 = call i32 @snprintf(i8* %17, i64 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %21)
br label %65
23: ; preds = %11
%24 = load i64, i64* %6
%25 = icmp slt i64 1048576, %24
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = load i8*, i8** %3
%28 = load i64, i64* %4
%29 = load i64, i64* %6
%30 = sitofp i64 %29 to double
%31 = fadd double %30, 5.242880e+04
%32 = fdiv double %31, 0x4130000000000000
%33 = fptosi double %32 to i64
%34 = call i32 @snprintf(i8* %27, i64 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %33)
br label %64
35: ; preds = %23
%36 = load i64, i64* %6
%37 = icmp slt i64 10240, %36
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i8*, i8** %3
%40 = load i64, i64* %4
%41 = load i64, i64* %6
%42 = add nsw i64 %41, 512
%43 = sdiv i64 %42, 1024
%44 = call i32 @snprintf(i8* %39, i64 %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %43)
br label %63
45: ; preds = %35
%46 = load i64, i64* %6
%47 = icmp slt i64 1024, %46
br i1 %47, label %48, label %57
48: ; preds = %45
%49 = load i8*, i8** %3
%50 = load i64, i64* %4
%51 = load i64, i64* %6
%52 = sitofp i64 %51 to double
%53 = fadd double %52, 5.120000e+01
%54 = fdiv double %53, 1.024000e+03
%55 = fptosi double %54 to i64
%56 = call i32 @snprintf(i8* %49, i64 %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %55)
br label %62
57: ; preds = %45
%58 = load i8*, i8** %3
%59 = load i64, i64* %4
%60 = load i64, i64* %6
%61 = call i32 @snprintf(i8* %58, i64 %59, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %60)
br label %62
62: ; preds = %57, %48
br label %63
63: ; preds = %62, %38
br label %64
64: ; preds = %63, %26
br label %65
65: ; preds = %64, %16
br label %66
66: ; preds = %65, %2
%67 = load i8*, i8** %3
ret i8* %67
}

LLVM IR (-O2):
%struct.rusage = type { i64 }
@RUSAGE_SELF = global i32 0
@.str = [6 x i8] c"%ldGB\00"
@.str.1 = [8 x i8] c"%0.1fGB\00"
@.str.2 = [6 x i8] c"%ldMB\00"
@.str.3 = [8 x i8] c"%0.1fMB\00"
@.str.4 = [6 x i8] c"%ldKB\00"
define i8* @mem_use(i8* returned %0, i64 %1) {
%3 = alloca %struct.rusage
%4 = bitcast %struct.rusage* %3 to i8*
store i8 0, i8* %0
%5 = load i32, i32* @RUSAGE_SELF
%6 = call i64 @getrusage(i32 %5, %struct.rusage* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %40
8: ; preds = %2
%9 = getelementptr inbounds %struct.rusage, %struct.rusage* %3, i64 0, i32 0
%10 = load i64, i64* %9
%11 = icmp sgt i64 %10, 10485760
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = add nuw nsw i64 %10, 524288
%14 = lshr i64 %13, 20
%15 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %14)
br label %40
16: ; preds = %8
%17 = icmp sgt i64 %10, 1048576
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = sitofp i64 %10 to double
%20 = fadd double %19, 5.242880e+04
%21 = fmul double %20, 0x3EB0000000000000
%22 = fptosi double %21 to i64
%23 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %22)
br label %40
24: ; preds = %16
%25 = icmp sgt i64 %10, 10240
br i1 %25, label %26, label %30
26: ; preds = %24
%27 = add nuw nsw i64 %10, 512
%28 = lshr i64 %27, 10
%29 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %28)
br label %40
30: ; preds = %24
%31 = icmp sgt i64 %10, 1024
br i1 %31, label %32, label %38
32: ; preds = %30
%33 = sitofp i64 %10 to double
%34 = fadd double %33, 5.120000e+01
%35 = fmul double %34, 0x3F50000000000000
%36 = fptosi double %35 to i64
%37 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %36)
br label %40
38: ; preds = %30
%39 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %10)
br label %40
40: ; preds = %12, %26, %38, %32, %18, %2
ret i8* %0
}