#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 */
void *malloc(size_t);
scalar_t__ posix_memalign(void **, size_t, size_t);
void *gfx_alloc(size_t size, size_t align) {
  if (align == 1) {
    return malloc(size);
  }
  if (!(align & (align - 1)) && align < sizeof(void *)) {
    align = sizeof(void *);
  }
  void *allocated;
  if (posix_memalign(&allocated, align, size) != 0) {
    return 0;
  }
  return allocated;
}


x86 assembly (clang, -O0):
<gfx_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jne    114d <gfx_alloc+0x2d>
mov    -0x10(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11a9 <gfx_alloc+0x89>
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    1179 <gfx_alloc+0x59>
cmpq   $0x8,-0x18(%rbp)
jae    1179 <gfx_alloc+0x59>
movq   $0x8,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1040 <posix_memalign@plt>
cmp    $0x0,%rax
je     11a1 <gfx_alloc+0x81>
movq   $0x0,-0x8(%rbp)
jmp    11a9 <gfx_alloc+0x89>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gfx_alloc>:
mov    %rdi,%rdx
cmp    $0x1,%rsi
jne    1131 <gfx_alloc+0x11>
mov    %rdx,%rdi
jmp    1030 <malloc@plt>
push   %rax
lea    -0x1(%rsi),%rax
test   %rax,%rsi
mov    $0x8,%eax
cmovne %rsi,%rax
cmp    $0x8,%rsi
cmovae %rsi,%rax
mov    %rsp,%rdi
mov    %rax,%rsi
call   1040 <posix_memalign@plt>
test   %rax,%rax
jne    1160 <gfx_alloc+0x40>
mov    (%rsp),%rax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i8* @gfx_alloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp eq i64 %7, 1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %4
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %3
br label %30
12: ; preds = %2
%13 = load i64, i64* %5
%14 = load i64, i64* %5
%15 = sub i64 %14, 1
%16 = and i64 %13, %15
%17 = icmp ne i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i64, i64* %5
%20 = icmp ult i64 %19, 8
br i1 %20, label %21, label %22
21: ; preds = %18
store i64 8, i64* %5
br label %22
22: ; preds = %21, %18, %12
%23 = load i64, i64* %5
%24 = load i64, i64* %4
%25 = call i64 @posix_memalign(i8** %6, i64 %23, i64 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i8* null, i8** %3
br label %30
28: ; preds = %22
%29 = load i8*, i8** %6
store i8* %29, i8** %3
br label %30
30: ; preds = %28, %27, %9
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
define i8* @gfx_alloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = icmp eq i64 %1, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @malloc(i64 %0)
br label %18
7: ; preds = %2
%8 = tail call i64 @llvm.ctpop.i64(i64 %1)
%9 = icmp ult i64 %8, 2
%10 = icmp ult i64 %1, 8
%11 = and i1 %10, %9
%12 = select i1 %11, i64 8, i64 %1
%13 = bitcast i8** %3 to i8*
%14 = call i64 @posix_memalign(i8** %3, i64 %12, i64 %0)
%15 = icmp eq i64 %14, 0
%16 = load i8*, i8** %3
%17 = select i1 %15, i8* %16, i8* null
br label %18
18: ; preds = %7, %5
%19 = phi i8* [ %6, %5 ], [ %17, %7 ]
ret i8* %19
}