#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 */
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
char *alloc(int buflen) {
  char *p, *q;
  int i;
  printf("Allocate %d bytes - ", buflen);
  p = malloc((size_t)buflen);
  if (p == NULL) {
    printf("Error: malloc() returns NULL!\n");
    return NULL;
  }
  for (q = p, i = 0; i < buflen; i++)
    *(q++) = '@';
  printf("OK!\n");
  return p;
}


x86 assembly (clang, -O0):
<alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0xc(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116f <alloc+0x4f>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <alloc+0xa0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11aa <alloc+0x8a>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x40,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117e <alloc+0x5e>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
lea    0xeb3(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movslq %ebp,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1182 <alloc+0x42>
lea    0xea8(%rip),%r14
test   %ebp,%ebp
jle    1189 <alloc+0x49>
mov    %ebp,%edx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1050 <memset@plt>
jmp    1189 <alloc+0x49>
lea    0xe90(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Allocate %d bytes - \00"
@.str.1 = [31 x i8] c"Error: malloc() returns NULL!\0A\00"
@.str.2 = [5 x i8] c"OK!\0A\00"
define i8* @alloc(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %31
16: ; preds = %1
%17 = load i8*, i8** %4
store i8* %17, i8** %5
store i32 0, i32* %6
br label %18
18: ; preds = %25, %16
%19 = load i32, i32* %6
%20 = load i32, i32* %3
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
store i8 64, i8* %23
br label %25
25: ; preds = %22
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %18
28: ; preds = %18
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %28, %14
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@.str = [21 x i8] c"Allocate %d bytes - \00"
@str = [4 x i8] c"OK!\00"
@str.3 = [30 x i8] c"Error: malloc() returns NULL!\00"
define noalias i8* @alloc(i32 %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = sext i32 %0 to i64
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %6, %8, %1
%11 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @str.3, i64 0, i64 0), %1 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0), %6 ]
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
ret i8* %4
}