#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*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int posix_memalign(void **, size_t, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int);
void *mem_alloc(size_t alignment, size_t size, char *str) {
  void *buffer = NULL;
  int ret;
  ret = posix_memalign((void **)&buffer, alignment, size);
  if (buffer == NULL) {
    fprintf(stderr, "posix_memalign failed for %s with error %s\n", str,
            strerror(ret));
    return NULL;
  }
  memset(buffer, 0, size);
  return buffer;
}


x86 assembly (clang, -O0):
<mem_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   1050 <posix_memalign@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <mem_alloc+0x7a>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d1 <mem_alloc+0x91>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_alloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rsi
movq   $0x0,(%rsp)
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1050 <posix_memalign@plt>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1179 <mem_alloc+0x39>
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <memset@plt>
mov    (%rsp),%rax
jmp    119f <mem_alloc+0x5f>
mov    0x2e60(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <strerror@plt>
lea    0xe70(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"posix_memalign failed for %s with error %s\0A\00"
define i8* @mem_alloc(i64 %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i64 %0, i64* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8* null, i8** %8
%10 = load i64, i64* %5
%11 = load i64, i64* %6
%12 = call i32 @posix_memalign(i8** %8, i64 %10, i64 %11)
store i32 %12, i32* %9
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %7
%18 = load i32, i32* %9
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %19)
store i8* null, i8** %4
br label %26
21: ; preds = %3
%22 = load i8*, i8** %8
%23 = load i64, i64* %6
%24 = call i32 @memset(i8* %22, i32 0, i64 %23)
%25 = load i8*, i8** %8
store i8* %25, i8** %4
br label %26
26: ; preds = %21, %15
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"posix_memalign failed for %s with error %s\0A\00"
define i8* @mem_alloc(i64 %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%6 = call i32 @posix_memalign(i8** %4, i64 %0, i64 %1)
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = call i8* @strerror(i32 %6)
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %11)
br label %16
13: ; preds = %3
%14 = call i32 @memset(i8* %7, i32 0, i64 %1)
%15 = load i8*, i8** %4
br label %16
16: ; preds = %13, %9
%17 = phi i8* [ null, %9 ], [ %15, %13 ]
ret i8* %17
}