#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 */
typedef char FILE;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fclose(char *);
char *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ loga();
int /*<<< orphan*/ logn();
int /*<<< orphan*/ logp(char *, char *, char *, char *);
scalar_t__ malloc(int);
scalar_t__ memalign(int, int);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ realloc(char *, int);
char *strlen(char *);
void test_malloc_c() {
  logn();
  loga();
  char *p = (char *)malloc(10);
  memset(p, '0', 5);
  logp("malloc: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  p = (char *)realloc(p, 20);
  memset(p, '1', 15);
  logp("realloc: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  free(p);
  logp("free: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  p = (char *)calloc(30, 1);
  memset(p, '2', 25);
  logp("calloc: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  free(p);
  logp("free: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  p = (char *)memalign(8, 40);
  memset(p, '3', 35);
  logp("memalign: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  free(p);
  logp("free: addr=%p size=%lu str=%s\n", p, strlen(p), p);
  FILE *f = fopen("testfile", "w");
  if (NULL != f) {
    logp("fopen: addr=%p file=%s mode=%s\n", f, "testfile", "w");
    fclose(f);
    logp("fclose: addr=%p file=%s mode=%s\n", f, "testfile", "w");
  }
}


x86 assembly (clang, -O0):
<test_malloc_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    $0x0,%al
call   1050 <logn@plt>
mov    $0x0,%al
call   10c0 <loga@plt>
mov    $0xa,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x30,%esi
mov    $0x5,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xde6(%rip),%rdi
call   1040 <logp@plt>
mov    -0x8(%rbp),%rdi
mov    $0x14,%esi
call   10d0 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x31,%esi
mov    $0xf,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xdba(%rip),%rdi
call   1040 <logp@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xdab(%rip),%rdi
call   1040 <logp@plt>
mov    $0x1e,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x32,%esi
mov    $0x19,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xd7c(%rip),%rdi
call   1040 <logp@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xd2c(%rip),%rdi
call   1040 <logp@plt>
mov    $0x8,%edi
mov    $0x28,%esi
call   10a0 <memalign@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x33,%esi
mov    $0x23,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xd1e(%rip),%rdi
call   1040 <logp@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xcad(%rip),%rdi
call   1040 <logp@plt>
lea    0xd04(%rip),%rdi
lea    0xd06(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1403 <test_malloc_c+0x243>
mov    -0x10(%rbp),%rsi
lea    0xce8(%rip),%rdi
lea    0xcd6(%rip),%rdx
lea    0xcd8(%rip),%rcx
call   1040 <logp@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rsi
lea    0xce1(%rip),%rdi
lea    0xcaf(%rip),%rdx
lea    0xcb1(%rip),%rcx
call   1040 <logp@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc_c>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1050 <logn@plt>
xor    %eax,%eax
call   10c0 <loga@plt>
mov    $0xa,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x30,%esi
mov    $0x5,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0xdff(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
call   10d0 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x31,%esi
mov    $0xf,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0xde1(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0xdde(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
mov    $0x1e,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x32,%esi
mov    $0x19,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0xdb9(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
mov    $0x8,%edi
mov    $0x28,%esi
call   10a0 <memalign@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x33,%esi
mov    $0x23,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0xd78(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <logp@plt>
lea    0xd65(%rip),%rdi
lea    0xd67(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     139a <test_malloc_c+0x1da>
mov    %rax,%rbx
lea    0xd55(%rip),%rdi
lea    0xd43(%rip),%r14
lea    0xd45(%rip),%r15
mov    %rax,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1040 <logp@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd4a(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <logp@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [33 x i8] c"malloc: addr=%p size=%lu str=%s\0A\00"
@.str.1 = [34 x i8] c"realloc: addr=%p size=%lu str=%s\0A\00"
@.str.2 = [31 x i8] c"free: addr=%p size=%lu str=%s\0A\00"
@.str.3 = [33 x i8] c"calloc: addr=%p size=%lu str=%s\0A\00"
@.str.4 = [35 x i8] c"memalign: addr=%p size=%lu str=%s\0A\00"
@.str.5 = [9 x i8] c"testfile\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [32 x i8] c"fopen: addr=%p file=%s mode=%s\0A\00"
@.str.8 = [33 x i8] c"fclose: addr=%p file=%s mode=%s\0A\00"
define void @test_malloc_c() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i32 (...) @logn()
%4 = call i32 (...) @loga()
%5 = call i64 @malloc(i32 10)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %1
%7 = load i8*, i8** %1
%8 = call i32 @memset(i8* %7, i8 signext 48, i32 5)
%9 = load i8*, i8** %1
%10 = load i8*, i8** %1
%11 = call i8* @strlen(i8* %10)
%12 = load i8*, i8** %1
%13 = call i32 @logp(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %11, i8* %12)
%14 = load i8*, i8** %1
%15 = call i64 @realloc(i8* %14, i32 20)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %1
%17 = load i8*, i8** %1
%18 = call i32 @memset(i8* %17, i8 signext 49, i32 15)
%19 = load i8*, i8** %1
%20 = load i8*, i8** %1
%21 = call i8* @strlen(i8* %20)
%22 = load i8*, i8** %1
%23 = call i32 @logp(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %21, i8* %22)
%24 = load i8*, i8** %1
%25 = call i32 @free(i8* %24)
%26 = load i8*, i8** %1
%27 = load i8*, i8** %1
%28 = call i8* @strlen(i8* %27)
%29 = load i8*, i8** %1
%30 = call i32 @logp(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %28, i8* %29)
%31 = call i64 @calloc(i32 30, i32 1)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %1
%33 = load i8*, i8** %1
%34 = call i32 @memset(i8* %33, i8 signext 50, i32 25)
%35 = load i8*, i8** %1
%36 = load i8*, i8** %1
%37 = call i8* @strlen(i8* %36)
%38 = load i8*, i8** %1
%39 = call i32 @logp(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %37, i8* %38)
%40 = load i8*, i8** %1
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %1
%43 = load i8*, i8** %1
%44 = call i8* @strlen(i8* %43)
%45 = load i8*, i8** %1
%46 = call i32 @logp(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %44, i8* %45)
%47 = call i64 @memalign(i32 8, i32 40)
%48 = inttoptr i64 %47 to i8*
store i8* %48, i8** %1
%49 = load i8*, i8** %1
%50 = call i32 @memset(i8* %49, i8 signext 51, i32 35)
%51 = load i8*, i8** %1
%52 = load i8*, i8** %1
%53 = call i8* @strlen(i8* %52)
%54 = load i8*, i8** %1
%55 = call i32 @logp(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %51, i8* %53, i8* %54)
%56 = load i8*, i8** %1
%57 = call i32 @free(i8* %56)
%58 = load i8*, i8** %1
%59 = load i8*, i8** %1
%60 = call i8* @strlen(i8* %59)
%61 = load i8*, i8** %1
%62 = call i32 @logp(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %58, i8* %60, i8* %61)
%63 = call i8* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i8* %63, i8** %2
%64 = load i8*, i8** %2
%65 = icmp ne i8* null, %64
br i1 %65, label %66, label %73
66: ; preds = %0
%67 = load i8*, i8** %2
%68 = call i32 @logp(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %67, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%69 = load i8*, i8** %2
%70 = call i32 @fclose(i8* %69)
%71 = load i8*, i8** %2
%72 = call i32 @logp(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %71, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %73
73: ; preds = %66, %0
ret void
}

LLVM IR (-O2):
@.str = [33 x i8] c"malloc: addr=%p size=%lu str=%s\0A\00"
@.str.1 = [34 x i8] c"realloc: addr=%p size=%lu str=%s\0A\00"
@.str.2 = [31 x i8] c"free: addr=%p size=%lu str=%s\0A\00"
@.str.3 = [33 x i8] c"calloc: addr=%p size=%lu str=%s\0A\00"
@.str.4 = [35 x i8] c"memalign: addr=%p size=%lu str=%s\0A\00"
@.str.5 = [9 x i8] c"testfile\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [32 x i8] c"fopen: addr=%p file=%s mode=%s\0A\00"
@.str.8 = [33 x i8] c"fclose: addr=%p file=%s mode=%s\0A\00"
define void @test_malloc_c() {
%1 = tail call i32 (...) @logn()
%2 = tail call i32 (...) @loga()
%3 = tail call i64 @malloc(i32 10)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @memset(i8* %4, i8 signext 48, i32 5)
%6 = tail call i8* @strlen(i8* %4)
%7 = tail call i32 @logp(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %6, i8* %4)
%8 = tail call i64 @realloc(i8* %4, i32 20)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @memset(i8* %9, i8 signext 49, i32 15)
%11 = tail call i8* @strlen(i8* %9)
%12 = tail call i32 @logp(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %11, i8* %9)
%13 = tail call i32 @free(i8* %9)
%14 = tail call i8* @strlen(i8* %9)
%15 = tail call i32 @logp(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %9, i8* %14, i8* %9)
%16 = tail call i64 @calloc(i32 30, i32 1)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @memset(i8* %17, i8 signext 50, i32 25)
%19 = tail call i8* @strlen(i8* %17)
%20 = tail call i32 @logp(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %17, i8* %19, i8* %17)
%21 = tail call i32 @free(i8* %17)
%22 = tail call i8* @strlen(i8* %17)
%23 = tail call i32 @logp(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %22, i8* %17)
%24 = tail call i64 @memalign(i32 8, i32 40)
%25 = inttoptr i64 %24 to i8*
%26 = tail call i32 @memset(i8* %25, i8 signext 51, i32 35)
%27 = tail call i8* @strlen(i8* %25)
%28 = tail call i32 @logp(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %25, i8* %27, i8* %25)
%29 = tail call i32 @free(i8* %25)
%30 = tail call i8* @strlen(i8* %25)
%31 = tail call i32 @logp(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %30, i8* %25)
%32 = tail call i8* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %38, label %34
34: ; preds = %0
%35 = tail call i32 @logp(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%36 = tail call i32 @fclose(i8* %32)
%37 = tail call i32 @logp(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %34, %0
ret void
}