#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*/ printf(char *, unsigned int);
void *sbrk(size_t);
void *myMalloc(size_t size) {
  void *p = sbrk(0);
  printf("%x\n", (unsigned int)p);
  if (sbrk(size) == (void *)-1)
    return NULL;
  printf("%x\n", (unsigned int)sbrk(0));
  return p;
}


x86 assembly (clang, -O0):
<myMalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <sbrk@plt>
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1171 <myMalloc+0x51>
movq   $0x0,-0x8(%rbp)
jmp    1190 <myMalloc+0x70>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <sbrk@plt>
mov    %eax,%esi
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myMalloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %rax,%r14
lea    0xec5(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
mov    %r15,%rdi
call   1040 <sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     1169 <myMalloc+0x49>
xor    %edi,%edi
call   1040 <sbrk@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %r14,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%x\0A\00"
define i8* @myMalloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i64 %0, i64* %3
%5 = call i8* @sbrk(i64 0)
store i8* %5, i8** %4
%6 = load i8*, i8** %4
%7 = ptrtoint i8* %6 to i32
%8 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i64, i64* %3
%10 = call i8* @sbrk(i64 %9)
%11 = icmp eq i8* %10, inttoptr (i64 -1 to i8*)
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %18
13: ; preds = %1
%14 = call i8* @sbrk(i64 0)
%15 = ptrtoint i8* %14 to i32
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i8*, i8** %4
store i8* %17, i8** %2
br label %18
18: ; preds = %13, %12
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
@.str = [4 x i8] c"%x\0A\00"
define i8* @myMalloc(i64 %0) {
%2 = tail call i8* @sbrk(i64 0)
%3 = ptrtoint i8* %2 to i64
%4 = trunc i64 %3 to i32
%5 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i8* @sbrk(i64 %0)
%7 = icmp eq i8* %6, inttoptr (i64 -1 to i8*)
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = tail call i8* @sbrk(i64 0)
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %1, %8
%14 = phi i8* [ %2, %8 ], [ null, %1 ]
ret i8* %14
}