#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*/ free_buffers(char **, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char **, int /*<<< orphan*/, int);
char **allocate_buffers(int num_bufs, size_t size) {
  char **buffers = NULL;
  if ((buffers = (char **)malloc(sizeof(char *) * num_bufs)) == NULL) {
    goto fail;
  }
  memset(buffers, 0, sizeof(char *) * num_bufs);
  int index;
  for (index = 0; index < num_bufs; ++index) {
    if ((buffers[index] = (char *)malloc(sizeof(char) * size)) == NULL) {
      goto fail;
    }
  }
  return buffers;
fail:
  free_buffers(buffers, num_bufs);
  return NULL;
}


x86 assembly (clang, -O0):
<allocate_buffers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1169 <allocate_buffers+0x39>
jmp    11db <allocate_buffers+0xab>
mov    -0x20(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ce <allocate_buffers+0x9e>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    11bb <allocate_buffers+0x8b>
jmp    11db <allocate_buffers+0xab>
jmp    11c0 <allocate_buffers+0x90>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1185 <allocate_buffers+0x55>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ef <allocate_buffers+0xbf>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <free_buffers@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_buffers>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%r14d
lea    0x0(,%r14,8),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118c <allocate_buffers+0x5c>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
test   %r14d,%r14d
jle    1199 <allocate_buffers+0x69>
mov    %r14d,%r12d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,(%rbx,%rbp,8)
test   %rax,%rax
je     118c <allocate_buffers+0x5c>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1170 <allocate_buffers+0x40>
jmp    1199 <allocate_buffers+0x69>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <free_buffers@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8** @allocate_buffers(i32 %0, i64 %1) {
%3 = shl i32 %0, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8**
%6 = icmp eq i64 %4, 0
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = tail call i32 @memset(i8** %5, i32 0, i32 %3)
%9 = icmp sgt i32 %0, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = trunc i64 %1 to i32
%12 = zext i32 %0 to i64
br label %15
13: ; preds = %15
%14 = icmp eq i64 %21, %12
br i1 %14, label %24, label %15
15: ; preds = %10, %13
%16 = phi i64 [ 0, %10 ], [ %21, %13 ]
%17 = tail call i64 @malloc(i32 %11)
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds i8*, i8** %5, i64 %16
store i8* %18, i8** %19
%20 = icmp eq i64 %17, 0
%21 = add nuw nsw i64 %16, 1
br i1 %20, label %22, label %13
22: ; preds = %15, %2
%23 = tail call i32 @free_buffers(i8** %5, i32 %0)
br label %24
24: ; preds = %13, %7, %22
%25 = phi i8** [ null, %22 ], [ %5, %7 ], [ %5, %13 ]
ret i8** %25
}