#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*/ EOVERFLOW;
size_t const SIZE_MAX;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int mulof_s(size_t const, size_t const);
void *realloc(void *, size_t const);
void *xrealloc(void **const p, const size_t hdr, const size_t n,
               const size_t size) {
  assert(p != NULL);
  if (mulof_s(n, size) == true) {
    errno = EOVERFLOW;
    return (NULL);
  }
  const size_t bytes = n * size;
  if (bytes > SIZE_MAX - hdr) {
    errno = EOVERFLOW;
    return (NULL);
  }
  void *new_p = realloc(*p, hdr + bytes);
  return (new_p == NULL ? (NULL) : (*p = new_p));
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <mulof_s@plt>
cmp    $0x1,%eax
jne    118f <xrealloc+0x5f>
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e58(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    121b <xrealloc+0xeb>
mov    -0x20(%rbp),%rax
imul   -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rcx
sub    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jbe    11d6 <xrealloc+0xa6>
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    0x2e11(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    121b <xrealloc+0xeb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1204 <xrealloc+0xd4>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    1213 <xrealloc+0xe3>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <mulof_s@plt>
cmp    $0x1,%eax
je     1171 <xrealloc+0x41>
imul   %r12,%rbx
mov    %r15,%rax
neg    %rax
cmp    %rax,%rbx
jbe    1191 <xrealloc+0x61>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    0x2e57(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r14),%rdi
add    %r15,%rbx
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1183 <xrealloc+0x53>
mov    %rax,(%r14)
jmp    1185 <xrealloc+0x55>

LLVM IR (-O0):
@EOVERFLOW = global i32 0
@errno = global i32 0
@SIZE_MAX = constant i64 0
define i8* @xrealloc(i8** %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store i8** %0, i8*** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = load i8**, i8*** %6
%13 = icmp ne i8** %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i64, i64* %8
%17 = load i64, i64* %9
%18 = call i32 @mulof_s(i64 %16, i64 %17)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* @EOVERFLOW
store i32 %21, i32* @errno
store i8* null, i8** %5
br label %48
22: ; preds = %4
%23 = load i64, i64* %8
%24 = load i64, i64* %9
%25 = mul i64 %23, %24
store i64 %25, i64* %10
%26 = load i64, i64* %10
%27 = load i64, i64* @SIZE_MAX
%28 = load i64, i64* %7
%29 = sub i64 %27, %28
%30 = icmp ugt i64 %26, %29
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = load i32, i32* @EOVERFLOW
store i32 %32, i32* @errno
store i8* null, i8** %5
br label %48
33: ; preds = %22
%34 = load i8**, i8*** %6
%35 = load i8*, i8** %34
%36 = load i64, i64* %7
%37 = load i64, i64* %10
%38 = add i64 %36, %37
%39 = call i8* @realloc(i8* %35, i64 %38)
store i8* %39, i8** %11
%40 = load i8*, i8** %11
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %43
42: ; preds = %33
br label %46
43: ; preds = %33
%44 = load i8*, i8** %11
%45 = load i8**, i8*** %6
store i8* %44, i8** %45
br label %46
46: ; preds = %43, %42
%47 = phi i8* [ null, %42 ], [ %44, %43 ]
store i8* %47, i8** %5
br label %48
48: ; preds = %46, %31, %20
%49 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
@EOVERFLOW = global i32 0
@errno = global i32 0
@SIZE_MAX = constant i64 0
define i8* @xrealloc(i8** %0, i64 %1, i64 %2, i64 %3) {
%5 = icmp ne i8** %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i32 @mulof_s(i64 %2, i64 %3)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = load i32, i32* @EOVERFLOW
store i32 %11, i32* @errno
br label %24
12: ; preds = %4
%13 = mul i64 %3, %2
%14 = sub i64 0, %1
%15 = icmp ugt i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* @EOVERFLOW
store i32 %17, i32* @errno
br label %24
18: ; preds = %12
%19 = load i8*, i8** %0
%20 = add i64 %13, %1
%21 = tail call i8* @realloc(i8* %19, i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %18
store i8* %21, i8** %0
br label %24
24: ; preds = %16, %18, %23, %10
%25 = phi i8* [ null, %10 ], [ null, %16 ], [ %21, %23 ], [ null, %18 ]
ret i8* %25
}