#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*/ _SC_PAGESIZE;
size_t malloc_usable_size(void *);
unsigned int sysconf(int /*<<< orphan*/);
unsigned int nvmed_check_buffer(void *nvmed_buf) {
  size_t buf_size;
  unsigned int predict_size, actual_size = 0;
  unsigned int *magic;
  if (nvmed_buf == NULL)
    return 0;
  buf_size = malloc_usable_size(nvmed_buf);
  if (buf_size < sysconf(_SC_PAGESIZE))
    return 0;
  predict_size = buf_size / sysconf(_SC_PAGESIZE);
  if (predict_size >= 512)
    predict_size -= predict_size >> 9;
  magic = nvmed_buf + (sysconf(_SC_PAGESIZE) * predict_size);
  if (*magic == 0x4E564DED) {
    actual_size = *(++magic);
  } else {
    predict_size--;
    magic = nvmed_buf + (sysconf(_SC_PAGESIZE) * predict_size);
    if (*magic == 0x4E564DED) {
      actual_size = *(++magic);
    }
  }
  return actual_size;
}


x86 assembly (clang, -O0):
<nvmed_check_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114a <nvmed_check_buffer+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    1280 <nvmed_check_buffer+0x160>
mov    -0x10(%rbp),%rdi
call   1030 <malloc_usable_size@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,%ecx
cmp    %rcx,%rax
jae    118a <nvmed_check_buffer+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    1280 <nvmed_check_buffer+0x160>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x1c(%rbp)
cmpl   $0x200,-0x1c(%rbp)
jb     11cb <nvmed_check_buffer+0xab>
mov    -0x1c(%rbp),%ecx
shr    $0x9,%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
imul   -0x1c(%rbp),%ecx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x4e564ded,(%rax)
jne    121e <nvmed_check_buffer+0xfe>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
mov    0x4(%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    127a <nvmed_check_buffer+0x15a>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
imul   -0x1c(%rbp),%ecx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x4e564ded,(%rax)
jne    1275 <nvmed_check_buffer+0x155>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
mov    0x4(%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    127a <nvmed_check_buffer+0x15a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvmed_check_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     11bf <nvmed_check_buffer+0x9f>
mov    %rdi,%r14
call   1030 <malloc_usable_size@plt>
mov    %rax,%rbx
mov    0x2ea4(%rip),%r15
mov    (%r15),%edi
call   1040 <sysconf@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     11bf <nvmed_check_buffer+0x9f>
mov    (%r15),%edi
call   1040 <sysconf@plt>
mov    %eax,%ecx
mov    %rbx,%rax
shr    $0x20,%rax
je     1173 <nvmed_check_buffer+0x53>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
mov    %rax,%rbx
jmp    117b <nvmed_check_buffer+0x5b>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
mov    %ebx,%eax
shr    $0x9,%eax
xor    %ebp,%ebp
cmp    $0x200,%ebx
cmovb  %ebp,%eax
sub    %eax,%ebx
mov    (%r15),%edi
call   1040 <sysconf@plt>
imul   %ebx,%eax
cmpl   $0x4e564ded,(%r14,%rax,1)
4e
je     11ba <nvmed_check_buffer+0x9a>
add    $0xffffffff,%ebx
mov    (%r15),%edi
call   1040 <sysconf@plt>
imul   %ebx,%eax
cmpl   $0x4e564ded,(%r14,%rax,1)
4e
jne    11bf <nvmed_check_buffer+0x9f>
mov    0x4(%r14,%rax,1),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
define i32 @nvmed_check_buffer(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %71
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i64 @malloc_usable_size(i8* %12)
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = load i32, i32* @_SC_PAGESIZE
%16 = call i32 @sysconf(i32 %15)
%17 = zext i32 %16 to i64
%18 = icmp ult i64 %14, %17
br i1 %18, label %19, label %20
19: ; preds = %11
store i32 0, i32* %2
br label %71
20: ; preds = %11
%21 = load i64, i64* %4
%22 = load i32, i32* @_SC_PAGESIZE
%23 = call i32 @sysconf(i32 %22)
%24 = zext i32 %23 to i64
%25 = udiv i64 %21, %24
%26 = trunc i64 %25 to i32
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp uge i32 %27, 512
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = load i32, i32* %5
%31 = lshr i32 %30, 9
%32 = load i32, i32* %5
%33 = sub i32 %32, %31
store i32 %33, i32* %5
br label %34
34: ; preds = %29, %20
%35 = load i8*, i8** %3
%36 = load i32, i32* @_SC_PAGESIZE
%37 = call i32 @sysconf(i32 %36)
%38 = load i32, i32* %5
%39 = mul i32 %37, %38
%40 = zext i32 %39 to i64
%41 = getelementptr i8, i8* %35, i64 %40
%42 = bitcast i8* %41 to i32*
store i32* %42, i32** %7
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, 1314278893
br i1 %45, label %46, label %50
46: ; preds = %34
%47 = load i32*, i32** %7
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %7
%49 = load i32, i32* %48
store i32 %49, i32* %6
br label %69
50: ; preds = %34
%51 = load i32, i32* %5
%52 = add i32 %51, -1
store i32 %52, i32* %5
%53 = load i8*, i8** %3
%54 = load i32, i32* @_SC_PAGESIZE
%55 = call i32 @sysconf(i32 %54)
%56 = load i32, i32* %5
%57 = mul i32 %55, %56
%58 = zext i32 %57 to i64
%59 = getelementptr i8, i8* %53, i64 %58
%60 = bitcast i8* %59 to i32*
store i32* %60, i32** %7
%61 = load i32*, i32** %7
%62 = load i32, i32* %61
%63 = icmp eq i32 %62, 1314278893
br i1 %63, label %64, label %68
64: ; preds = %50
%65 = load i32*, i32** %7
%66 = getelementptr inbounds i32, i32* %65, i32 1
store i32* %66, i32** %7
%67 = load i32, i32* %66
store i32 %67, i32* %6
br label %68
68: ; preds = %64, %50
br label %69
69: ; preds = %68, %46
%70 = load i32, i32* %6
store i32 %70, i32* %2
br label %71
71: ; preds = %69, %19, %10
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
define i32 @nvmed_check_buffer(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %43, label %3
3: ; preds = %1
%4 = tail call i64 @malloc_usable_size(i8* %0)
%5 = load i32, i32* @_SC_PAGESIZE
%6 = tail call i32 @sysconf(i32 %5)
%7 = zext i32 %6 to i64
%8 = icmp ult i64 %4, %7
br i1 %8, label %43, label %9
9: ; preds = %3
%10 = load i32, i32* @_SC_PAGESIZE
%11 = tail call i32 @sysconf(i32 %10)
%12 = zext i32 %11 to i64
%13 = udiv i64 %4, %12
%14 = trunc i64 %13 to i32
%15 = icmp ugt i32 %14, 511
%16 = lshr i32 %14, 9
%17 = select i1 %15, i32 %16, i32 0
%18 = sub i32 %14, %17
%19 = load i32, i32* @_SC_PAGESIZE
%20 = tail call i32 @sysconf(i32 %19)
%21 = mul i32 %18, %20
%22 = zext i32 %21 to i64
%23 = getelementptr i8, i8* %0, i64 %22
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 1314278893
br i1 %26, label %37, label %27
27: ; preds = %9
%28 = add i32 %18, -1
%29 = load i32, i32* @_SC_PAGESIZE
%30 = tail call i32 @sysconf(i32 %29)
%31 = mul i32 %30, %28
%32 = zext i32 %31 to i64
%33 = getelementptr i8, i8* %0, i64 %32
%34 = bitcast i8* %33 to i32*
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, 1314278893
br i1 %36, label %37, label %43
37: ; preds = %27, %9
%38 = phi i64 [ %22, %9 ], [ %32, %27 ]
%39 = getelementptr i8, i8* %0, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 4
%41 = bitcast i8* %40 to i32*
%42 = load i32, i32* %41
br label %43
43: ; preds = %37, %27, %3, %1
%44 = phi i32 [ 0, %1 ], [ 0, %3 ], [ 0, %27 ], [ %42, %37 ]
ret i32 %44
}