#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;
int /*<<< orphan*/ err_exit(char *);
long sysconf(int /*<<< orphan*/);
long align_to_page(size_t size) {
  long pg_size;
  pg_size = sysconf(_SC_PAGESIZE);
  if (pg_size == -1)
    err_exit("sysconf()");
  if (size <= pg_size)
    return pg_size;
  else
    return size + pg_size - (size % pg_size);
}


x86 assembly (clang, -O0):
<align_to_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1155 <align_to_page+0x35>
lea    0xeb0(%rip),%rdi
call   1030 <err_exit@plt>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     1170 <align_to_page+0x50>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1193 <align_to_page+0x73>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    -0x20(%rbp),%rax
sub    %rdx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<align_to_page>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     1145 <align_to_page+0x25>
cmp    %r14,%rbx
jb     1156 <align_to_page+0x36>
jmp    117f <align_to_page+0x5f>
lea    0xeb4(%rip),%rdi
call   1030 <err_exit@plt>
cmp    %r14,%rbx
jae    117f <align_to_page+0x5f>
mov    %r14,%rax
or     %rbx,%rax
mov    %rbx,%rcx
add    %r14,%rcx
shr    $0x20,%rax
je     1172 <align_to_page+0x52>
mov    %r14,%rax
xor    %edx,%edx
div    %rbx
jmp    1179 <align_to_page+0x59>
mov    %r14d,%eax
xor    %edx,%edx
div    %ebx
sub    %rdx,%rcx
mov    %rcx,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [10 x i8] c"sysconf()\00"
define i64 @align_to_page(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %3
%5 = load i32, i32* @_SC_PAGESIZE
%6 = call i64 @sysconf(i32 %5)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @err_exit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = icmp ule i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i64, i64* %4
store i64 %16, i64* %2
br label %25
17: ; preds = %11
%18 = load i64, i64* %3
%19 = load i64, i64* %4
%20 = add i64 %18, %19
%21 = load i64, i64* %3
%22 = load i64, i64* %4
%23 = urem i64 %21, %22
%24 = sub i64 %20, %23
store i64 %24, i64* %2
br label %25
25: ; preds = %17, %15
%26 = load i64, i64* %2
ret i64 %26
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [10 x i8] c"sysconf()\00"
define i64 @align_to_page(i64 %0) {
%2 = load i32, i32* @_SC_PAGESIZE
%3 = tail call i64 @sysconf(i32 %2)
%4 = icmp eq i64 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @err_exit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = icmp ult i64 %3, %0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = add i64 %3, %0
%11 = urem i64 %0, %3
%12 = sub i64 %10, %11
br label %13
13: ; preds = %7, %9
%14 = phi i64 [ %12, %9 ], [ %3, %7 ]
ret i64 %14
}