#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*/ assert(int);
int atol(char *);
void *hbw_calloc(size_t, int);
int hbw_check_available();
int /*<<< orphan*/ hbw_free(void *);
void *hbw_malloc(size_t);
int hbw_posix_memalign(void **, int, size_t);
void *hbw_realloc(void *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  size_t bytes = (argc > 1) ? atol(argv[1]) : 1024 * 1024;
  int avail = hbw_check_available();
  printf("%s returned %s\n", "hbw_check_available",
         avail == 0 ? "SUCCESS" : "FAILURE");
  assert(avail == 0);
  void *a = hbw_malloc(bytes);
  assert(a != NULL);
  hbw_free(a);
  void *b = hbw_calloc(bytes, 1);
  assert(b != NULL);
  hbw_free(b);
  void *c = hbw_realloc(NULL, bytes);
  printf("hbw_realloc c=%p\n", c);
  assert(c != NULL);
  void *d = hbw_realloc(c, 0);
  printf("hbw_realloc c=%p d=%p\n", c, d);
  void *e = NULL;
  int rc = hbw_posix_memalign(&e, 4096, bytes);
  printf("hbw_posix_memalign rc=%d e=%p\n", rc, e);
  assert(rc == 0 && e != NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c5 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atol@plt>
mov    %eax,-0x50(%rbp)
jmp    11d2 <main+0x42>
mov    $0x100000,%eax
mov    %eax,-0x50(%rbp)
jmp    11d2 <main+0x42>
mov    -0x50(%rbp),%eax
cltq
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <hbw_check_available@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
lea    0xe3d(%rip),%rdx
lea    0xe2e(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rdx
lea    0xdfc(%rip),%rdi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <hbw_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rdi
call   1070 <hbw_free@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   10a0 <hbw_calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x30(%rbp),%rdi
call   1070 <hbw_free@plt>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <hbw_realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <hbw_realloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x48(%rbp),%rdi
mov    $0x1000,%esi
call   1030 <hbw_posix_memalign@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdx
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
cmpl   $0x0,-0x4c(%rbp)
mov    %al,-0x51(%rbp)
jne    1320 <main+0x190>
cmpq   $0x0,-0x48(%rbp)
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x100000,%ebx
cmp    $0x2,%edi
jl     11a9 <main+0x19>
mov    0x8(%rsi),%rdi
call   1080 <atol@plt>
movslq %eax,%rbx
xor    %eax,%eax
call   1090 <hbw_check_available@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
lea    0xe65(%rip),%rax
lea    0xe66(%rip),%rdx
cmove  %rax,%rdx
lea    0xe2f(%rip),%rdi
lea    0xe38(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <assert@plt>
mov    %rbx,%rdi
call   10b0 <hbw_malloc@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    %rbp,%rdi
call   1070 <hbw_free@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   10a0 <hbw_calloc@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    %rbp,%rdi
call   1070 <hbw_free@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <hbw_realloc@plt>
mov    %rax,%rbp
lea    0xdf3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1060 <assert@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <hbw_realloc@plt>
lea    0xddc(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,(%rsp)
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1030 <hbw_posix_memalign@plt>
mov    %eax,%ebx
mov    (%rsp),%rdx
lea    0xdc1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
sete   %al
cmpq   $0x0,(%rsp)
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1060 <assert@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"%s returned %s\0A\00"
@.str.1 = [20 x i8] c"hbw_check_available\00"
@.str.2 = [8 x i8] c"SUCCESS\00"
@.str.3 = [8 x i8] c"FAILURE\00"
@.str.4 = [18 x i8] c"hbw_realloc c=%p\0A\00"
@.str.5 = [23 x i8] c"hbw_realloc c=%p d=%p\0A\00"
@.str.6 = [31 x i8] c"hbw_posix_memalign rc=%d e=%p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atol(i8* %19)
br label %22
21: ; preds = %2
br label %22
22: ; preds = %21, %16
%23 = phi i32 [ %20, %16 ], [ 1048576, %21 ]
%24 = sext i32 %23 to i64
store i64 %24, i64* %6
%25 = call i32 (...) @hbw_check_available()
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i64
%29 = select i1 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i64, i64* %6
%36 = call i8* @hbw_malloc(i64 %35)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i8*, i8** %8
%42 = call i32 @hbw_free(i8* %41)
%43 = load i64, i64* %6
%44 = call i8* @hbw_calloc(i64 %43, i32 1)
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = icmp ne i8* %45, null
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i8*, i8** %9
%50 = call i32 @hbw_free(i8* %49)
%51 = load i64, i64* %6
%52 = call i8* @hbw_realloc(i8* null, i64 %51)
store i8* %52, i8** %10
%53 = load i8*, i8** %10
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = load i8*, i8** %10
%56 = icmp ne i8* %55, null
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = load i8*, i8** %10
%60 = call i8* @hbw_realloc(i8* %59, i64 0)
store i8* %60, i8** %11
%61 = load i8*, i8** %10
%62 = load i8*, i8** %11
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %61, i8* %62)
store i8* null, i8** %12
%64 = load i64, i64* %6
%65 = call i32 @hbw_posix_memalign(i8** %12, i32 4096, i64 %64)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = load i8*, i8** %12
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %66, i8* %67)
%69 = load i32, i32* %13
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %22
%72 = load i8*, i8** %12
%73 = icmp ne i8* %72, null
br label %74
74: ; preds = %71, %22
%75 = phi i1 [ false, %22 ], [ %73, %71 ]
%76 = zext i1 %75 to i32
%77 = call i32 @assert(i32 %76)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"%s returned %s\0A\00"
@.str.1 = [20 x i8] c"hbw_check_available\00"
@.str.2 = [8 x i8] c"SUCCESS\00"
@.str.3 = [8 x i8] c"FAILURE\00"
@.str.4 = [18 x i8] c"hbw_realloc c=%p\0A\00"
@.str.5 = [23 x i8] c"hbw_realloc c=%p d=%p\0A\00"
@.str.6 = [31 x i8] c"hbw_posix_memalign rc=%d e=%p\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atol(i8* %7)
%9 = sext i32 %8 to i64
br label %10
10: ; preds = %2, %5
%11 = phi i64 [ %9, %5 ], [ 1048576, %2 ]
%12 = tail call i32 (...) @hbw_check_available()
%13 = icmp eq i32 %12, 0
%14 = select i1 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = zext i1 %13 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = tail call i8* @hbw_malloc(i64 %11)
%19 = icmp ne i8* %18, null
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = tail call i32 @hbw_free(i8* %18)
%23 = tail call i8* @hbw_calloc(i64 %11, i32 1)
%24 = icmp ne i8* %23, null
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
%27 = tail call i32 @hbw_free(i8* %23)
%28 = tail call i8* @hbw_realloc(i8* null, i64 %11)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = icmp ne i8* %28, null
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = tail call i8* @hbw_realloc(i8* %28, i64 0)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %28, i8* %33)
%35 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%36 = call i32 @hbw_posix_memalign(i8** %3, i32 4096, i64 %11)
%37 = load i8*, i8** %3
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %36, i8* %37)
%39 = icmp eq i32 %36, 0
%40 = load i8*, i8** %3
%41 = icmp ne i8* %40, null
%42 = select i1 %39, i1 %41, i1 false
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
ret i32 0
}