#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 MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
scalar_t__ mprotect(void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int /*<<< orphan*/ perror(char *);
size_t sysconf(int /*<<< orphan*/);
void *tst_malloc_canary_left(size_t size) {
  size_t pagesize = sysconf(_SC_PAGESIZE);
  size_t pages = size / pagesize + !!(size % pagesize) + 1;
  void *ret;
  if (size == 0)
    return NULL;
  ret = mmap(NULL, pages * pagesize, PROT_READ | PROT_WRITE,
             MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (ret == MAP_FAILED)
    return NULL;
  if (mprotect(ret, pagesize, PROT_NONE)) {
    perror("mprotect");
    munmap(ret, pages * pagesize);
    return NULL;
  }
  return ret + pagesize;
}


x86 assembly (clang, -O0):
<tst_malloc_canary_left>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1070 <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)
cmpq   $0x0,-0x10(%rbp)
jne    11c7 <tst_malloc_canary_left+0x77>
movq   $0x0,-0x8(%rbp)
jmp    1282 <tst_malloc_canary_left+0x132>
mov    -0x20(%rbp),%rsi
imul   -0x18(%rbp),%rsi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc6(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%rax
jne    122b <tst_malloc_canary_left+0xdb>
movq   $0x0,-0x8(%rbp)
jmp    1282 <tst_malloc_canary_left+0x132>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
call   1050 <mprotect@plt>
cmp    $0x0,%rax
je     1276 <tst_malloc_canary_left+0x126>
lea    0xdae(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1040 <munmap@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <tst_malloc_canary_left+0x132>
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tst_malloc_canary_left>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %rbx,%rbx
je     118a <tst_malloc_canary_left+0x3a>
mov    %rax,%r14
mov    %rbx,%rax
or     %r14,%rax
shr    $0x20,%rax
je     1192 <tst_malloc_canary_left+0x42>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
mov    %rax,%rbx
jmp    119b <tst_malloc_canary_left+0x4b>
xor    %r12d,%r12d
jmp    1213 <tst_malloc_canary_left+0xc3>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebx
cmp    $0x1,%rdx
sbb    $0xffffffffffffffff,%rbx
add    $0x1,%rbx
imul   %r14,%rbx
mov    0x2e46(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0c(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    0x2dc1(%rip),%rax
cmp    (%rax),%r15
je     1213 <tst_malloc_canary_left+0xc3>
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <mprotect@plt>
test   %rax,%rax
jne    1222 <tst_malloc_canary_left+0xd2>
add    %r14,%r15
mov    %r15,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd7(%rip),%rdi
call   1060 <perror@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <munmap@plt>
xor    %r12d,%r12d
jmp    1213 <tst_malloc_canary_left+0xc3>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
@.str = [9 x i8] c"mprotect\00"
define i8* @tst_malloc_canary_left(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %3
%7 = load i32, i32* @_SC_PAGESIZE
%8 = call i64 @sysconf(i32 %7)
store i64 %8, i64* %4
%9 = load i64, i64* %3
%10 = load i64, i64* %4
%11 = udiv i64 %9, %10
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = urem i64 %12, %13
%15 = icmp ne i64 %14, 0
%16 = xor i1 %15, true
%17 = xor i1 %16, true
%18 = zext i1 %17 to i32
%19 = sext i32 %18 to i64
%20 = add i64 %11, %19
%21 = add i64 %20, 1
store i64 %21, i64* %5
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %1
store i8* null, i8** %2
br label %57
25: ; preds = %1
%26 = load i64, i64* %5
%27 = load i64, i64* %4
%28 = mul i64 %26, %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_ANONYMOUS
%33 = load i32, i32* @MAP_PRIVATE
%34 = or i32 %32, %33
%35 = call i8* @mmap(i32* null, i64 %28, i32 %31, i32 %34, i32 -1, i32 0)
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %40
39: ; preds = %25
store i8* null, i8** %2
br label %57
40: ; preds = %25
%41 = load i8*, i8** %6
%42 = load i64, i64* %4
%43 = load i32, i32* @PROT_NONE
%44 = call i64 @mprotect(i8* %41, i64 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%48 = load i8*, i8** %6
%49 = load i64, i64* %5
%50 = load i64, i64* %4
%51 = mul i64 %49, %50
%52 = call i32 @munmap(i8* %48, i64 %51)
store i8* null, i8** %2
br label %57
53: ; preds = %40
%54 = load i8*, i8** %6
%55 = load i64, i64* %4
%56 = getelementptr i8, i8* %54, i64 %55
store i8* %56, i8** %2
br label %57
57: ; preds = %53, %46, %39, %24
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
@.str = [9 x i8] c"mprotect\00"
define i8* @tst_malloc_canary_left(i64 %0) {
%2 = load i32, i32* @_SC_PAGESIZE
%3 = tail call i64 @sysconf(i32 %2)
%4 = icmp eq i64 %0, 0
br i1 %4, label %31, label %5
5: ; preds = %1
%6 = udiv i64 %0, %3
%7 = urem i64 %0, %3
%8 = icmp ne i64 %7, 0
%9 = zext i1 %8 to i64
%10 = add i64 %6, 1
%11 = add i64 %10, %9
%12 = mul i64 %11, %3
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = load i32, i32* @MAP_PRIVATE
%18 = or i32 %17, %16
%19 = tail call i8* @mmap(i32* null, i64 %12, i32 %15, i32 %18, i32 -1, i32 0)
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %31, label %22
22: ; preds = %5
%23 = load i32, i32* @PROT_NONE
%24 = tail call i64 @mprotect(i8* %19, i64 %3, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @munmap(i8* %19, i64 %12)
br label %31
29: ; preds = %22
%30 = getelementptr i8, i8* %19, i64 %3
br label %31
31: ; preds = %5, %1, %29, %26
%32 = phi i8* [ null, %26 ], [ %30, %29 ], [ null, %1 ], [ null, %5 ]
ret i8* %32
}