#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 *my_malloc(size_t size) {
  void *temp = malloc(size);
  if (!temp) {
    printf("Out of memory!\n");
    temp = NULL;
  } else if (!size) {
    printf("Size error!");
    temp = malloc(1);
  }
  return temp;
}


x86 assembly (clang, -O0):
<my_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115f <my_malloc+0x3f>
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x10(%rbp)
jmp    1189 <my_malloc+0x69>
cmpq   $0x0,-0x8(%rbp)
jne    1184 <my_malloc+0x64>
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
jmp    1189 <my_malloc+0x69>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_malloc>:
push   %rbx
mov    %rdi,%rbx
call   1050 <malloc@plt>
test   %rax,%rax
je     1145 <my_malloc+0x15>
test   %rbx,%rbx
je     1155 <my_malloc+0x25>
pop    %rbx
ret
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xea4(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
pop    %rbx
jmp    1050 <malloc@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Out of memory!\0A\00"
@.str.1 = [12 x i8] c"Size error!\00"
define i8* @my_malloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = trunc i64 %4 to i32
%6 = call i8* @malloc(i32 %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %18
11: ; preds = %1
%12 = load i64, i64* %2
%13 = icmp ne i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = call i8* @malloc(i32 1)
store i8* %16, i8** %3
br label %17
17: ; preds = %14, %11
br label %18
18: ; preds = %17, %9
%19 = load i8*, i8** %3
ret i8* %19
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"Size error!\00"
@str = [15 x i8] c"Out of memory!\00"
define noalias i8* @my_malloc(i64 %0) {
%2 = trunc i64 %0 to i32
%3 = tail call i8* @malloc(i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %12
7: ; preds = %1
%8 = icmp eq i64 %0, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
br label %12
12: ; preds = %7, %9, %5
%13 = phi i8* [ %3, %7 ], [ %11, %9 ], [ null, %5 ]
ret i8* %13
}