#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 /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ calloc(int, int);
size_t sysconf(int /*<<< orphan*/);
unsigned char *my_memalign(int length, unsigned char **wrkBuffp) {
  unsigned char *wrkBuff;
  size_t psz;
  psz = sysconf(_SC_PAGESIZE);
  wrkBuff = (unsigned char *)calloc(length + psz, 1);
  if (NULL == wrkBuff) {
    if (wrkBuffp)
      *wrkBuffp = NULL;
    return NULL;
  } else if (wrkBuffp)
    *wrkBuffp = wrkBuff;
  return (unsigned char *)(((unsigned long)wrkBuff + psz - 1) & (~(psz - 1)));
}


x86 assembly (clang, -O0):
<my_memalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x28(%rbp)
movslq -0xc(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1188 <my_memalign+0x68>
cmpq   $0x0,-0x18(%rbp)
je     117b <my_memalign+0x5b>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11c2 <my_memalign+0xa2>
cmpq   $0x0,-0x18(%rbp)
je     119e <my_memalign+0x7e>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    11a3 <my_memalign+0x83>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_memalign>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eb8(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,%rbx
add    %ebx,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
test   %rax,%rax
je     1164 <my_memalign+0x44>
test   %r14,%r14
je     1155 <my_memalign+0x35>
mov    %rax,(%r14)
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
neg    %rbx
and    %rax,%rbx
jmp    1172 <my_memalign+0x52>
test   %r14,%r14
je     1170 <my_memalign+0x50>
movq   $0x0,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
define i8* @my_memalign(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @_SC_PAGESIZE
%9 = call i64 @sysconf(i32 %8)
store i64 %9, i64* %7
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = load i64, i64* %7
%13 = add i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = call i64 @calloc(i32 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* null, %17
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = icmp ne i8** %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i8**, i8*** %5
store i8* null, i8** %23
br label %24
24: ; preds = %22, %19
store i8* null, i8** %3
br label %43
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = icmp ne i8** %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i8*, i8** %6
%30 = load i8**, i8*** %5
store i8* %29, i8** %30
br label %31
31: ; preds = %28, %25
br label %32
32: ; preds = %31
%33 = load i8*, i8** %6
%34 = ptrtoint i8* %33 to i64
%35 = load i64, i64* %7
%36 = add i64 %34, %35
%37 = sub i64 %36, 1
%38 = load i64, i64* %7
%39 = sub i64 %38, 1
%40 = xor i64 %39, -1
%41 = and i64 %37, %40
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %3
br label %43
43: ; preds = %32, %24
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
define i8* @my_memalign(i32 %0, i8** writeonly %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i64 @sysconf(i32 %3)
%5 = trunc i64 %4 to i32
%6 = add i32 %5, %0
%7 = tail call i64 @calloc(i32 %6, i32 1)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
%10 = icmp eq i8** %1, null
br i1 %9, label %11, label %13
11: ; preds = %2
br i1 %10, label %21, label %12
12: ; preds = %11
store i8* null, i8** %1
br label %21
13: ; preds = %2
br i1 %10, label %15, label %14
14: ; preds = %13
store i8* %8, i8** %1
br label %15
15: ; preds = %13, %14
%16 = add i64 %4, -1
%17 = add i64 %16, %7
%18 = sub i64 0, %4
%19 = and i64 %17, %18
%20 = inttoptr i64 %19 to i8*
br label %21
21: ; preds = %11, %12, %15
%22 = phi i8* [ %20, %15 ], [ null, %12 ], [ null, %11 ]
ret i8* %22
}