#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*/ ptr;

/* Variables and functions */
int CHAR_BIT;
int EOVERFLOW;
size_t SIZE_MAX;
scalar_t__ __predict_false(int);
int errno;
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void **, int);
void *realloc(void *, size_t);
int reallocarr(void *ptr, size_t number, size_t size) {
  int saved_errno, result;
  void *optr;
  void *nptr;
  saved_errno = errno;
  memcpy(&optr, ptr, sizeof(ptr));
  if (number == 0 || size == 0) {
    free(optr);
    nptr = NULL;
    memcpy(ptr, &nptr, sizeof(ptr));
    errno = saved_errno;
    return 0;
  }
  if (__predict_false((number | size) >=
                          (((size_t)1) << (sizeof(size_t) * CHAR_BIT / 2)) &&
                      number > SIZE_MAX / size)) {
    errno = saved_errno;
    return EOVERFLOW;
  }
  nptr = realloc(optr, number * size);
  if (__predict_false(nptr == NULL)) {
    result = errno;
  } else {
    result = 0;
    memcpy(ptr, &nptr, sizeof(ptr));
  }
  errno = saved_errno;
  return result;
}


x86 assembly (clang, -O0):
<reallocarr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     1188 <reallocarr+0x48>
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <reallocarr+0x83>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e1b(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12c7 <reallocarr+0x187>
mov    -0x18(%rbp),%rax
or     -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e02(%rip),%rax
movslq (%rax),%rcx
shl    $0x3,%rcx
shr    $0x1,%rcx
mov    $0x1,%edx
shl    %cl,%rdx
mov    -0x48(%rbp),%rcx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x39(%rbp)
jb     1225 <reallocarr+0xe5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
seta   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <__predict_false@plt>
cmp    $0x0,%rax
je     1259 <reallocarr+0x119>
mov    -0x24(%rbp),%ecx
mov    0x2d8a(%rip),%rax
mov    %ecx,(%rax)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c7 <reallocarr+0x187>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <__predict_false@plt>
cmp    $0x0,%rax
je     129c <reallocarr+0x15c>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    12b5 <reallocarr+0x175>
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d11(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reallocarr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e74(%rip),%r12
mov    (%r12),%ebp
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
test   %r15,%r15
je     11b3 <reallocarr+0x73>
test   %rbx,%rbx
je     11b3 <reallocarr+0x73>
mov    %rbx,%rax
or     %r15,%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%cl
shl    $0x2,%cl
shr    %cl,%rax
test   %rax,%rax
je     11dd <reallocarr+0x9d>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     11e1 <reallocarr+0xa1>
xor    %edx,%edx
div    %rbx
jmp    11e5 <reallocarr+0xa5>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    %ebp,(%r12)
xor    %eax,%eax
jmp    124b <reallocarr+0x10b>
xor    %edi,%edi
jmp    11ee <reallocarr+0xae>
xor    %edx,%edx
div    %ebx
xor    %edi,%edi
cmp    %r15,%rax
setb   %dil
call   1030 <__predict_false@plt>
test   %rax,%rax
je     1207 <reallocarr+0xc7>
mov    %ebp,(%r12)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
jmp    124b <reallocarr+0x10b>
mov    0x8(%rsp),%rdi
imul   %r15,%rbx
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,(%rsp)
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1030 <__predict_false@plt>
test   %rax,%rax
je     1235 <reallocarr+0xf5>
mov    (%r12),%eax
jmp    1247 <reallocarr+0x107>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
mov    %ebp,(%r12)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@CHAR_BIT = global i32 0
@SIZE_MAX = global i64 0
@EOVERFLOW = global i32 0
define i32 @reallocarr(i8* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%12 = load i32, i32* @errno
store i32 %12, i32* %8
%13 = bitcast i8** %10 to i8*
%14 = load i8*, i8** %5
%15 = bitcast i8* %14 to i8**
%16 = call i32 @memcpy(i8* %13, i8** %15, i32 8)
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %3
%20 = load i64, i64* %7
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19, %3
%23 = load i8*, i8** %10
%24 = call i32 @free(i8* %23)
store i8* null, i8** %11
%25 = load i8*, i8** %5
%26 = call i32 @memcpy(i8* %25, i8** %11, i32 8)
%27 = load i32, i32* %8
store i32 %27, i32* @errno
store i32 0, i32* %4
br label %71
28: ; preds = %19
%29 = load i64, i64* %6
%30 = load i64, i64* %7
%31 = or i64 %29, %30
%32 = load i32, i32* @CHAR_BIT
%33 = sext i32 %32 to i64
%34 = mul i64 8, %33
%35 = udiv i64 %34, 2
%36 = shl i64 1, %35
%37 = icmp uge i64 %31, %36
br i1 %37, label %38, label %44
38: ; preds = %28
%39 = load i64, i64* %6
%40 = load i64, i64* @SIZE_MAX
%41 = load i64, i64* %7
%42 = udiv i64 %40, %41
%43 = icmp ugt i64 %39, %42
br label %44
44: ; preds = %38, %28
%45 = phi i1 [ false, %28 ], [ %43, %38 ]
%46 = zext i1 %45 to i32
%47 = call i64 @__predict_false(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* %8
store i32 %50, i32* @errno
%51 = load i32, i32* @EOVERFLOW
store i32 %51, i32* %4
br label %71
52: ; preds = %44
%53 = load i8*, i8** %10
%54 = load i64, i64* %6
%55 = load i64, i64* %7
%56 = mul i64 %54, %55
%57 = call i8* @realloc(i8* %53, i64 %56)
store i8* %57, i8** %11
%58 = load i8*, i8** %11
%59 = icmp eq i8* %58, null
%60 = zext i1 %59 to i32
%61 = call i64 @__predict_false(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %52
%64 = load i32, i32* @errno
store i32 %64, i32* %9
br label %68
65: ; preds = %52
store i32 0, i32* %9
%66 = load i8*, i8** %5
%67 = call i32 @memcpy(i8* %66, i8** %11, i32 8)
br label %68
68: ; preds = %65, %63
%69 = load i32, i32* %8
store i32 %69, i32* @errno
%70 = load i32, i32* %9
store i32 %70, i32* %4
br label %71
71: ; preds = %68, %49, %22
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
@errno = global i32 0
@CHAR_BIT = global i32 0
@SIZE_MAX = global i64 0
@EOVERFLOW = global i32 0
define i32 @reallocarr(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = bitcast i8** %4 to i8*
%7 = bitcast i8** %5 to i8*
%8 = load i32, i32* @errno
%9 = bitcast i8* %0 to i8**
%10 = call i32 @memcpy(i8* %6, i8** %9, i32 8)
%11 = icmp eq i64 %1, 0
%12 = icmp eq i64 %2, 0
%13 = or i1 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 @free(i8* %15)
store i8* null, i8** %5
%17 = call i32 @memcpy(i8* %0, i8** %5, i32 8)
store i32 %8, i32* @errno
br label %51
18: ; preds = %3
%19 = or i64 %2, %1
%20 = load i32, i32* @CHAR_BIT
%21 = sext i32 %20 to i64
%22 = shl nsw i64 %21, 2
%23 = and i64 %22, 9223372036854775804
%24 = lshr i64 %19, %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %18
%27 = load i64, i64* @SIZE_MAX
%28 = udiv i64 %27, %2
%29 = icmp ult i64 %28, %1
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %26, %18
%32 = phi i32 [ 0, %18 ], [ %30, %26 ]
%33 = call i64 @__predict_false(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %31
store i32 %8, i32* @errno
%36 = load i32, i32* @EOVERFLOW
br label %51
37: ; preds = %31
%38 = load i8*, i8** %4
%39 = mul i64 %2, %1
%40 = call i8* @realloc(i8* %38, i64 %39)
store i8* %40, i8** %5
%41 = icmp eq i8* %40, null
%42 = zext i1 %41 to i32
%43 = call i64 @__predict_false(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %37
%46 = load i32, i32* @errno
br label %49
47: ; preds = %37
%48 = call i32 @memcpy(i8* %0, i8** %5, i32 8)
br label %49
49: ; preds = %47, %45
%50 = phi i32 [ %46, %45 ], [ 0, %47 ]
store i32 %8, i32* @errno
br label %51
51: ; preds = %49, %35, %14
%52 = phi i32 [ 0, %14 ], [ %36, %35 ], [ %50, %49 ]
ret i32 %52
}