#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 *rpaligned_alloc(size_t, size_t);
int rpposix_memalign(void **memptr, size_t alignment, size_t size) {
  if (memptr)
    *memptr = rpaligned_alloc(alignment, size);
  else
    return EINVAL;
  return *memptr ? 0 : ENOMEM;
}


x86 assembly (clang, -O0):
<rpposix_memalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114b <rpposix_memalign+0x3b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <rpaligned_alloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    115c <rpposix_memalign+0x4c>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1186 <rpposix_memalign+0x76>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1174 <rpposix_memalign+0x64>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1180 <rpposix_memalign+0x70>
mov    0x2e65(%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):
<rpposix_memalign>:
push   %rbx
test   %rdi,%rdi
je     113a <rpposix_memalign+0x2a>
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1030 <rpaligned_alloc@plt>
mov    %rax,%rcx
mov    %rax,(%rbx)
xor    %eax,%eax
test   %rcx,%rcx
jne    1143 <rpposix_memalign+0x33>
mov    0x2ea8(%rip),%rax
jmp    1141 <rpposix_memalign+0x31>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @rpposix_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 i8**, i8*** %5
%9 = icmp ne i8** %8, null
br i1 %9, label %10, label %15
10: ; preds = %3
%11 = load i64, i64* %6
%12 = load i64, i64* %7
%13 = call i8* @rpaligned_alloc(i64 %11, i64 %12)
%14 = load i8**, i8*** %5
store i8* %13, i8** %14
br label %17
15: ; preds = %3
%16 = load i32, i32* @EINVAL
store i32 %16, i32* %4
br label %26
17: ; preds = %10
%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 = %17
br label %24
22: ; preds = %17
%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, %15
%27 = load i32, i32* %4
ret i32 %27
}

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