#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 */
scalar_t__ ENOMEM;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
scalar_t__ mlock(char *, size_t);
int /*<<< orphan*/ stderr;
char *alloc_workbuf(size_t size) {
  char *ptr;
  ptr = malloc(size);
  if (ptr == NULL)
    return NULL;
  if (mlock(ptr, size)) {
    if (errno == ENOMEM)
      fprintf(stderr, "Not enough permissions to lock memory\n");
    free(ptr);
    return NULL;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<alloc_workbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <alloc_workbuf+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11d5 <alloc_workbuf+0x95>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <mlock@plt>
cmp    $0x0,%rax
je     11cd <alloc_workbuf+0x8d>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
jne    11b7 <alloc_workbuf+0x77>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d5 <alloc_workbuf+0x95>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_workbuf>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <malloc@plt>
test   %rax,%rax
je     1197 <alloc_workbuf+0x57>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <mlock@plt>
test   %rax,%rax
je     119a <alloc_workbuf+0x5a>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <alloc_workbuf+0x4f>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
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):
@errno = global i64 0
@ENOMEM = global i64 0
@stderr = global i32 0
@.str = [39 x i8] c"Not enough permissions to lock memory\0A\00"
define i8* @alloc_workbuf(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = call i8* @malloc(i64 %5)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %27
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i64, i64* %3
%13 = call i64 @mlock(i8* %11, i64 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOMEM
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %15
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
store i8* null, i8** %2
br label %27
25: ; preds = %10
%26 = load i8*, i8** %4
store i8* %26, i8** %2
br label %27
27: ; preds = %25, %22, %9
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
@errno = global i64 0
@ENOMEM = global i64 0
@stderr = global i32 0
@.str = [39 x i8] c"Not enough permissions to lock memory\0A\00"
define i8* @alloc_workbuf(i64 %0) {
%2 = tail call i8* @malloc(i64 %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = tail call i64 @mlock(i8* %2, i64 %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOMEM
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %7
%15 = tail call i32 @free(i8* %2)
br label %16
16: ; preds = %4, %1, %14
%17 = phi i8* [ null, %14 ], [ null, %1 ], [ %2, %4 ]
ret i8* %17
}