#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 scalar_t__ bp;

/* Variables and functions */
void *coalesce(char *);
char *mem_sbrk(size_t);
void *extend_heap(size_t words) {
  char *bp;
  size_t size;
  size = (words % 2) ? (words + 1) * 4 : words * 4;
  bp = mem_sbrk(size);
  if ((long)bp == -1)
    return NULL;
  int prev_alloc = ((*(unsigned int *)(((char *)(bp)-4))) & 0x2);
  (*(unsigned int *)(((char *)(bp)-4)) = (((size) | (prev_alloc))));
  (*(unsigned int *)(((char *)(bp) +
                      ((*(unsigned int *)(((char *)(bp)-4))) & ~0x7) - 8)) =
       (((size) | (prev_alloc))));
  (*(unsigned int *)((
       (char *)(((char *)(bp) +
                 ((*(unsigned int *)(((char *)(bp)-4))) & ~0x7))) -
       4)) = (((0) | (1))));
  return coalesce(bp);
}


x86 assembly (clang, -O0):
<extend_heap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1153 <extend_heap+0x33>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
shl    $0x2,%rax
mov    %rax,-0x30(%rbp)
jmp    115f <extend_heap+0x3f>
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <mem_sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    118f <extend_heap+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <extend_heap+0xd6>
mov    -0x18(%rbp),%rax
mov    -0x4(%rax),%eax
and    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
or     %rcx,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,-0x4(%rax)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
or     %rcx,%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x4(%rcx),%ecx
and    $0xfffffff8,%ecx
mov    %ecx,%ecx
mov    %edx,-0x8(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x4(%rcx),%ecx
and    $0xfffffff8,%ecx
mov    %ecx,%ecx
movl   $0x1,-0x4(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <coalesce@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extend_heap>:
push   %rbx
lea    0x0(,%rdi,4),%rax
test   $0x1,%dil
lea    0x4(,%rdi,4),%rbx
cmove  %rax,%rbx
mov    %rbx,%rdi
call   1040 <mem_sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     1170 <extend_heap+0x50>
mov    -0x4(%rax),%ecx
and    $0x2,%ecx
or     %ebx,%ecx
mov    %ecx,-0x4(%rax)
and    $0xfffffff8,%ebx
mov    %ecx,-0x8(%rax,%rbx,1)
mov    -0x4(%rax),%ecx
and    $0xfffffff8,%ecx
movl   $0x1,-0x4(%rax,%rcx,1)
mov    %rax,%rdi
pop    %rbx
jmp    1030 <coalesce@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @extend_heap(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = urem i64 %7, 2
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i64, i64* %3
%12 = add i64 %11, 1
%13 = mul i64 %12, 4
br label %17
14: ; preds = %1
%15 = load i64, i64* %3
%16 = mul i64 %15, 4
br label %17
17: ; preds = %14, %10
%18 = phi i64 [ %13, %10 ], [ %16, %14 ]
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = call i8* @mem_sbrk(i64 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = ptrtoint i8* %21 to i64
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %17
store i8* null, i8** %2
br label %66
25: ; preds = %17
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 -4
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = and i32 %29, 2
store i32 %30, i32* %6
%31 = load i64, i64* %5
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = or i64 %31, %33
%35 = trunc i64 %34 to i32
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 -4
%38 = bitcast i8* %37 to i32*
store i32 %35, i32* %38
%39 = load i64, i64* %5
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = or i64 %39, %41
%43 = trunc i64 %42 to i32
%44 = load i8*, i8** %4
%45 = load i8*, i8** %4
%46 = getelementptr inbounds i8, i8* %45, i64 -4
%47 = bitcast i8* %46 to i32*
%48 = load i32, i32* %47
%49 = and i32 %48, -8
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %44, i64 %50
%52 = getelementptr inbounds i8, i8* %51, i64 -8
%53 = bitcast i8* %52 to i32*
store i32 %43, i32* %53
%54 = load i8*, i8** %4
%55 = load i8*, i8** %4
%56 = getelementptr inbounds i8, i8* %55, i64 -4
%57 = bitcast i8* %56 to i32*
%58 = load i32, i32* %57
%59 = and i32 %58, -8
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %54, i64 %60
%62 = getelementptr inbounds i8, i8* %61, i64 -4
%63 = bitcast i8* %62 to i32*
store i32 1, i32* %63
%64 = load i8*, i8** %4
%65 = call i8* @coalesce(i8* %64)
store i8* %65, i8** %2
br label %66
66: ; preds = %25, %24
%67 = load i8*, i8** %2
ret i8* %67
}

LLVM IR (-O2):
define i8* @extend_heap(i64 %0) {
%2 = and i64 %0, 1
%3 = icmp eq i64 %2, 0
%4 = shl i64 %0, 2
%5 = add i64 %4, 4
%6 = select i1 %3, i64 %4, i64 %5
%7 = tail call i8* @mem_sbrk(i64 %6)
%8 = icmp eq i8* %7, inttoptr (i64 -1 to i8*)
br i1 %8, label %27, label %9
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %7, i64 -4
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
%13 = and i32 %12, 2
%14 = trunc i64 %6 to i32
%15 = or i32 %13, %14
store i32 %15, i32* %11
%16 = and i64 %6, 4294967288
%17 = getelementptr inbounds i8, i8* %7, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 -8
%19 = bitcast i8* %18 to i32*
store i32 %15, i32* %19
%20 = load i32, i32* %11
%21 = and i32 %20, -8
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %7, i64 %22
%24 = getelementptr inbounds i8, i8* %23, i64 -4
%25 = bitcast i8* %24 to i32*
store i32 1, i32* %25
%26 = tail call i8* @coalesce(i8* %7)
br label %27
27: ; preds = %1, %9
%28 = phi i8* [ %26, %9 ], [ null, %1 ]
ret i8* %28
}