#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 */
size_t SIZE_MAX;
void *malloc(size_t);
void *realloc(void *, size_t);
int /*<<< orphan*/ ssh_fatal(char *, ...);
void *ssh_realloc(void *ptr, size_t nmemb, size_t size) {
  void *new_ptr;
  size_t new_size = nmemb * size;
  if (new_size == 0)
    ssh_fatal("xrealloc: zero size");
  if (SIZE_MAX / nmemb < size)
    ssh_fatal("xrealloc: nmemb * size > SIZE_MAX");
  if (ptr == NULL)
    new_ptr = malloc(new_size);
  else
    new_ptr = realloc(ptr, new_size);
  if (new_ptr == NULL)
    ssh_fatal("xrealloc: out of memory (new_size %zu bytes)", new_size);
  return new_ptr;
}


x86 assembly (clang, -O0):
<ssh_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116a <ssh_realloc+0x3a>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1050 <ssh_fatal@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
jae    1194 <ssh_realloc+0x64>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1050 <ssh_fatal@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11b1 <ssh_realloc+0x81>
mov    -0x28(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11c2 <ssh_realloc+0x92>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <ssh_realloc+0xaf>
mov    -0x28(%rbp),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1050 <ssh_fatal@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssh_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%r12
imul   %rsi,%r12
test   %r12,%r12
jne    115b <ssh_realloc+0x2b>
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1050 <ssh_fatal@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     119b <ssh_realloc+0x6b>
xor    %edx,%edx
div    %rbx
cmp    %r15,%rax
jae    1189 <ssh_realloc+0x59>
lea    0xe92(%rip),%rdi
xor    %eax,%eax
call   1050 <ssh_fatal@plt>
test   %r14,%r14
je     11a6 <ssh_realloc+0x76>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <realloc@plt>
jmp    11ae <ssh_realloc+0x7e>
xor    %edx,%edx
div    %ebx
cmp    %r15,%rax
jb     117b <ssh_realloc+0x4b>
jmp    1189 <ssh_realloc+0x59>
mov    %r12,%rdi
call   1030 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c7 <ssh_realloc+0x97>
lea    0xe79(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <ssh_fatal@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"xrealloc: zero size\00"
@SIZE_MAX = global i64 0
@.str.1 = [34 x i8] c"xrealloc: nmemb * size > SIZE_MAX\00"
@.str.2 = [45 x i8] c"xrealloc: out of memory (new_size %zu bytes)\00"
define i8* @ssh_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%9 = load i64, i64* %5
%10 = load i64, i64* %6
%11 = mul i64 %9, %10
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 (i8*, ...) @ssh_fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i64, i64* @SIZE_MAX
%18 = load i64, i64* %5
%19 = udiv i64 %17, %18
%20 = load i64, i64* %6
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (i8*, ...) @ssh_fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i8*, i8** %4
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i64, i64* %8
%29 = call i8* @malloc(i64 %28)
store i8* %29, i8** %7
br label %34
30: ; preds = %24
%31 = load i8*, i8** %4
%32 = load i64, i64* %8
%33 = call i8* @realloc(i8* %31, i64 %32)
store i8* %33, i8** %7
br label %34
34: ; preds = %30, %27
%35 = load i8*, i8** %7
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i64, i64* %8
%39 = call i32 (i8*, ...) @ssh_fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i64 %38)
br label %40
40: ; preds = %37, %34
%41 = load i8*, i8** %7
ret i8* %41
}

LLVM IR (-O2):
@.str = [20 x i8] c"xrealloc: zero size\00"
@SIZE_MAX = global i64 0
@.str.1 = [34 x i8] c"xrealloc: nmemb * size > SIZE_MAX\00"
@.str.2 = [45 x i8] c"xrealloc: out of memory (new_size %zu bytes)\00"
define i8* @ssh_realloc(i8* %0, i64 %1, i64 %2) {
%4 = mul i64 %2, %1
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @ssh_fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = load i64, i64* @SIZE_MAX
%10 = udiv i64 %9, %1
%11 = icmp ult i64 %10, %2
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @ssh_fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = icmp eq i8* %0, null
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i8* @malloc(i64 %4)
br label %20
18: ; preds = %14
%19 = tail call i8* @realloc(i8* %0, i64 %4)
br label %20
20: ; preds = %18, %16
%21 = phi i8* [ %17, %16 ], [ %19, %18 ]
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 (i8*, ...) @ssh_fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i64 %4)
br label %25
25: ; preds = %23, %20
ret i8* %21
}