#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;
int /*<<< orphan*/ munmap(void *, size_t);
size_t sysconf(int /*<<< orphan*/);
void tst_free_canary_left(void *ptr, size_t size __attribute__((unused))) {
  size_t pagesize = sysconf(_SC_PAGESIZE);
  size_t pages = size / pagesize + !!(size % pagesize) + 1;
  void *start = ptr - pagesize;
  munmap(start, pages * pagesize);
}


x86 assembly (clang, -O0):
<tst_free_canary_left>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2eb1(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    -0x30(%rbp),%rax
cmp    $0x0,%rdx
setne  %cl
xor    $0xff,%cl
xor    $0xff,%cl
and    $0x1,%cl
movzbl %cl,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
xor    %ecx,%ecx
sub    -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1030 <munmap@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tst_free_canary_left>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2eb7(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     1151 <tst_free_canary_left+0x31>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    1157 <tst_free_canary_left+0x37>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
cmp    $0x1,%rdx
sbb    $0xffffffffffffffff,%rax
add    $0x1,%rax
sub    %rcx,%r14
imul   %rcx,%rax
mov    %r14,%rdi
mov    %rax,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <munmap@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
define void @tst_free_canary_left(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i32, i32* @_SC_PAGESIZE
%9 = call i64 @sysconf(i32 %8)
store i64 %9, i64* %5
%10 = load i64, i64* %4
%11 = load i64, i64* %5
%12 = udiv i64 %10, %11
%13 = load i64, i64* %4
%14 = load i64, i64* %5
%15 = urem i64 %13, %14
%16 = icmp ne i64 %15, 0
%17 = xor i1 %16, true
%18 = xor i1 %17, true
%19 = zext i1 %18 to i32
%20 = sext i32 %19 to i64
%21 = add i64 %12, %20
%22 = add i64 %21, 1
store i64 %22, i64* %6
%23 = load i8*, i8** %3
%24 = load i64, i64* %5
%25 = sub i64 0, %24
%26 = getelementptr i8, i8* %23, i64 %25
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = load i64, i64* %6
%29 = load i64, i64* %5
%30 = mul i64 %28, %29
%31 = call i32 @munmap(i8* %27, i64 %30)
ret void
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
define void @tst_free_canary_left(i8* %0, i64 %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i64 @sysconf(i32 %3)
%5 = udiv i64 %1, %4
%6 = urem i64 %1, %4
%7 = icmp ne i64 %6, 0
%8 = zext i1 %7 to i64
%9 = add i64 %5, 1
%10 = add i64 %9, %8
%11 = sub i64 0, %4
%12 = getelementptr i8, i8* %0, i64 %11
%13 = mul i64 %10, %4
%14 = tail call i32 @munmap(i8* %12, i64 %13)
ret void
}