#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 */
struct elasticarray {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
size_t SIZE_MAX;
int /*<<< orphan*/ errno;
scalar_t__ resize(struct elasticarray *, size_t);
int elasticarray_resize(struct elasticarray *EA, size_t nrec, size_t reclen) {
  if (nrec > SIZE_MAX / reclen) {
    errno = ENOMEM;
    goto err0;
  }
  if (resize(EA, nrec * reclen))
    goto err0;
  return (0);
err0:
  return (-1);
}


x86 assembly (clang, -O0):
<elasticarray_resize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jbe    1165 <elasticarray_resize+0x55>
mov    0x2e8b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7a(%rip),%rax
mov    %ecx,(%rax)
jmp    1192 <elasticarray_resize+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1030 <resize@plt>
cmp    $0x0,%rax
je     1186 <elasticarray_resize+0x76>
jmp    1192 <elasticarray_resize+0x82>
movl   $0x0,-0x4(%rbp)
jmp    1199 <elasticarray_resize+0x89>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elasticarray_resize>:
push   %rax
mov    %rdx,%rcx
mov    0x2ead(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1148 <elasticarray_resize+0x38>
xor    %edx,%edx
div    %rcx
cmp    %rsi,%rax
jae    1151 <elasticarray_resize+0x41>
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    0x2e94(%rip),%rcx
mov    %eax,(%rcx)
jmp    1162 <elasticarray_resize+0x52>
xor    %edx,%edx
div    %ecx
cmp    %rsi,%rax
jb     1134 <elasticarray_resize+0x24>
imul   %rsi,%rcx
mov    %rcx,%rsi
call   1030 <resize@plt>
test   %rax,%rax
je     1169 <elasticarray_resize+0x59>
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.elasticarray = type { i32 }
@SIZE_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @elasticarray_resize(%struct.elasticarray* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.elasticarray*
%6 = alloca i64
%7 = alloca i64
store %struct.elasticarray* %0, %struct.elasticarray** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %6
%9 = load i64, i64* @SIZE_MAX
%10 = load i64, i64* %7
%11 = udiv i64 %9, %10
%12 = icmp ugt i64 %8, %11
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* @ENOMEM
store i32 %14, i32* @errno
br label %24
15: ; preds = %3
%16 = load %struct.elasticarray*, %struct.elasticarray** %5
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = mul i64 %17, %18
%20 = call i64 @resize(%struct.elasticarray* %16, i64 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
br label %24
23: ; preds = %15
store i32 0, i32* %4
br label %25
24: ; preds = %22, %13
store i32 -1, i32* %4
br label %25
25: ; preds = %24, %23
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
%struct.elasticarray = type { i32 }
@SIZE_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @elasticarray_resize(%struct.elasticarray* %0, i64 %1, i64 %2) {
%4 = load i64, i64* @SIZE_MAX
%5 = udiv i64 %4, %2
%6 = icmp ult i64 %5, %1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @ENOMEM
store i32 %8, i32* @errno
br label %13
9: ; preds = %3
%10 = mul i64 %2, %1
%11 = tail call i64 @resize(%struct.elasticarray* %0, i64 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %9, %7
br label %14
14: ; preds = %9, %13
%15 = phi i32 [ -1, %13 ], [ 0, %9 ]
ret i32 %15
}