#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 *pt3memalign(size_t, size_t);
int pt3posix_memalign(void **memptr, size_t alignment, size_t size) {
  void *mem;
  if (alignment % sizeof(void *) != 0 ||
      !((((alignment / sizeof(void *)) - 1) & (alignment / sizeof(void *))) ==
        0) != 0 ||
      alignment == 0)
    return EINVAL;
  mem = pt3memalign(alignment, size);
  if (mem != NULL) {
    *memptr = mem;
    return 0;
  }
  return ENOMEM;
}


x86 assembly (clang, -O0):
<pt3posix_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
jne    116f <pt3posix_memalign+0x5f>
mov    -0x18(%rbp),%rax
shr    $0x3,%rax
sub    $0x1,%rax
mov    -0x18(%rbp),%rcx
shr    $0x3,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
sete   %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
jne    116f <pt3posix_memalign+0x5f>
cmpq   $0x0,-0x18(%rbp)
jne    1180 <pt3posix_memalign+0x70>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <pt3posix_memalign+0xaf>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <pt3memalign@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b3 <pt3posix_memalign+0xa3>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11bf <pt3posix_memalign+0xaf>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pt3posix_memalign>:
push   %rbx
test   $0x7,%sil
je     1122 <pt3posix_memalign+0x12>
mov    0x2eba(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
test   %rsi,%rsi
je     1117 <pt3posix_memalign+0x7>
mov    %rsi,%rax
shr    $0x3,%rax
lea    -0x1(%rax),%rcx
and    %rax,%rcx
jne    1117 <pt3posix_memalign+0x7>
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1030 <pt3memalign@plt>
test   %rax,%rax
je     1151 <pt3posix_memalign+0x41>
mov    %rax,(%rbx)
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e88(%rip),%rax
jmp    111e <pt3posix_memalign+0xe>

LLVM IR (-O0):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @pt3posix_memalign(i8** %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i64, i64* %6
%10 = urem i64 %9, 8
%11 = icmp ne i64 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %3
%13 = load i64, i64* %6
%14 = udiv i64 %13, 8
%15 = sub i64 %14, 1
%16 = load i64, i64* %6
%17 = udiv i64 %16, 8
%18 = and i64 %15, %17
%19 = icmp eq i64 %18, 0
%20 = xor i1 %19, true
%21 = zext i1 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %12
%24 = load i64, i64* %6
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23, %12, %3
%27 = load i32, i32* @EINVAL
store i32 %27, i32* %4
br label %39
28: ; preds = %23
%29 = load i64, i64* %6
%30 = load i64, i64* %7
%31 = call i8* @pt3memalign(i64 %29, i64 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i8*, i8** %8
%36 = load i8**, i8*** %5
store i8* %35, i8** %36
store i32 0, i32* %4
br label %39
37: ; preds = %28
%38 = load i32, i32* @ENOMEM
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %34, %26
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @pt3posix_memalign(i8** nocapture writeonly %0, i64 %1, i64 %2) {
%4 = and i64 %1, 7
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = lshr i64 %1, 3
%8 = add nuw nsw i64 %7, 2305843009213693951
%9 = and i64 %8, %7
%10 = icmp ne i64 %9, 0
%11 = icmp eq i64 %1, 0
%12 = or i1 %11, %10
br i1 %12, label %13, label %15
13: ; preds = %6, %3
%14 = load i32, i32* @EINVAL
br label %21
15: ; preds = %6
%16 = tail call i8* @pt3memalign(i64 %1, i64 %2)
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15
store i8* %16, i8** %0
br label %21
19: ; preds = %15
%20 = load i32, i32* @ENOMEM
br label %21
21: ; preds = %19, %18, %13
%22 = phi i32 [ %14, %13 ], [ 0, %18 ], [ %20, %19 ]
ret i32 %22
}