#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)-4))) >> 1) & 0x1) << 1))));
  (*(unsigned int *)(((char *)(bp) +
                      ((*(unsigned int *)(((char *)(bp)-4))) & ~0x7) - 8)) =
       (((size) | (0) |
         ((((*(unsigned int *)(((char *)(bp)-4))) >> 1) & 0x1) << 1))));
  (*(unsigned int *)((
       (char *)(((char *)(bp) +
                 ((*(unsigned int *)(((char *)(bp)-4))) & ~0x7))) -
       4)) = (((0) | (1) | (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    1209 <extend_heap+0xe9>
mov    -0x20(%rbp),%rax
or     $0x0,%rax
mov    -0x18(%rbp),%rcx
mov    -0x4(%rcx),%ecx
shr    $0x1,%ecx
and    $0x1,%ecx
shl    $0x1,%ecx
mov    %ecx,%ecx
or     %rcx,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,-0x4(%rax)
mov    -0x20(%rbp),%rax
or     $0x0,%rax
mov    -0x18(%rbp),%rcx
mov    -0x4(%rcx),%ecx
shr    $0x1,%ecx
and    $0x1,%ecx
shl    $0x1,%ecx
mov    %ecx,%ecx
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
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 %73
23: ; preds = %16
%24 = load i64, i64* %5
%25 = or i64 %24, 0
%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 = lshr i32 %29, 1
%31 = and i32 %30, 1
%32 = shl i32 %31, 1
%33 = zext i32 %32 to i64
%34 = or i64 %25, %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 = or i64 %39, 0
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 -4
%43 = bitcast i8* %42 to i32*
%44 = load i32, i32* %43
%45 = lshr i32 %44, 1
%46 = and i32 %45, 1
%47 = shl i32 %46, 1
%48 = zext i32 %47 to i64
%49 = or i64 %40, %48
%50 = trunc i64 %49 to i32
%51 = load i8*, i8** %4
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 -4
%54 = bitcast i8* %53 to i32*
%55 = load i32, i32* %54
%56 = and i32 %55, -8
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %51, i64 %57
%59 = getelementptr inbounds i8, i8* %58, i64 -8
%60 = bitcast i8* %59 to i32*
store i32 %50, i32* %60
%61 = load i8*, i8** %4
%62 = load i8*, i8** %4
%63 = getelementptr inbounds i8, i8* %62, i64 -4
%64 = bitcast i8* %63 to i32*
%65 = load i32, i32* %64
%66 = and i32 %65, -8
%67 = zext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %61, i64 %67
%69 = getelementptr inbounds i8, i8* %68, i64 -4
%70 = bitcast i8* %69 to i32*
store i32 1, i32* %70
%71 = load i8*, i8** %4
%72 = call i8* @coalesce(i8* %71)
store i8* %72, i8** %2
br label %73
73: ; preds = %23, %22
%74 = load i8*, i8** %2
ret i8* %74
}

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
}