#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*/ free(void *);
void *malloc(int);
size_t malloc_usable_size(void *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, ...);
int do_test(void) {
  size_t usable_size;
  void *p = malloc(7);
  if (!p) {
    printf("memory allocation failed\n");
    return 1;
  }
  usable_size = malloc_usable_size(p);
  if (usable_size != 7) {
    printf("malloc_usable_size: expected 7 but got %zu\n", usable_size);
    return 1;
  }
  memset(p, 0, usable_size);
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x7,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <do_test+0x3b>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <do_test+0x90>
mov    -0x18(%rbp),%rdi
call   1060 <malloc_usable_size@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x7,-0x10(%rbp)
je     11c1 <do_test+0x71>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <do_test+0x90>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %rbx
push   %rax
mov    $0x7,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     119e <do_test+0x3e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <malloc_usable_size@plt>
cmp    $0x7,%rax
jne    11ac <do_test+0x4c>
xor    %ebp,%ebp
mov    $0x7,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11c2 <do_test+0x62>
lea    0xe87(%rip),%rdi
call   1040 <puts@plt>
jmp    11bd <do_test+0x5d>
lea    0xe4d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"memory allocation failed\0A\00"
@.str.1 = [44 x i8] c"malloc_usable_size: expected 7 but got %zu\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = call i8* @malloc(i32 7)
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %23
9: ; preds = %0
%10 = load i8*, i8** %3
%11 = call i64 @malloc_usable_size(i8* %10)
store i64 %11, i64* %2
%12 = load i64, i64* %2
%13 = icmp ne i64 %12, 7
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i64, i64* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %15)
store i32 1, i32* %1
br label %23
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = load i64, i64* %2
%20 = call i32 @memset(i8* %18, i32 0, i64 %19)
%21 = load i8*, i8** %3
%22 = call i32 @free(i8* %21)
store i32 0, i32* %1
br label %23
23: ; preds = %17, %14, %7
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str.1 = [44 x i8] c"malloc_usable_size: expected 7 but got %zu\0A\00"
@str = [25 x i8] c"memory allocation failed\00"
define i32 @do_test() {
%1 = tail call dereferenceable_or_null(7) i8* @malloc(i32 7)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %13
5: ; preds = %0
%6 = tail call i64 @malloc_usable_size(i8* %1)
%7 = icmp eq i64 %6, 7
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %13
10: ; preds = %5
%11 = tail call i32 @memset(i8* %1, i32 0, i64 7)
%12 = tail call i32 @free(i8* %1)
br label %13
13: ; preds = %10, %8, %3
%14 = phi i32 [ 1, %8 ], [ 0, %10 ], [ 1, %3 ]
ret i32 %14
}