#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 */
typedef int /*<<< orphan*/ int8_t;
typedef size_t int32_t;

/* Variables and functions */
size_t *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *newarr(int32_t nelements, size_t size) {
  int32_t *ptr = malloc(sizeof(size_t) + (nelements * size));
  if (ptr == NULL)
    return NULL;
  *ptr = nelements;
  void *ret = (void *)((int8_t *)ptr + sizeof(size_t));
  ;
  memset(ret, 0, nelements * size);
  return ret;
}


x86 assembly (clang, -O0):
<newarr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
add    $0x8,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1160 <newarr+0x40>
movq   $0x0,-0x8(%rbp)
jmp    1193 <newarr+0x73>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
imul   -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newarr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
imul   %rdi,%r14
lea    0x8(%r14),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1156 <newarr+0x36>
mov    %rax,%rbx
mov    %r15,(%rax)
add    $0x20,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
jmp    1158 <newarr+0x38>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @newarr(i64 %0, i64 %1) {
%3 = mul i64 %1, %0
%4 = trunc i64 %3 to i32
%5 = add i32 %4, 8
%6 = tail call i64* @malloc(i32 %5)
%7 = icmp eq i64* %6, null
br i1 %7, label %12, label %8
8: ; preds = %2
store i64 %0, i64* %6
%9 = getelementptr inbounds i64, i64* %6, i64 4
%10 = bitcast i64* %9 to i8*
%11 = tail call i32 @memset(i8* %10, i32 0, i64 %3)
br label %12
12: ; preds = %2, %8
%13 = phi i8* [ %10, %8 ], [ null, %2 ]
ret i8* %13
}