#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ krfree(void *);
void *krmalloc(int);
int /*<<< orphan*/ krmalloc_init(void *, int);
void *malloc(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  void *pool;
  void *ptr;
  pool = malloc(sizeof(char) * 1024);
  krmalloc_init(pool, sizeof(char) * 1024);
  ptr = krmalloc(sizeof(int) * 10);
  if (ptr == NULL) {
    fprintf(stderr, "can not allocate memory\n");
    return -1;
  }
  krfree(ptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x400,%esi
call   1030 <krmalloc_init@plt>
mov    $0x28,%edi
call   1060 <krmalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bc <main+0x6c>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <main+0x7c>
mov    -0x20(%rbp),%rdi
call   1070 <krfree@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
mov    $0x400,%esi
call   1030 <krmalloc_init@plt>
mov    $0x28,%edi
call   1060 <krmalloc@plt>
test   %rax,%rax
je     1183 <main+0x33>
mov    %rax,%rdi
call   1070 <krfree@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"can not allocate memory\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i8* @malloc(i32 1024)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = call i32 @krmalloc_init(i8* %9, i32 1024)
%11 = call i8* @krmalloc(i32 40)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %20
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = call i32 @krfree(i8* %18)
store i32 0, i32* %3
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"can not allocate memory\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = tail call i32 @krmalloc_init(i8* %3, i32 1024)
%5 = tail call i8* @krmalloc(i32 40)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %2
%11 = tail call i32 @krfree(i8* %5)
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ -1, %7 ], [ 0, %10 ]
ret i32 %13
}