#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 errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int mlock(void *, size_t);
int /*<<< orphan*/ stderr;
void *alloc_locked(size_t size) {
  void *ptr;
  int r;
  if ((size & 63) != 0) {
    fprintf(stderr, "size should be 8 byte-aligned");
    return NULL;
  }
  ptr = malloc(size);
  if (ptr == NULL) {
    return NULL;
  }
  r = mlock(ptr, size);
  if (r < 0) {
    fprintf(stderr, "mlock() failed w/ code: %d\n", errno);
    free(ptr);
    return NULL;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<alloc_locked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
and    $0x3f,%rax
cmp    $0x0,%rax
je     1182 <alloc_locked+0x42>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ff <alloc_locked+0xbf>
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a7 <alloc_locked+0x67>
movq   $0x0,-0x8(%rbp)
jmp    11ff <alloc_locked+0xbf>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <mlock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f7 <alloc_locked+0xb7>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ff <alloc_locked+0xbf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_locked>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   $0x3f,%bl
je     1168 <alloc_locked+0x28>
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
lea    0xea4(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11b2 <alloc_locked+0x72>
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11af <alloc_locked+0x6f>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <mlock@plt>
test   %eax,%eax
jns    11b2 <alloc_locked+0x72>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    0xe7e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"size should be 8 byte-aligned\00"
@.str.1 = [28 x i8] c"mlock() failed w/ code: %d\0A\00"
@errno = global i32 0
define i8* @alloc_locked(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = and i64 %6, 63
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %32
12: ; preds = %1
%13 = load i64, i64* %3
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %12
store i8* null, i8** %2
br label %32
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = load i64, i64* %3
%21 = call i32 @mlock(i8* %19, i64 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
store i8* null, i8** %2
br label %32
30: ; preds = %18
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %32
32: ; preds = %30, %24, %17, %9
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"size should be 8 byte-aligned\00"
@.str.1 = [28 x i8] c"mlock() failed w/ code: %d\0A\00"
@errno = global i32 0
define i8* @alloc_locked(i64 %0) {
%2 = and i64 %0, 63
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %18
7: ; preds = %1
%8 = tail call i8* @malloc(i64 %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = tail call i32 @mlock(i8* %8, i64 %0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i32 @free(i8* %8)
br label %18
18: ; preds = %10, %7, %13, %4
%19 = phi i8* [ null, %4 ], [ null, %13 ], [ null, %7 ], [ %8, %10 ]
ret i8* %19
}