#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*/ EINVAL;
int /*<<< orphan*/ errno;
size_t grow_size(size_t, size_t const);
size_t optimal_alignment(size_t const);
size_t optimal_size(size_t const);
scalar_t__ posix_memalign(void **, size_t, size_t);
void *narray_alloc(const size_t nelem, const size_t stride) {
  void *ptr = NULL;
  size_t alignment, size;
  if (nelem == 0 || stride == 0) {
    errno = EINVAL;
    goto error;
  }
  alignment = optimal_alignment(stride);
  size = optimal_size(stride);
  size = grow_size(size, nelem * stride);
  if (posix_memalign(&ptr, alignment, size) != 0) {
    goto error;
  }
  return ptr;
error:
  return NULL;
}


x86 assembly (clang, -O0):
<narray_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116e <narray_alloc+0x2e>
cmpq   $0x0,-0x18(%rbp)
jne    1185 <narray_alloc+0x45>
mov    0x2e63(%rip),%rax
mov    (%rax),%ecx
mov    0x2e62(%rip),%rax
mov    %ecx,(%rax)
jmp    11e2 <narray_alloc+0xa2>
mov    -0x18(%rbp),%rdi
call   1040 <optimal_alignment@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <optimal_size@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1060 <grow_size@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1050 <posix_memalign@plt>
cmp    $0x0,%rax
je     11d5 <narray_alloc+0x95>
jmp    11e2 <narray_alloc+0xa2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ea <narray_alloc+0xaa>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<narray_alloc>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
test   %rdi,%rdi
je     11a0 <narray_alloc+0x60>
mov    %rsi,%r14
test   %rsi,%rsi
je     11a0 <narray_alloc+0x60>
mov    %rdi,%rbx
mov    %r14,%rdi
call   1040 <optimal_alignment@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <optimal_size@plt>
imul   %r14,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <grow_size@plt>
lea    0x8(%rsp),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <posix_memalign@plt>
test   %rax,%rax
jne    11b2 <narray_alloc+0x72>
mov    0x8(%rsp),%rax
jmp    11b4 <narray_alloc+0x74>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e30(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @narray_alloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i8* null, i8** %6
%9 = load i64, i64* %4
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i64, i64* %5
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11, %2
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
br label %33
16: ; preds = %11
%17 = load i64, i64* %5
%18 = call i64 @optimal_alignment(i64 %17)
store i64 %18, i64* %7
%19 = load i64, i64* %5
%20 = call i64 @optimal_size(i64 %19)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = load i64, i64* %4
%23 = load i64, i64* %5
%24 = mul i64 %22, %23
%25 = call i64 @grow_size(i64 %21, i64 %24)
store i64 %25, i64* %8
%26 = load i64, i64* %7
%27 = load i64, i64* %8
%28 = call i64 @posix_memalign(i8** %6, i64 %26, i64 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %16
br label %33
31: ; preds = %16
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %34
33: ; preds = %30, %14
store i8* null, i8** %3
br label %34
34: ; preds = %33, %31
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @narray_alloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = icmp eq i64 %0, 0
%6 = icmp eq i64 %1, 0
%7 = or i1 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %19
10: ; preds = %2
%11 = tail call i64 @optimal_alignment(i64 %1)
%12 = tail call i64 @optimal_size(i64 %1)
%13 = mul i64 %1, %0
%14 = tail call i64 @grow_size(i64 %12, i64 %13)
%15 = call i64 @posix_memalign(i8** %3, i64 %11, i64 %14)
%16 = icmp eq i64 %15, 0
%17 = load i8*, i8** %3
%18 = select i1 %16, i8* %17, i8* null
br label %19
19: ; preds = %10, %8
%20 = phi i8* [ null, %8 ], [ %18, %10 ]
ret i8* %20
}