#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*/ error(char *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
short *myalloc(int size) {
  register short *p, *q;
  p = (short *)malloc(size);
  if (p == 0)
    error("out of memory", NULL);
  for (q = p; size > 0; size -= sizeof(short))
    *q++ = 0;
  return (p);
}


x86 assembly (clang, -O0):
<myalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1152 <myalloc+0x32>
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <error@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1188 <myalloc+0x68>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x2,%rcx
mov    %rcx,-0x18(%rbp)
movw   $0x0,(%rax)
movslq -0x4(%rbp),%rax
sub    $0x2,%rax
mov    %eax,-0x4(%rbp)
jmp    115a <myalloc+0x3a>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myalloc>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1176 <myalloc+0x46>
test   %ebx,%ebx
jle    116b <myalloc+0x3b>
cmp    $0x2,%ebx
mov    $0x2,%eax
cmovl  %ebx,%eax
neg    %eax
lea    (%rbx,%rax,1),%edx
add    $0x1,%edx
and    $0xfffffffe,%edx
add    $0x2,%rdx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe83(%rip),%rdi
xor    %esi,%esi
call   1050 <error@plt>
test   %ebx,%ebx
jg     1147 <myalloc+0x17>
jmp    116b <myalloc+0x3b>

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
define i16* @myalloc(i32 %0) {
%2 = alloca i32
%3 = alloca i16*
%4 = alloca i16*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i16*
store i16* %7, i16** %3
%8 = load i16*, i16** %3
%9 = icmp eq i16* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* null)
br label %12
12: ; preds = %10, %1
%13 = load i16*, i16** %3
store i16* %13, i16** %4
br label %14
14: ; preds = %20, %12
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i16*, i16** %4
%19 = getelementptr inbounds i16, i16* %18, i32 1
store i16* %19, i16** %4
store i16 0, i16* %18
br label %20
20: ; preds = %17
%21 = load i32, i32* %2
%22 = sext i32 %21 to i64
%23 = sub i64 %22, 2
%24 = trunc i64 %23 to i32
store i32 %24, i32* %2
br label %14
25: ; preds = %14
%26 = load i16*, i16** %3
ret i16* %26
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
define i16* @myalloc(i32 %0) {
%2 = tail call i64 @malloc(i32 %0)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* null)
br label %7
7: ; preds = %5, %1
%8 = icmp sgt i32 %0, 0
br i1 %8, label %9, label %16
9: ; preds = %7
%10 = add nuw i32 %0, 1
%11 = call i32 @llvm.smin.i32(i32 %0, i32 2)
%12 = sub nuw i32 %10, %11
%13 = and i32 %12, -2
%14 = zext i32 %13 to i64
%15 = add nuw nsw i64 %14, 2
br label %16
16: ; preds = %9, %7
%17 = inttoptr i64 %2 to i16*
ret i16* %17
}