#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 */
typedef unsigned int uint64_t;
typedef unsigned int uint32_t;

/* Variables and functions */
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ sys_exit_group(int);
scalar_t__ sys_mprotect(void *, unsigned int, int);
unsigned int grow_memory(void *stack_limit, void *memory_addr, void *text,
                         unsigned int grow_pages) {
  void *stack = (void *)stack_limit - 12288;
  unsigned int grow_limit = *(uint64_t *)(text - 5 * 8);
  unsigned int old_pages = *(uint32_t *)stack;
  uint64_t new_pages = (uint64_t)old_pages + (uint64_t)grow_pages;
  if (new_pages > grow_limit)
    return -1;
  if (sys_mprotect(memory_addr + (old_pages << 16), grow_pages << 16,
                   PROT_READ | PROT_WRITE) != 0)
    sys_exit_group(1);
  *(uint32_t *)stack = new_pages;
  return old_pages;
}


x86 assembly (clang, -O0):
<grow_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffd000,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jbe    1179 <grow_memory+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <grow_memory+0xa8>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%eax
shl    $0x10,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
shl    $0x10,%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
call   1030 <sys_mprotect@plt>
cmp    $0x0,%rax
je     11b9 <grow_memory+0x99>
mov    $0x1,%edi
call   1040 <sys_exit_group@plt>
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grow_memory>:
push   %rbp
push   %r14
push   %rbx
mov    -0x3000(%rdi),%ebx
lea    (%rbx,%rcx,1),%ebp
mov    $0xffffffff,%eax
cmp    -0x28(%rdx),%ebp
ja     1179 <grow_memory+0x59>
mov    %rdi,%r14
mov    %ebx,%eax
shl    $0x10,%eax
add    %rax,%rsi
shl    $0x10,%ecx
mov    0x2eac(%rip),%rax
mov    0x2e85(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    %rsi,%rdi
mov    %ecx,%esi
call   1030 <sys_mprotect@plt>
test   %rax,%rax
je     1170 <grow_memory+0x50>
mov    $0x1,%edi
call   1040 <sys_exit_group@plt>
mov    %ebp,-0x3000(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @grow_memory(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i8*, i8** %6
%15 = getelementptr i8, i8* %14, i64 -12288
store i8* %15, i8** %10
%16 = load i8*, i8** %8
%17 = getelementptr i8, i8* %16, i64 -40
%18 = bitcast i8* %17 to i32*
%19 = load i32, i32* %18
store i32 %19, i32* %11
%20 = load i8*, i8** %10
%21 = bitcast i8* %20 to i32*
%22 = load i32, i32* %21
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = load i32, i32* %9
%25 = add i32 %23, %24
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = load i32, i32* %11
%28 = icmp ugt i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %4
store i32 -1, i32* %5
br label %50
30: ; preds = %4
%31 = load i8*, i8** %7
%32 = load i32, i32* %12
%33 = shl i32 %32, 16
%34 = zext i32 %33 to i64
%35 = getelementptr i8, i8* %31, i64 %34
%36 = load i32, i32* %9
%37 = shl i32 %36, 16
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %38, %39
%41 = call i64 @sys_mprotect(i8* %35, i32 %37, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %30
%44 = call i32 @sys_exit_group(i32 1)
br label %45
45: ; preds = %43, %30
%46 = load i32, i32* %13
%47 = load i8*, i8** %10
%48 = bitcast i8* %47 to i32*
store i32 %46, i32* %48
%49 = load i32, i32* %12
store i32 %49, i32* %5
br label %50
50: ; preds = %45, %29
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @grow_memory(i8* nocapture %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = getelementptr i8, i8* %0, i64 -12288
%6 = getelementptr i8, i8* %2, i64 -40
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
%9 = bitcast i8* %5 to i32*
%10 = load i32, i32* %9
%11 = add i32 %10, %3
%12 = icmp ugt i32 %11, %8
br i1 %12, label %26, label %13
13: ; preds = %4
%14 = shl i32 %10, 16
%15 = zext i32 %14 to i64
%16 = getelementptr i8, i8* %1, i64 %15
%17 = shl i32 %3, 16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = tail call i64 @sys_mprotect(i8* %16, i32 %17, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %13
%24 = tail call i32 @sys_exit_group(i32 1)
br label %25
25: ; preds = %23, %13
store i32 %11, i32* %9
br label %26
26: ; preds = %4, %25
%27 = phi i32 [ %10, %25 ], [ -1, %4 ]
ret i32 %27
}