#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*/ free(char *);
char *malloc(int);
int msize(char *);
int /*<<< orphan*/ printf(char *);
int test_malloc(void) {
  char *block, *block1;
  printf("\tmalloc");
  block = malloc(0);
  if (block == NULL) {
    printf(".");
  } else {
    free(block);
    printf(".");
  }
  block = malloc(127);
  if (block == NULL)
    return 1;
  else
    printf(".");
  block1 = malloc(127);
  if ((block1 == NULL) || (block1 == block))
    return 1;
  else
    printf(".");
  block[0] = 0;
  block[126] = 0;
  printf(".");
  if (msize(block) < 127)
    return 1;
  else
    printf(".");
  free(block);
  printf(".");
  free(block1);
  printf(".");
  free(malloc(127));
  printf(".");
  printf(" passed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117b <test_malloc+0x3b>
lea    0xe97(%rip),%rdi
call   1040 <printf@plt>
jmp    1190 <test_malloc+0x50>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x7f,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b5 <test_malloc+0x75>
movl   $0x1,-0x4(%rbp)
jmp    12a0 <test_malloc+0x160>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x7f,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e8 <test_malloc+0xa8>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11f4 <test_malloc+0xb4>
movl   $0x1,-0x4(%rbp)
jmp    12a0 <test_malloc+0x160>
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x7e(%rax)
lea    0xdf2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <msize@plt>
cmp    $0x7f,%eax
jge    1239 <test_malloc+0xf9>
movl   $0x1,-0x4(%rbp)
jmp    12a0 <test_malloc+0x160>
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xdb3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xd9e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x7f,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
lea    0xd80(%rip),%rdi
call   1040 <printf@plt>
lea    0xd76(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc>:
push   %rbp
push   %r14
push   %rbx
lea    0xe95(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1184 <test_malloc+0x24>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x2e,%edi
call   1040 <putchar@plt>
mov    $0x7f,%edi
call   1070 <malloc@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     1254 <test_malloc+0xf4>
mov    %rax,%rbx
mov    $0x2e,%edi
call   1040 <putchar@plt>
mov    $0x7f,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1254 <test_malloc+0xf4>
mov    %rax,%rbp
cmp    %rbx,%rax
je     1254 <test_malloc+0xf4>
mov    $0x2e,%edi
call   1040 <putchar@plt>
movb   $0x0,(%rbx)
movb   $0x0,0x7e(%rbx)
mov    $0x2e,%edi
call   1040 <putchar@plt>
mov    %rbx,%rdi
call   1080 <msize@plt>
cmp    $0x7f,%eax
jl     1254 <test_malloc+0xf4>
mov    $0x2e,%edi
call   1040 <putchar@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x2e,%edi
call   1040 <putchar@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0x2e,%edi
call   1040 <putchar@plt>
mov    $0x7f,%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x2e,%edi
call   1040 <putchar@plt>
lea    0xdbc(%rip),%rdi
call   1050 <puts@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"\09malloc\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [10 x i8] c" passed.\0A\00"
define i32 @test_malloc() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%5 = call i8* @malloc(i32 0)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %14
10: ; preds = %0
%11 = load i8*, i8** %2
%12 = call i32 @free(i8* %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %10, %8
%15 = call i8* @malloc(i32 127)
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 1, i32* %1
br label %55
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19
%22 = call i8* @malloc(i32 127)
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = icmp eq i8* %23, null
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = load i8*, i8** %2
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25, %21
store i32 1, i32* %1
br label %55
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30
%33 = load i8*, i8** %2
%34 = getelementptr inbounds i8, i8* %33, i64 0
store i8 0, i8* %34
%35 = load i8*, i8** %2
%36 = getelementptr inbounds i8, i8* %35, i64 126
store i8 0, i8* %36
%37 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = load i8*, i8** %2
%39 = call i32 @msize(i8* %38)
%40 = icmp slt i32 %39, 127
br i1 %40, label %41, label %42
41: ; preds = %32
store i32 1, i32* %1
br label %55
42: ; preds = %32
%43 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %42
%45 = load i8*, i8** %2
%46 = call i32 @free(i8* %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%48 = load i8*, i8** %3
%49 = call i32 @free(i8* %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%51 = call i8* @malloc(i32 127)
%52 = call i32 @free(i8* %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %55
55: ; preds = %44, %41, %29, %18
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [8 x i8] c"\09malloc\00"
@str = [9 x i8] c" passed.\00"
define i32 @test_malloc() {
%1 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%2 = tail call i8* @malloc(i32 0)
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @free(i8* %2)
br label %6
6: ; preds = %0, %4
%7 = tail call i32 @putchar(i32 46)
%8 = tail call dereferenceable_or_null(127) i8* @malloc(i32 127)
%9 = icmp eq i8* %8, null
br i1 %9, label %32, label %10
10: ; preds = %6
%11 = tail call i32 @putchar(i32 46)
%12 = tail call dereferenceable_or_null(127) i8* @malloc(i32 127)
%13 = icmp eq i8* %12, null
%14 = icmp eq i8* %12, %8
%15 = or i1 %13, %14
br i1 %15, label %32, label %16
16: ; preds = %10
%17 = tail call i32 @putchar(i32 46)
store i8 0, i8* %8
%18 = getelementptr inbounds i8, i8* %8, i64 126
store i8 0, i8* %18
%19 = tail call i32 @putchar(i32 46)
%20 = tail call i32 @msize(i8* %8)
%21 = icmp slt i32 %20, 127
br i1 %21, label %32, label %22
22: ; preds = %16
%23 = tail call i32 @putchar(i32 46)
%24 = tail call i32 @free(i8* %8)
%25 = tail call i32 @putchar(i32 46)
%26 = tail call i32 @free(i8* %12)
%27 = tail call i32 @putchar(i32 46)
%28 = tail call dereferenceable_or_null(127) i8* @malloc(i32 127)
%29 = tail call i32 @free(i8* %28)
%30 = tail call i32 @putchar(i32 46)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %16, %10, %6, %22
%33 = phi i32 [ 0, %22 ], [ 1, %6 ], [ 1, %10 ], [ 1, %16 ]
ret i32 %33
}