#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 EINVAL;
int ENOMEM;
void *memalign(size_t, size_t);
int posix_memalign(void **memptr, size_t alignment, size_t size) {
  if (alignment % sizeof(void *) != 0)
    return EINVAL;
  *memptr = memalign(alignment, size);
  return (*memptr != NULL ? 0 : ENOMEM);
}


x86 assembly (clang, -O0):
<posix_memalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     1147 <posix_memalign+0x37>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <posix_memalign+0x78>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <memalign@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1176 <posix_memalign+0x66>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1182 <posix_memalign+0x72>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_memalign>:
push   %rbx
test   $0x7,%sil
je     1120 <posix_memalign+0x10>
mov    0x2eba(%rip),%rax
jmp    1142 <posix_memalign+0x32>
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1030 <memalign@plt>
mov    %rax,%rcx
mov    %rax,(%rbx)
xor    %eax,%eax
test   %rcx,%rcx
jne    1144 <posix_memalign+0x34>
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @posix_memalign(i8** %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %6
%9 = urem i64 %8, 8
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* %4
br label %26
13: ; preds = %3
%14 = load i64, i64* %6
%15 = load i64, i64* %7
%16 = call i8* @memalign(i64 %14, i64 %15)
%17 = load i8**, i8*** %5
store i8* %16, i8** %17
%18 = load i8**, i8*** %5
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %13
br label %24
22: ; preds = %13
%23 = load i32, i32* @ENOMEM
br label %24
24: ; preds = %22, %21
%25 = phi i32 [ 0, %21 ], [ %23, %22 ]
store i32 %25, i32* %4
br label %26
26: ; preds = %24, %11
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @posix_memalign(i8** nocapture writeonly %0, i64 %1, i64 %2) {
%4 = and i64 %1, 7
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = load i32, i32* @EINVAL
br label %13
8: ; preds = %3
%9 = tail call i8* @memalign(i64 %1, i64 %2)
store i8* %9, i8** %0
%10 = icmp eq i8* %9, null
%11 = load i32, i32* @ENOMEM
%12 = select i1 %10, i32 %11, i32 0
br label %13
13: ; preds = %8, %6
%14 = phi i32 [ %7, %6 ], [ %12, %8 ]
ret i32 %14
}