#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 /*<<< orphan*/ dumpmallinfo();
int /*<<< orphan*/ free(int /*<<< orphan*/);
int /*<<< orphan*/ malloc(int);
int /*<<< orphan*/ malloc_info(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ malloc_stats();
int malloc_trim(int);
int /*<<< orphan*/ mtrace();
int /*<<< orphan*/ muntrace();
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ setenv(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(void) {
  int i;
  setenv("MALLOC_TRACE", "/dev/stdout", 0);
  mtrace();
  for (i = 0; i < 1024; i++)
    free(malloc(1024 * (rand() % 1024)));
  malloc_stats();
  malloc_info(0, stdout);
  dumpmallinfo();
  printf("trim %d\n", malloc_trim(1024));
  muntrace();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
lea    0xe40(%rip),%rsi
xor    %edx,%edx
call   1050 <setenv@plt>
mov    $0x0,%al
call   10c0 <mtrace@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x400,-0x8(%rbp)
jge    121d <main+0x6d>
mov    $0x0,%al
call   10d0 <rand@plt>
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
shl    $0xa,%edi
call   1090 <malloc@plt>
mov    %eax,%edi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11e2 <main+0x32>
mov    $0x0,%al
call   10b0 <malloc_stats@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <malloc_info@plt>
mov    $0x0,%al
call   1070 <dumpmallinfo@plt>
mov    $0x400,%edi
call   10a0 <malloc_trim@plt>
mov    %eax,%esi
lea    0xdcb(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <muntrace@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe48(%rip),%rdi
lea    0xe4e(%rip),%rsi
xor    %edx,%edx
call   1050 <setenv@plt>
xor    %eax,%eax
call   10c0 <mtrace@plt>
mov    $0x400,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10d0 <rand@plt>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0x3ffc00,%ecx
sub    %ecx,%eax
shl    $0xa,%eax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %eax,%edi
call   1030 <free@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x30>
xor    %eax,%eax
call   10b0 <malloc_stats@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <malloc_info@plt>
xor    %eax,%eax
call   1070 <dumpmallinfo@plt>
mov    $0x400,%edi
call   10a0 <malloc_trim@plt>
lea    0xdda(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <muntrace@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"MALLOC_TRACE\00"
@.str.1 = [12 x i8] c"/dev/stdout\00"
@stdout = global i32 0
@.str.2 = [9 x i8] c"trim %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @setenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 0)
%4 = call i32 (...) @mtrace()
store i32 0, i32* %2
br label %5
5: ; preds = %14, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 1024
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = call i32 (...) @rand()
%10 = srem i32 %9, 1024
%11 = mul nsw i32 1024, %10
%12 = call i32 @malloc(i32 %11)
%13 = call i32 @free(i32 %12)
br label %14
14: ; preds = %8
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
store i32 %16, i32* %2
br label %5
17: ; preds = %5
%18 = call i32 (...) @malloc_stats()
%19 = load i32, i32* @stdout
%20 = call i32 @malloc_info(i32 0, i32 %19)
%21 = call i32 (...) @dumpmallinfo()
%22 = call i32 @malloc_trim(i32 1024)
%23 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = call i32 (...) @muntrace()
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"MALLOC_TRACE\00"
@.str.1 = [12 x i8] c"/dev/stdout\00"
@stdout = global i32 0
@.str.2 = [9 x i8] c"trim %d\0A\00"
define i32 @main() {
%1 = tail call i32 @setenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 0)
%2 = tail call i32 (...) @mtrace()
br label %3
3: ; preds = %0, %3
%4 = phi i32 [ 0, %0 ], [ %10, %3 ]
%5 = tail call i32 (...) @rand()
%6 = srem i32 %5, 1024
%7 = shl nsw i32 %6, 10
%8 = tail call i32 @malloc(i32 %7)
%9 = tail call i32 @free(i32 %8)
%10 = add nuw nsw i32 %4, 1
%11 = icmp eq i32 %10, 1024
br i1 %11, label %12, label %3
12: ; preds = %3
%13 = tail call i32 (...) @malloc_stats()
%14 = load i32, i32* @stdout
%15 = tail call i32 @malloc_info(i32 0, i32 %14)
%16 = tail call i32 (...) @dumpmallinfo()
%17 = tail call i32 @malloc_trim(i32 1024)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = tail call i32 (...) @muntrace()
ret i32 0
}