#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 */
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void *malloc_helper(int size) {
  void *ptr = NULL;
  if (size <= 0) {
    printf("[-] malloc size == 0\n");
    return ptr;
  }
  ptr = malloc(size);
  if (!ptr) {
    printf("[-] malloc error\n");
    return ptr;
  }
  printf("[+] malloc: %p\n", ptr);
  return ptr;
}


x86 assembly (clang, -O0):
<malloc_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1158 <malloc_helper+0x38>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <malloc_helper+0x84>
mov    -0xc(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118a <malloc_helper+0x6a>
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <malloc_helper+0x84>
mov    -0x18(%rbp),%rsi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_helper>:
push   %rbx
test   %edi,%edi
jle    1155 <malloc_helper+0x25>
call   1050 <malloc@plt>
test   %rax,%rax
je     115e <malloc_helper+0x2e>
mov    %rax,%rbx
lea    0xeb7(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    116c <malloc_helper+0x3c>
lea    0xec5(%rip),%rdi
jmp    1165 <malloc_helper+0x35>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"[-] malloc size == 0\0A\00"
@.str.1 = [18 x i8] c"[-] malloc error\0A\00"
@.str.2 = [16 x i8] c"[+] malloc: %p\0A\00"
define i8* @malloc_helper(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* null, i8** %4
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %4
store i8* %9, i8** %2
br label %22
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %4
store i8* %17, i8** %2
br label %22
18: ; preds = %10
%19 = load i8*, i8** %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %4
store i8* %21, i8** %2
br label %22
22: ; preds = %18, %15, %7
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@.str.2 = [16 x i8] c"[+] malloc: %p\0A\00"
@str = [17 x i8] c"[-] malloc error\00"
@str.3 = [21 x i8] c"[-] malloc size == 0\00"
define i8* @malloc_helper(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.3, i64 0, i64 0))
br label %12
5: ; preds = %1
%6 = tail call i8* @malloc(i32 %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %12
10: ; preds = %5
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %12
12: ; preds = %10, %8, %3
%13 = phi i8* [ null, %3 ], [ %6, %10 ], [ null, %8 ]
ret i8* %13
}