#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 */
unsigned int BUFSIZ;
int /*<<< orphan*/ cpm_free(char *);
char *cpm_malloc(unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int);
char *grabmem(unsigned int *sizep) {
  static char *p, *q;
  static unsigned size;
  q = cpm_malloc(BUFSIZ + 10);
  size = 16384 + 10;
  while ((p = cpm_malloc(size)) == (char *)0) {
    size -= 1024;
    printf(" %d ", size);
    if ((size - 10) < 2048) {
      size = 0;
      break;
    }
  }
  *sizep = size - 10;
  cpm_free(q);
  return p;
}


x86 assembly (clang, -O0):
<grabmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
add    $0xa,%edi
call   1040 <cpm_malloc@plt>
mov    %rax,0x2ef4(%rip)
movl   $0x400a,0x2ef2(%rip)
mov    0x2eec(%rip),%edi
call   1040 <cpm_malloc@plt>
mov    %rax,0x2ed0(%rip)
cmp    $0x0,%rax
jne    11c5 <grabmem+0x95>
mov    0x2ed0(%rip),%eax
sub    $0x400,%eax
mov    %eax,0x2ec5(%rip)
mov    0x2ebf(%rip),%esi
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
mov    0x2ead(%rip),%eax
sub    $0xa,%eax
cmp    $0x800,%eax
jae    11c0 <grabmem+0x90>
movl   $0x0,0x2e95(%rip)
jmp    11c5 <grabmem+0x95>
jmp    115e <grabmem+0x2e>
mov    0x2e85(%rip),%ecx
sub    $0xa,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e6d(%rip),%rdi
call   1050 <cpm_free@plt>
mov    0x2e59(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grabmem>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea2(%rip),%rax
mov    (%rax),%edi
add    $0xa,%edi
call   1040 <cpm_malloc@plt>
mov    %rax,0x2ef9(%rip)
movl   $0x400a,0x2ef7(%rip)
mov    $0x400a,%edi
lea    0xe9b(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
call   1040 <cpm_malloc@plt>
mov    %rax,0x2ec4(%rip)
mov    0x2ece(%rip),%esi
test   %rax,%rax
jne    11bc <grabmem+0x8c>
add    $0xfffffc00,%esi
mov    %esi,0x2ebd(%rip)
mov    %rbx,%rdi
call   1030 <printf@plt>
mov    0x2eaf(%rip),%edi
lea    -0xa(%rdi),%eax
cmp    $0x7ff,%eax
ja     1170 <grabmem+0x40>
movl   $0x0,0x2e9b(%rip)
mov    $0xfffffff6,%esi
jmp    11bf <grabmem+0x8f>
add    $0xfffffff6,%esi
mov    %esi,(%r14)
mov    0x2e7f(%rip),%rdi
call   1050 <cpm_free@plt>
mov    0x2e6b(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@grabmem.p = internal global i8* null
@grabmem.q = internal global i8* null
@grabmem.size = internal global i32 0
@BUFSIZ = global i32 0
@.str = [5 x i8] c" %d \00"
define i8* @grabmem(i32* %0) {
%2 = alloca i32*
store i32* %0, i32** %2
%3 = load i32, i32* @BUFSIZ
%4 = add i32 %3, 10
%5 = call i8* @cpm_malloc(i32 %4)
store i8* %5, i8** @grabmem.q
store i32 16394, i32* @grabmem.size
br label %6
6: ; preds = %19, %1
%7 = load i32, i32* @grabmem.size
%8 = call i8* @cpm_malloc(i32 %7)
store i8* %8, i8** @grabmem.p
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %20
10: ; preds = %6
%11 = load i32, i32* @grabmem.size
%12 = sub i32 %11, 1024
store i32 %12, i32* @grabmem.size
%13 = load i32, i32* @grabmem.size
%14 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @grabmem.size
%16 = sub i32 %15, 10
%17 = icmp ult i32 %16, 2048
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 0, i32* @grabmem.size
br label %20
19: ; preds = %10
br label %6
20: ; preds = %18, %6
%21 = load i32, i32* @grabmem.size
%22 = sub i32 %21, 10
%23 = load i32*, i32** %2
store i32 %22, i32* %23
%24 = load i8*, i8** @grabmem.q
%25 = call i32 @cpm_free(i8* %24)
%26 = load i8*, i8** @grabmem.p
ret i8* %26
}

LLVM IR (-O2):
@grabmem.p = internal unnamed_addr global i8* null
@grabmem.q = internal unnamed_addr global i8* null
@grabmem.size = internal unnamed_addr global i32 0
@BUFSIZ = global i32 0
@.str = [5 x i8] c" %d \00"
define i8* @grabmem(i32* nocapture writeonly %0) {
%2 = load i32, i32* @BUFSIZ
%3 = add i32 %2, 10
%4 = tail call i8* @cpm_malloc(i32 %3)
store i8* %4, i8** @grabmem.q
store i32 16394, i32* @grabmem.size
br label %5
5: ; preds = %10, %1
%6 = phi i32 [ %13, %10 ], [ 16394, %1 ]
%7 = tail call i8* @cpm_malloc(i32 %6)
store i8* %7, i8** @grabmem.p
%8 = icmp eq i8* %7, null
%9 = load i32, i32* @grabmem.size
br i1 %8, label %10, label %17
10: ; preds = %5
%11 = add i32 %9, -1024
store i32 %11, i32* @grabmem.size
%12 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @grabmem.size
%14 = add i32 %13, -10
%15 = icmp ult i32 %14, 2048
br i1 %15, label %16, label %5
16: ; preds = %10
store i32 0, i32* @grabmem.size
br label %19
17: ; preds = %5
%18 = add i32 %9, -10
br label %19
19: ; preds = %17, %16
%20 = phi i32 [ %18, %17 ], [ -10, %16 ]
store i32 %20, i32* %0
%21 = load i8*, i8** @grabmem.q
%22 = tail call i32 @cpm_free(i8* %21)
%23 = load i8*, i8** @grabmem.p
ret i8* %23
}