#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*/ assert(int);
int /*<<< orphan*/ free(char *);
void *malloc(int);
char **alloc_buffer(int size) {
  assert(size > 0);
  char **buffer = malloc(size * sizeof(char *));
  if (buffer == NULL)
    return NULL;
  for (int i = 0; i < size; i++) {
    buffer[i] = malloc((1024 + 1) * sizeof(char));
    if (buffer[i] == NULL) {
      for (int j = 0; j < i; j++)
        free(buffer[j]);
      return buffer;
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<alloc_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <alloc_buffer+0x47>
movq   $0x0,-0x8(%rbp)
jmp    1210 <alloc_buffer+0xe0>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1208 <alloc_buffer+0xd8>
mov    $0x401,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11f5 <alloc_buffer+0xc5>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e8 <alloc_buffer+0xb8>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11bd <alloc_buffer+0x8d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <alloc_buffer+0xe0>
jmp    11fa <alloc_buffer+0xca>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117e <alloc_buffer+0x4e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_buffer>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %edi,%edi
test   %r14d,%r14d
setg   %dil
call   1040 <assert@plt>
lea    0x0(,%r14,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    11b2 <alloc_buffer+0x82>
test   %r15,%r15
je     11b2 <alloc_buffer+0x82>
mov    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x401,%edi
call   1050 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
test   %rax,%rax
je     118e <alloc_buffer+0x5e>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1170 <alloc_buffer+0x40>
jmp    11b2 <alloc_buffer+0x82>
test   %ebx,%ebx
je     11b2 <alloc_buffer+0x82>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%r14,8),%rdi
call   1030 <free@plt>
add    $0x1,%r14
cmp    %r14,%rbx
jne    11a0 <alloc_buffer+0x70>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8** @alloc_buffer(i32 %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 8
%14 = trunc i64 %13 to i32
%15 = call i8* @malloc(i32 %14)
%16 = bitcast i8* %15 to i8**
store i8** %16, i8*** %4
%17 = load i8**, i8*** %4
%18 = icmp eq i8** %17, null
br i1 %18, label %19, label %20
19: ; preds = %1
store i8** null, i8*** %2
br label %60
20: ; preds = %1
store i32 0, i32* %5
br label %21
21: ; preds = %55, %20
%22 = load i32, i32* %5
%23 = load i32, i32* %3
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %58
25: ; preds = %21
%26 = call i8* @malloc(i32 1025)
%27 = load i8**, i8*** %4
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
store i8* %26, i8** %30
%31 = load i8**, i8*** %4
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %54
37: ; preds = %25
store i32 0, i32* %6
br label %38
38: ; preds = %49, %37
%39 = load i32, i32* %6
%40 = load i32, i32* %5
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %52
42: ; preds = %38
%43 = load i8**, i8*** %4
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8*, i8** %43, i64 %45
%47 = load i8*, i8** %46
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %42
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 1
store i32 %51, i32* %6
br label %38
52: ; preds = %38
%53 = load i8**, i8*** %4
store i8** %53, i8*** %2
br label %60
54: ; preds = %25
br label %55
55: ; preds = %54
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %21
58: ; preds = %21
%59 = load i8**, i8*** %4
store i8** %59, i8*** %2
br label %60
60: ; preds = %58, %52, %19
%61 = load i8**, i8*** %2
ret i8** %61
}

LLVM IR (-O2):
define noalias i8** @alloc_buffer(i32 %0) {
%2 = icmp sgt i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = shl i32 %0, 3
%6 = tail call i8* @malloc(i32 %5)
%7 = bitcast i8* %6 to i8**
%8 = icmp ne i8* %6, null
%9 = and i1 %2, %8
br i1 %9, label %10, label %32
10: ; preds = %1
%11 = zext i32 %0 to i64
br label %12
12: ; preds = %10, %29
%13 = phi i64 [ 0, %10 ], [ %30, %29 ]
%14 = tail call dereferenceable_or_null(1025) i8* @malloc(i32 1025)
%15 = getelementptr inbounds i8*, i8** %7, i64 %13
store i8* %14, i8** %15
%16 = icmp eq i8* %14, null
br i1 %16, label %17, label %29
17: ; preds = %12
%18 = trunc i64 %13 to i32
%19 = icmp eq i32 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %17
%21 = and i64 %13, 4294967295
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ 0, %20 ], [ %27, %22 ]
%24 = getelementptr inbounds i8*, i8** %7, i64 %23
%25 = load i8*, i8** %24
%26 = tail call i32 @free(i8* %25)
%27 = add nuw nsw i64 %23, 1
%28 = icmp eq i64 %27, %21
br i1 %28, label %32, label %22
29: ; preds = %12
%30 = add nuw nsw i64 %13, 1
%31 = icmp eq i64 %30, %11
br i1 %31, label %32, label %12
32: ; preds = %29, %22, %1, %17
ret i8** %7
}