void *cx_malloc(int size) {
  char *mem;
  if ((mem = malloc(size)) == 0) {
    if ((write(2, "Malloc error\n", 13)) == -1)
      exit(1);
    exit(1);
  }
  while (size > 0) {
    mem[size - 1] = 0;
    size = size - 1;
  }
  return (mem);
}


x86 assembly (clang, -O0):
<cx_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movslq -0x4(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1187 <cx_malloc+0x57>
mov    $0x2,%edi
lea    0xea2(%rip),%rsi
mov    $0xd,%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    117d <cx_malloc+0x4d>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    118c <cx_malloc+0x5c>
cmpl   $0x0,-0x4(%rbp)
jle    11b5 <cx_malloc+0x85>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    118c <cx_malloc+0x5c>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cx_malloc>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
movslq %edi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     116f <cx_malloc+0x2f>
mov    %rax,%rbx
test   %ebp,%ebp
jle    1165 <cx_malloc+0x25>
mov    %ebp,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe8a(%rip),%rsi
mov    $0x2,%edi
mov    $0xd,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Malloc error\0A\00"
define i8* @cx_malloc(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = sext i32 %4 to i64
%6 = call i8* @malloc(i64 %5)
store i8* %6, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 13)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %8
unreachable
12: ; preds = %8
unreachable
13: ; preds = %1
br label %14
14: ; preds = %17, %13
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = load i32, i32* %2
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8 0, i8* %22
%23 = load i32, i32* %2
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %2
br label %14
25: ; preds = %14
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@.str = [14 x i8] c"Malloc error\0A\00"
define i8* @cx_malloc(i32 %0) {
%2 = sext i32 %0 to i64
%3 = tail call i8* @malloc(i64 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %11
7: ; preds = %5
%8 = zext i32 %0 to i64
br label %11
9: ; preds = %1
%10 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 13)
tail call void @exit(i32 1)
unreachable
11: ; preds = %7, %5
ret i8* %3
}