#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 */
int /*<<< orphan*/ ENOMEM;
void *calloc(int, int);
int /*<<< orphan*/ errno;
extern void *vec_new_cap(size_t size, size_t nitems) {
  void *vec = calloc(1, (sizeof(size_t) * 3) + size * nitems);
  if (vec == NULL) {
    errno = ENOMEM;
    return NULL;
  }
  vec = (char *)vec + (sizeof(size_t) * 3);
  (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)))) = nitems;
  (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)) + sizeof(size_t))) = 0;
  (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)) + sizeof(size_t) * 2)) =
      size;
  return vec;
}


x86 assembly (clang, -O0):
<vec_new_cap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
add    $0x18,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1167 <vec_new_cap+0x57>
mov    0x2e91(%rip),%rax
mov    (%rax),%ecx
mov    0x2e80(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    119f <vec_new_cap+0x8f>
mov    -0x20(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x18(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,-0x10(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x8(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vec_new_cap>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
imul   %ebx,%esi
add    $0x18,%esi
mov    $0x1,%edi
call   1030 <calloc@plt>
test   %rax,%rax
je     113f <vec_new_cap+0x2f>
mov    %rax,%rcx
add    $0x18,%rax
mov    %r14,(%rcx)
mov    %rbx,0x10(%rcx)
jmp    1153 <vec_new_cap+0x43>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    0x2e89(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @vec_new_cap(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* %4
%8 = load i64, i64* %5
%9 = mul i64 %7, %8
%10 = add i64 24, %9
%11 = trunc i64 %10 to i32
%12 = call i8* @calloc(i32 1, i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
store i8* null, i8** %3
br label %34
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 24
store i8* %19, i8** %6
%20 = load i64, i64* %5
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i64 -24
%23 = bitcast i8* %22 to i64*
store i64 %20, i64* %23
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 -24
%26 = getelementptr inbounds i8, i8* %25, i64 8
%27 = bitcast i8* %26 to i64*
store i64 0, i64* %27
%28 = load i64, i64* %4
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i64 -24
%31 = getelementptr inbounds i8, i8* %30, i64 16
%32 = bitcast i8* %31 to i64*
store i64 %28, i64* %32
%33 = load i8*, i8** %6
store i8* %33, i8** %3
br label %34
34: ; preds = %17, %15
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define noalias i8* @vec_new_cap(i64 %0, i64 %1) {
%3 = mul i64 %1, %0
%4 = trunc i64 %3 to i32
%5 = add i32 %4, 24
%6 = tail call i8* @calloc(i32 1, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @ENOMEM
store i32 %9, i32* @errno
br label %15
10: ; preds = %2
%11 = getelementptr inbounds i8, i8* %6, i64 24
%12 = bitcast i8* %6 to i64*
store i64 %1, i64* %12
%13 = getelementptr inbounds i8, i8* %6, i64 16
%14 = bitcast i8* %13 to i64*
store i64 %0, i64* %14
br label %15
15: ; preds = %10, %8
%16 = phi i8* [ null, %8 ], [ %11, %10 ]
ret i8* %16
}