#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;
scalar_t__ check_align(size_t);
int errno;
void *memalign(size_t, size_t);
int posix_memalign(void **ptr, size_t align, size_t size) {
  if (check_align(align))
    return EINVAL;
  int saved_errno = errno;
  void *p = memalign(align, size);
  if (p == NULL) {
    errno = saved_errno;
    return ENOMEM;
  }
  *ptr = p;
  return 0;
}


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),%rdi
call   1030 <check_align@plt>
cmp    $0x0,%rax
je     1158 <posix_memalign+0x38>
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <posix_memalign+0x8f>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <memalign@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119d <posix_memalign+0x7d>
mov    -0x24(%rbp),%ecx
mov    0x2e4e(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <posix_memalign+0x8f>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_memalign>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <check_align@plt>
test   %rax,%rax
je     1146 <posix_memalign+0x26>
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
jmp    115b <posix_memalign+0x3b>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <memalign@plt>
test   %rax,%rax
je     1161 <posix_memalign+0x41>
mov    %rax,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e78(%rip),%rax
jmp    1142 <posix_memalign+0x22>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = 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
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%10 = load i64, i64* %6
%11 = call i64 @check_align(i64 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* @EINVAL
store i32 %14, i32* %4
br label %28
15: ; preds = %3
%16 = load i32, i32* @errno
store i32 %16, i32* %8
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = call i8* @memalign(i64 %17, i64 %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %8
store i32 %23, i32* @errno
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* %4
br label %28
25: ; preds = %15
%26 = load i8*, i8** %9
%27 = load i8**, i8*** %5
store i8* %26, i8** %27
store i32 0, i32* %4
br label %28
28: ; preds = %25, %22, %13
%29 = load i32, i32* %4
ret i32 %29
}

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