#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t, size_t);
void *malloc(size_t);
int /*<<< orphan*/ stderr;
void *ckalloc(size_t amount, size_t size) {
  void *p;
  static size_t mtotal;
  mtotal += amount * size;
  if ((p = malloc(amount * size)) == NULL) {
    fprintf(stderr, "Ran out of near memory: %ld*%ld/%ld\n", amount, size,
            mtotal);
    exit(1);
  }
  return (p);
}


x86 assembly (clang, -O0):
<ckalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
imul   -0x10(%rbp),%rax
add    0x2ef0(%rip),%rax
mov    %rax,0x2ee9(%rip)
mov    -0x8(%rbp),%rdi
imul   -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a1 <ckalloc+0x71>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    0x2eb5(%rip),%r8
lea    0xe6e(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ckalloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
imul   %r15,%rdi
mov    0x2ef7(%rip),%rbx
add    %rdi,%rbx
mov    %rbx,0x2eed(%rip)
call   1040 <malloc@plt>
test   %rax,%rax
je     1163 <ckalloc+0x33>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@ckalloc.mtotal = internal global i64 0
@stderr = global i32 0
@.str = [37 x i8] c"Ran out of near memory: %ld*%ld/%ld\0A\00"
define i8* @ckalloc(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i64 %0, i64* %3
store i64 %1, i64* %4
%6 = load i64, i64* %3
%7 = load i64, i64* %4
%8 = mul i64 %6, %7
%9 = load i64, i64* @ckalloc.mtotal
%10 = add i64 %9, %8
store i64 %10, i64* @ckalloc.mtotal
%11 = load i64, i64* %3
%12 = load i64, i64* %4
%13 = mul i64 %11, %12
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i64, i64* %3
%19 = load i64, i64* %4
%20 = load i64, i64* @ckalloc.mtotal
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %18, i64 %19, i64 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8*, i8** %5
ret i8* %24
}

LLVM IR (-O2):
@ckalloc.mtotal = internal unnamed_addr global i64 0
@stderr = global i32 0
@.str = [37 x i8] c"Ran out of near memory: %ld*%ld/%ld\0A\00"
define noalias i8* @ckalloc(i64 %0, i64 %1) {
%3 = mul i64 %1, %0
%4 = load i64, i64* @ckalloc.mtotal
%5 = add i64 %4, %3
store i64 %5, i64* @ckalloc.mtotal
%6 = tail call i8* @malloc(i64 %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i64 %0, i64 %1, i64 %5)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
ret i8* %6
}