#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long, int);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int posix_memalign(void *, int, long);
int /*<<< orphan*/ stderr;
char *alloc_buf(long msize, int alignment) {
  int res;
  char *buf;
  FILE *fp;
  if ((res = posix_memalign((void *)&buf, alignment, msize)) != 0) {
    fprintf(stderr, "cannot allocate aligned buffer of %ld bytes (err %d)\n",
            msize, res);
    return (NULL);
  }
  fp = fopen("/dev/urandom", "r");
  fread(buf, (8 * 1024 * 1024), 1, fp);
  fclose(fp);
  return (buf);
}


x86 assembly (clang, -O0):
<alloc_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1070 <posix_memalign@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     11a4 <alloc_buf+0x54>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xe6e(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <alloc_buf+0x93>
lea    0xe8b(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x800000,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_buf>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
call   1070 <posix_memalign@plt>
test   %eax,%eax
je     1187 <alloc_buf+0x37>
mov    0x2e70(%rip),%rcx
mov    (%rcx),%edi
lea    0xe87(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1050 <fprintf@plt>
xor    %eax,%eax
jmp    11c1 <alloc_buf+0x71>
lea    0xea8(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
mov    $0x800000,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"cannot allocate aligned buffer of %ld bytes (err %d)\0A\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @alloc_buf(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
store i64 %0, i64* %4
store i32 %1, i32* %5
%9 = bitcast i8** %7 to i8*
%10 = load i32, i32* %5
%11 = load i64, i64* %4
%12 = call i32 @posix_memalign(i8* %9, i32 %10, i64 %11)
store i32 %12, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i64, i64* %4
%17 = load i32, i32* %6
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i64 %16, i32 %17)
store i8* null, i8** %3
br label %27
19: ; preds = %2
%20 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i8*, i8** %7
%22 = load i32*, i32** %8
%23 = call i32 @fread(i8* %21, i32 8388608, i32 1, i32* %22)
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
%26 = load i8*, i8** %7
store i8* %26, i8** %3
br label %27
27: ; preds = %19, %14
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"cannot allocate aligned buffer of %ld bytes (err %d)\0A\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @alloc_buf(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @posix_memalign(i8* %4, i32 %1, i64 %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i64 %0, i32 %5)
br label %16
10: ; preds = %2
%11 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = load i8*, i8** %3
%13 = call i32 @fread(i8* %12, i32 8388608, i32 1, i32* %11)
%14 = call i32 @fclose(i32* %11)
%15 = load i8*, i8** %3
br label %16
16: ; preds = %10, %7
%17 = phi i8* [ null, %7 ], [ %15, %10 ]
ret i8* %17
}