#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 */
char *malloc(int);
void *ft_memalloc(size_t size) {
  size_t i;
  char *mem;
  i = 0;
  if (size == 0)
    return (NULL);
  mem = malloc(sizeof(char) * size);
  if (!mem)
    return (NULL);
  while (i < size) {
    mem[i] = 0;
    i++;
  }
  return ((void *)mem);
}


x86 assembly (clang, -O0):
<ft_memalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113c <ft_memalloc+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    119f <ft_memalloc+0x8f>
mov    -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1167 <ft_memalloc+0x57>
movq   $0x0,-0x8(%rbp)
jmp    119f <ft_memalloc+0x8f>
jmp    116c <ft_memalloc+0x5c>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1197 <ft_memalloc+0x87>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    116c <ft_memalloc+0x5c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_memalloc>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1148 <ft_memalloc+0x28>
mov    %rdi,%rbx
call   1040 <malloc@plt>
test   %rax,%rax
je     1148 <ft_memalloc+0x28>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <memset@plt>
jmp    114b <ft_memalloc+0x2b>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @ft_memalloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i64 %0, i64* %3
store i64 0, i64* %4
%6 = load i64, i64* %3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %30
9: ; preds = %1
%10 = load i64, i64* %3
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %9
store i8* null, i8** %2
br label %30
17: ; preds = %9
br label %18
18: ; preds = %22, %17
%19 = load i64, i64* %4
%20 = load i64, i64* %3
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i64, i64* %4
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
%26 = load i64, i64* %4
%27 = add i64 %26, 1
store i64 %27, i64* %4
br label %18
28: ; preds = %18
%29 = load i8*, i8** %5
store i8* %29, i8** %2
br label %30
30: ; preds = %28, %16, %8
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
define noalias i8* @ft_memalloc(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %8, label %3
3: ; preds = %1
%4 = trunc i64 %0 to i32
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %3
br label %8
8: ; preds = %7, %3, %1
%9 = phi i8* [ null, %1 ], [ null, %3 ], [ %5, %7 ]
ret i8* %9
}