#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 keepcost;
};

/* Variables and functions */
int /*<<< orphan*/ M_TOP_PAD;
int /*<<< orphan*/ free(char *);
struct mallinfo mallinfo();
char *malloc(int);
int /*<<< orphan*/ malloc_stats();
int /*<<< orphan*/ malloc_trim(int /*<<< orphan*/);
int /*<<< orphan*/ mallopt(int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int thr;
  char *p1;
  struct mallinfo status;
  status = mallinfo();
  printf("\n top-most, releasable (via malloc_trim) space : %d\n",
         status.keepcost);
  thr = 5000;
  if (!mallopt(M_TOP_PAD, thr)) {
    printf("mallopt() failed\n");
  }
  p1 = malloc(100);
  status = mallinfo();
  printf("\n top-most, releasable (via malloc_trim) space : %d\n",
         status.keepcost);
  malloc_stats();
  printf("\nNow freeing 100k\n");
  malloc_trim(0);
  free(p1);
  malloc_stats();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1388,-0x14(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
call   1040 <mallopt@plt>
cmp    $0x0,%eax
jne    11d6 <main+0x66>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <malloc_stats@plt>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <malloc_trim@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1090 <malloc_stats@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <mallinfo@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    $0x1388,%esi
call   1050 <mallopt@plt>
test   %eax,%eax
jne    11bb <main+0x3b>
lea    0xe7f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <mallinfo@plt>
lea    0xe2a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10a0 <malloc_stats@plt>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <malloc_trim@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   10a0 <malloc_stats@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.mallinfo = type { i32 }
@.str = [53 x i8] c"\0A top-most, releasable (via malloc_trim) space : %d\0A\00"
@M_TOP_PAD = global i32 0
@.str.1 = [18 x i8] c"mallopt() failed\0A\00"
@.str.2 = [19 x i8] c"\0ANow freeing 100k\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.mallinfo
%9 = alloca %struct.mallinfo
%10 = alloca %struct.mallinfo
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @mallinfo()
%12 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %9, i32 0, i32 0
store i32 %11, i32* %12
%13 = bitcast %struct.mallinfo* %8 to i8*
%14 = bitcast %struct.mallinfo* %9 to i8*
%15 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %8, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 5000, i32* %6
%18 = load i32, i32* @M_TOP_PAD
%19 = load i32, i32* %6
%20 = call i32 @mallopt(i32 %18, i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %2
%25 = call i8* @malloc(i32 100)
store i8* %25, i8** %7
%26 = call i32 (...) @mallinfo()
%27 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %10, i32 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.mallinfo* %8 to i8*
%29 = bitcast %struct.mallinfo* %10 to i8*
%30 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @malloc_stats()
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @malloc_trim(i32 0)
%36 = load i8*, i8** %7
%37 = call i32 @free(i8* %36)
%38 = call i32 (...) @malloc_stats()
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [53 x i8] c"\0A top-most, releasable (via malloc_trim) space : %d\0A\00"
@M_TOP_PAD = global i32 0
@str = [17 x i8] c"mallopt() failed\00"
@str.3 = [18 x i8] c"\0ANow freeing 100k\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @mallinfo()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @M_TOP_PAD
%6 = tail call i32 @mallopt(i32 %5, i32 5000)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%12 = tail call i32 (...) @mallinfo()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @malloc_stats()
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.3, i64 0, i64 0))
%16 = tail call i32 @malloc_trim(i32 0)
%17 = tail call i32 @free(i8* %11)
%18 = tail call i32 (...) @malloc_stats()
ret i32 0
}