#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;
  if ((long)(bp = mem_sbrk(size)) == -1)
    return NULL;
  (*(unsigned int *)(((char *)(bp)-4)) = (((size) | (0))));
  (*(unsigned int *)(((char *)(bp) +
                      ((*(unsigned int *)(((char *)(bp)-4))) & ~0x7) - 8)) =
       (((size) | (0))));
  (*(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,-0x28(%rbp)
jmp    115f <extend_heap+0x3f>
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <mem_sbrk@plt>
mov    %rax,-0x18(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    118b <extend_heap+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    11df <extend_heap+0xbf>
mov    -0x20(%rbp),%rax
or     $0x0,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,-0x4(%rax)
mov    -0x20(%rbp),%rax
or     $0x0,%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     116a <extend_heap+0x4a>
mov    %ebx,-0x4(%rax)
mov    %ebx,%ecx
and    $0xfffffff8,%ecx
mov    %ebx,-0x8(%rax,%rcx,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
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = urem i64 %6, 2
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i64, i64* %3
%11 = add i64 %10, 1
%12 = mul i64 %11, 4
br label %16
13: ; preds = %1
%14 = load i64, i64* %3
%15 = mul i64 %14, 4
br label %16
16: ; preds = %13, %9
%17 = phi i64 [ %12, %9 ], [ %15, %13 ]
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = call i8* @mem_sbrk(i64 %18)
store i8* %19, i8** %4
%20 = ptrtoint i8* %19 to i64
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %16
store i8* null, i8** %2
br label %55
23: ; preds = %16
%24 = load i64, i64* %5
%25 = or i64 %24, 0
%26 = trunc i64 %25 to i32
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 -4
%29 = bitcast i8* %28 to i32*
store i32 %26, i32* %29
%30 = load i64, i64* %5
%31 = or i64 %30, 0
%32 = trunc i64 %31 to i32
%33 = load i8*, i8** %4
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 -4
%36 = bitcast i8* %35 to i32*
%37 = load i32, i32* %36
%38 = and i32 %37, -8
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %33, i64 %39
%41 = getelementptr inbounds i8, i8* %40, i64 -8
%42 = bitcast i8* %41 to i32*
store i32 %32, i32* %42
%43 = load i8*, i8** %4
%44 = load i8*, i8** %4
%45 = getelementptr inbounds i8, i8* %44, i64 -4
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
%48 = and i32 %47, -8
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %43, i64 %49
%51 = getelementptr inbounds i8, i8* %50, i64 -4
%52 = bitcast i8* %51 to i32*
store i32 1, i32* %52
%53 = load i8*, i8** %4
%54 = call i8* @coalesce(i8* %53)
store i8* %54, i8** %2
br label %55
55: ; preds = %23, %22
%56 = load i8*, i8** %2
ret i8* %56
}

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 %24, label %9
9: ; preds = %1
%10 = trunc i64 %6 to i32
%11 = getelementptr inbounds i8, i8* %7, i64 -4
%12 = bitcast i8* %11 to i32*
store i32 %10, i32* %12
%13 = and i64 %6, 4294967288
%14 = getelementptr inbounds i8, i8* %7, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -8
%16 = bitcast i8* %15 to i32*
store i32 %10, i32* %16
%17 = load i32, i32* %12
%18 = and i32 %17, -8
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %7, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -4
%22 = bitcast i8* %21 to i32*
store i32 1, i32* %22
%23 = tail call i8* @coalesce(i8* %7)
br label %24
24: ; preds = %1, %9
%25 = phi i8* [ %23, %9 ], [ null, %1 ]
ret i8* %25
}