#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_exit(char *);
int /*<<< orphan*/ free(void *);
void *malloc(int);
void *pkt_alloc_buffer(void *buffer, int *sizep, int size) {
  if ((buffer == NULL) || (*sizep < size)) {
    if (buffer)
      free(buffer);
    buffer = malloc(size);
    if (buffer == NULL)
      error_exit("Out of memory.\n");
    *sizep = size;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<pkt_alloc_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115d <pkt_alloc_buffer+0x2d>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jge    119d <pkt_alloc_buffer+0x6d>
cmpq   $0x0,-0x8(%rbp)
je     1171 <pkt_alloc_buffer+0x41>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1194 <pkt_alloc_buffer+0x64>
lea    0xe71(%rip),%rdi
call   1040 <error_exit@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkt_alloc_buffer>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
test   %rdi,%rdi
je     114e <pkt_alloc_buffer+0x1e>
mov    %rdi,%rbx
cmp    %ebp,(%r14)
jge    116c <pkt_alloc_buffer+0x3c>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1169 <pkt_alloc_buffer+0x39>
lea    0xe9c(%rip),%rdi
call   1040 <error_exit@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Out of memory.\0A\00"
define i8* @pkt_alloc_buffer(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = load i32*, i32** %5
%11 = load i32, i32* %10
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %30
14: ; preds = %9, %3
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %6
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32, i32* %6
%29 = load i32*, i32** %5
store i32 %28, i32* %29
br label %30
30: ; preds = %27, %9
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
@.str = [16 x i8] c"Out of memory.\0A\00"
define i8* @pkt_alloc_buffer(i8* %0, i32* nocapture %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %10, label %5
5: ; preds = %3
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, %2
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = tail call i32 @free(i8* %0)
br label %10
10: ; preds = %3, %8
%11 = tail call i8* @malloc(i32 %2)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
store i32 %2, i32* %1
br label %16
16: ; preds = %15, %5
%17 = phi i8* [ %11, %15 ], [ %0, %5 ]
ret i8* %17
}