#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 mallinfo {
  int arena;
  int keepcost;
  int fordblks;
  int uordblks;
  int usmblks;
  int hblkhd;
};

/* Variables and functions */
int /*<<< orphan*/ M_MMAP_THRESHOLD;
int /*<<< orphan*/ M_TOP_PAD;
int /*<<< orphan*/ M_TRIM_THRESHOLD;
int /*<<< orphan*/ free(char *);
struct mallinfo mallinfo();
char *malloc(int);
int malloc_trim(int /*<<< orphan*/);
int /*<<< orphan*/ mallopt(int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int main(void) {
  int ret;
  struct mallinfo m;
  static char *p[50000];
  int i;
  mallopt(M_TRIM_THRESHOLD, 1000000);
  mallopt(M_TOP_PAD, 1);
  mallopt(M_MMAP_THRESHOLD, 10000000);
  for (i = 0; i < 50000; i++)
    p[i] = malloc(256);
  m = mallinfo();
  printf("malloc:  arena %d  keepcost %d  fordblks %d  uord=%d,usm=%d  hblkhd "
         "%d\n",
         m.arena, m.keepcost, m.fordblks, m.uordblks, m.usmblks, m.hblkhd);
  for (i = 50000 - 1; i >= 0; i--)
    free(p[i]);
  m = mallinfo();
  printf("malloc:  arena %d  keepcost %d  fordblks %d  uord=%d,usm=%d  hblkhd "
         "%d\n",
         m.arena, m.keepcost, m.fordblks, m.uordblks, m.usmblks, m.hblkhd);
  ret = malloc_trim(0);
  printf("ret %d\n", ret);
  m = mallinfo();
  printf("keepcost %d\n", m.keepcost);
  ret = malloc_trim(0);
  printf("ret %d\n", ret);
  m = mallinfo();
  printf("keepcost %d\n", m.keepcost);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    $0xf4240,%esi
call   1040 <mallopt@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
call   1040 <mallopt@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    $0x989680,%esi
call   1040 <mallopt@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xc350,-0x24(%rbp)
jge    11e9 <main+0x89>
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
movslq -0x24(%rbp),%rcx
lea    0x2e89(%rip),%rax
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b2 <main+0x52>
lea    -0x40(%rbp),%rdi
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x10(%rbp),%r9d
mov    -0xc(%rbp),%eax
lea    0xdd9(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xc34f,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1264 <main+0x104>
movslq -0x24(%rbp),%rcx
lea    0x2e13(%rip),%rax
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1238 <main+0xd8>
lea    -0x58(%rbp),%rdi
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x10(%rbp),%r9d
mov    -0xc(%rbp),%eax
lea    0xd5e(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <malloc_trim@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    -0x70(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <malloc_trim@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x88(%rbp),%rdi
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    -0x88(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    $0xf4240,%esi
call   1040 <mallopt@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
call   1040 <mallopt@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    $0x989680,%esi
call   1040 <mallopt@plt>
xor    %ebx,%ebx
lea    0x2eb7(%rip),%r14
nopl   0x0(%rax)
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,(%rbx,%r14,1)
add    $0x8,%rbx
cmp    $0x61a80,%rbx
jne    11b0 <main+0x50>
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1060 <mallinfo@plt>
mov    0x10(%rsp),%esi
mov    0x14(%rsp),%edx
mov    0x18(%rsp),%ecx
mov    0x1c(%rsp),%r8d
mov    0x20(%rsp),%r9d
mov    0x24(%rsp),%eax
mov    %eax,(%rsp)
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xc34f,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%rbx
jb     1210 <main+0xb0>
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1060 <mallinfo@plt>
mov    0x10(%rsp),%esi
mov    0x14(%rsp),%edx
mov    0x18(%rsp),%ecx
mov    0x1c(%rsp),%r8d
mov    0x20(%rsp),%r9d
mov    0x24(%rsp),%eax
mov    %eax,(%rsp)
lea    0xdb1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <malloc_trim@plt>
lea    0xde4(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1060 <mallinfo@plt>
mov    0x14(%rsp),%esi
lea    0xdc9(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <malloc_trim@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1060 <mallinfo@plt>
mov    0x14(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.mallinfo = type { i32, i32, i32, i32, i32, i32 }
@main.p = internal global [50000 x i8*] zeroinitializer
@M_TRIM_THRESHOLD = global i32 0
@M_TOP_PAD = global i32 0
@M_MMAP_THRESHOLD = global i32 0
@.str = [72 x i8] c"malloc: arena %d keepcost %d fordblks %d uord=%d,usm=%d hblkhd %d\0A\00"
@.str.1 = [8 x i8] c"ret %d\0A\00"
@.str.2 = [13 x i8] c"keepcost %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.mallinfo
%4 = alloca i32
%5 = alloca %struct.mallinfo
%6 = alloca %struct.mallinfo
%7 = alloca %struct.mallinfo
%8 = alloca %struct.mallinfo
store i32 0, i32* %1
%9 = load i32, i32* @M_TRIM_THRESHOLD
%10 = call i32 @mallopt(i32 %9, i32 1000000)
%11 = load i32, i32* @M_TOP_PAD
%12 = call i32 @mallopt(i32 %11, i32 1)
%13 = load i32, i32* @M_MMAP_THRESHOLD
%14 = call i32 @mallopt(i32 %13, i32 10000000)
store i32 0, i32* %4
br label %15
15: ; preds = %23, %0
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 50000
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = call i8* @malloc(i32 256)
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* @main.p, i64 0, i64 %21
store i8* %19, i8** %22
br label %23
23: ; preds = %18
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %15
26: ; preds = %15
%27 = bitcast %struct.mallinfo* %3 to i8*
%28 = bitcast %struct.mallinfo* %5 to i8*
%29 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 2
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 3
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 4
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 5
%40 = load i32, i32* %39
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %32, i32 %34, i32 %36, i32 %38, i32 %40)
store i32 49999, i32* %4
br label %42
42: ; preds = %51, %26
%43 = load i32, i32* %4
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* @main.p, i64 0, i64 %47
%49 = load i8*, i8** %48
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %45
%52 = load i32, i32* %4
%53 = add nsw i32 %52, -1
store i32 %53, i32* %4
br label %42
54: ; preds = %42
%55 = bitcast %struct.mallinfo* %3 to i8*
%56 = bitcast %struct.mallinfo* %6 to i8*
%57 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 0
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 1
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 2
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 3
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 4
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 5
%68 = load i32, i32* %67
%69 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %58, i32 %60, i32 %62, i32 %64, i32 %66, i32 %68)
%70 = call i32 @malloc_trim(i32 0)
store i32 %70, i32* %2
%71 = load i32, i32* %2
%72 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %71)
%73 = bitcast %struct.mallinfo* %3 to i8*
%74 = bitcast %struct.mallinfo* %7 to i8*
%75 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 1
%76 = load i32, i32* %75
%77 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %76)
%78 = call i32 @malloc_trim(i32 0)
store i32 %78, i32* %2
%79 = load i32, i32* %2
%80 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %79)
%81 = bitcast %struct.mallinfo* %3 to i8*
%82 = bitcast %struct.mallinfo* %8 to i8*
%83 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i32 0, i32 1
%84 = load i32, i32* %83
%85 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %84)
ret i32 0
}

LLVM IR (-O2):
%struct.mallinfo = type { i32, i32, i32, i32, i32, i32 }
@main.p = internal unnamed_addr global [50000 x i8*] zeroinitializer
@M_TRIM_THRESHOLD = global i32 0
@M_TOP_PAD = global i32 0
@M_MMAP_THRESHOLD = global i32 0
@.str = [72 x i8] c"malloc: arena %d keepcost %d fordblks %d uord=%d,usm=%d hblkhd %d\0A\00"
@.str.1 = [8 x i8] c"ret %d\0A\00"
@.str.2 = [13 x i8] c"keepcost %d\0A\00"
define i32 @main() {
%1 = alloca %struct.mallinfo
%2 = alloca %struct.mallinfo
%3 = alloca %struct.mallinfo
%4 = alloca %struct.mallinfo
%5 = load i32, i32* @M_TRIM_THRESHOLD
%6 = tail call i32 @mallopt(i32 %5, i32 1000000)
%7 = load i32, i32* @M_TOP_PAD
%8 = tail call i32 @mallopt(i32 %7, i32 1)
%9 = load i32, i32* @M_MMAP_THRESHOLD
%10 = tail call i32 @mallopt(i32 %9, i32 10000000)
br label %11
11: ; preds = %0, %11
%12 = phi i64 [ 0, %0 ], [ %15, %11 ]
%13 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%14 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* @main.p, i64 0, i64 %12
store i8* %13, i8** %14
%15 = add nuw nsw i64 %12, 1
%16 = icmp eq i64 %15, 50000
br i1 %16, label %17, label %11
17: ; preds = %11
%18 = bitcast %struct.mallinfo* %1 to i8*
%19 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %1, i64 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %1, i64 0, i32 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %1, i64 0, i32 2
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %1, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %1, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %1, i64 0, i32 5
%30 = load i32, i32* %29
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %22, i32 %24, i32 %26, i32 %28, i32 %30)
br label %32
32: ; preds = %17, %32
%33 = phi i64 [ 49999, %17 ], [ %37, %32 ]
%34 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* @main.p, i64 0, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @free(i8* %35)
%37 = add nsw i64 %33, -1
%38 = icmp eq i64 %33, 0
br i1 %38, label %39, label %32
39: ; preds = %32
%40 = bitcast %struct.mallinfo* %2 to i8*
%41 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i64 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i64 0, i32 2
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i64 0, i32 3
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i64 0, i32 4
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i64 0, i32 5
%52 = load i32, i32* %51
%53 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %42, i32 %44, i32 %46, i32 %48, i32 %50, i32 %52)
%54 = call i32 @malloc_trim(i32 0)
%55 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = bitcast %struct.mallinfo* %3 to i8*
%57 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %3, i64 0, i32 1
%58 = load i32, i32* %57
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = call i32 @malloc_trim(i32 0)
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %60)
%62 = bitcast %struct.mallinfo* %4 to i8*
%63 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %4, i64 0, i32 1
%64 = load i32, i32* %63
%65 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %64)
ret i32 0
}