#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 size;

/* Variables and functions */
void *malloc(int);
int malloc_usable_size(void *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
void *htalloc(size_t cap, size_t size) {
  void *table =
      malloc((sizeof(const char *) +
              (((size) + ((sizeof(void *)) - 1)) & ~((sizeof(void *)) - 1))) *
             cap);
  if (!table)
    return NULL;
  cap = (malloc_usable_size(table) /
         (sizeof(const char *) +
          (((size) + ((sizeof(void *)) - 1)) & ~((sizeof(void *)) - 1))));
  memset(table, 0,
         (sizeof(const char *) +
          (((size) + ((sizeof(void *)) - 1)) & ~((sizeof(void *)) - 1))) *
             cap);
  return table;
}


x86 assembly (clang, -O0):
<htalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x7,%rax
and    $0xfffffffffffffff8,%rax
add    $0x8,%rax
imul   -0x10(%rbp),%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1178 <htalloc+0x48>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <htalloc+0x96>
mov    -0x20(%rbp),%rdi
call   1040 <malloc_usable_size@plt>
cltq
mov    -0x18(%rbp),%rcx
add    $0x7,%rcx
and    $0xfffffffffffffff8,%rcx
add    $0x8,%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    $0x7,%rax
and    $0xfffffffffffffff8,%rax
add    $0x8,%rax
imul   -0x10(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htalloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
add    $0xf,%rbx
and    $0xfffffffffffffff8,%rbx
imul   %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1182 <htalloc+0x52>
mov    %r14,%rdi
call   1040 <malloc_usable_size@plt>
mov    %eax,%ecx
cltq
mov    %rax,%rdx
or     %rbx,%rdx
shr    $0x20,%rdx
je     116e <htalloc+0x3e>
xor    %edx,%edx
div    %rbx
jmp    1174 <htalloc+0x44>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
sub    %edx,%ecx
mov    %r14,%rdi
xor    %esi,%esi
mov    %ecx,%edx
call   1030 <memset@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @htalloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = add i64 %7, 7
%9 = and i64 %8, -8
%10 = add i64 8, %9
%11 = load i64, i64* %4
%12 = mul i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i8* null, i8** %3
br label %37
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = call i32 @malloc_usable_size(i8* %19)
%21 = sext i32 %20 to i64
%22 = load i64, i64* %5
%23 = add i64 %22, 7
%24 = and i64 %23, -8
%25 = add i64 8, %24
%26 = udiv i64 %21, %25
store i64 %26, i64* %4
%27 = load i8*, i8** %6
%28 = load i64, i64* %5
%29 = add i64 %28, 7
%30 = and i64 %29, -8
%31 = add i64 8, %30
%32 = load i64, i64* %4
%33 = mul i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = call i32 @memset(i8* %27, i32 0, i32 %34)
%36 = load i8*, i8** %6
store i8* %36, i8** %3
br label %37
37: ; preds = %18, %17
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define i8* @htalloc(i64 %0, i64 %1) {
%3 = add i64 %1, 15
%4 = and i64 %3, -8
%5 = mul i64 %4, %0
%6 = trunc i64 %5 to i32
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = tail call i32 @malloc_usable_size(i8* %7)
%11 = sext i32 %10 to i64
%12 = urem i64 %11, %4
%13 = trunc i64 %12 to i32
%14 = sub i32 %10, %13
%15 = tail call i32 @memset(i8* %7, i32 0, i32 %14)
br label %16
16: ; preds = %2, %9
ret i8* %7
}