#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 */
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ munlock(void const *, unsigned long);
char *strchr(char *, char);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int mln_unlock_code(void) {
  FILE *fp = fopen("/proc/self/maps", "r");
  char buf[1024];
  int err = 0;
  if (!fp)
    return -1;
  while (fgets(buf, sizeof(buf), fp)) {
    char *ptr = strchr(buf, ' ');
    unsigned long from;
    unsigned long size;
    if (!ptr)
      continue;
    if ('r' == ptr[1] && 'w' == ptr[2])
      continue;
    ptr = strchr(buf, '-');
    if (!ptr)
      continue;
    *ptr++ = '\0';
    from = strtoul(buf, NULL, 16);
    size = strtoul(ptr, NULL, 16) - from;
    if (munlock((const void *)from, size))
      err = errno;
  }
  fclose(fp);
  errno = err;
  return (err ? -1 : 0);
}


x86 assembly (clang, -O0):
<mln_unlock_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x414(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <mln_unlock_code+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <mln_unlock_code+0x1a8>
jmp    11a8 <mln_unlock_code+0x48>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12da <mln_unlock_code+0x17a>
lea    -0x410(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    11f2 <mln_unlock_code+0x92>
jmp    11a8 <mln_unlock_code+0x48>
mov    -0x420(%rbp),%rax
movsbl 0x1(%rax),%ecx
mov    $0x72,%eax
cmp    %ecx,%eax
jne    1227 <mln_unlock_code+0xc7>
mov    -0x420(%rbp),%rax
movsbl 0x2(%rax),%ecx
mov    $0x77,%eax
cmp    %ecx,%eax
jne    1227 <mln_unlock_code+0xc7>
jmp    11a8 <mln_unlock_code+0x48>
lea    -0x410(%rbp),%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1252 <mln_unlock_code+0xf2>
jmp    11a8 <mln_unlock_code+0x48>
mov    -0x420(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x420(%rbp)
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x428(%rbp)
mov    -0x420(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
sub    -0x428(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x430(%rbp),%rsi
call   1030 <munlock@plt>
cmp    $0x0,%rax
je     12d5 <mln_unlock_code+0x175>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
jmp    11a8 <mln_unlock_code+0x48>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x414(%rbp),%ecx
mov    0x2cf0(%rip),%rax
mov    %ecx,(%rax)
mov    -0x414(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mln_unlock_code>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
lea    0xe88(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1269 <mln_unlock_code+0x109>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1250 <mln_unlock_code+0xf0>
mov    %rsp,%r15
mov    0x2e2a(%rip),%rbp
jmp    11d5 <mln_unlock_code+0x75>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1250 <mln_unlock_code+0xf0>
mov    %r15,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11c0 <mln_unlock_code+0x60>
cmpb   $0x72,0x1(%rax)
jne    11f3 <mln_unlock_code+0x93>
cmpb   $0x77,0x2(%rax)
je     11c0 <mln_unlock_code+0x60>
mov    %r15,%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11c0 <mln_unlock_code+0x60>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,(%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,%r12
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
sub    %r12,%rax
mov    %r12,%rdi
mov    %rax,%rsi
call   1030 <munlock@plt>
test   %rax,%rax
je     11c0 <mln_unlock_code+0x60>
mov    0x0(%rbp),%r13d
jmp    11c0 <mln_unlock_code+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2d81(%rip),%rax
mov    %r13d,(%rax)
neg    %r13d
sbb    %eax,%eax
jmp    126e <mln_unlock_code+0x10e>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @mln_unlock_code() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
store i32 0, i32* %4
%9 = load i32*, i32** %2
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %0
store i32 -1, i32* %1
br label %68
12: ; preds = %0
br label %13
13: ; preds = %59, %42, %36, %23, %12
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %2
%16 = call i64 @fgets(i8* %14, i32 1024, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %60
18: ; preds = %13
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = call i8* @strchr(i8* %19, i8 signext 32)
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %18
br label %13
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 114, %28
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 2
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 119, %34
br i1 %35, label %36, label %37
36: ; preds = %30
br label %13
37: ; preds = %30, %24
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%39 = call i8* @strchr(i8* %38, i8 signext 45)
store i8* %39, i8** %5
%40 = load i8*, i8** %5
%41 = icmp ne i8* %40, null
br i1 %41, label %43, label %42
42: ; preds = %37
br label %13
43: ; preds = %37
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %5
store i8 0, i8* %44
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%47 = call i64 @strtoul(i8* %46, i32* null, i32 16)
store i64 %47, i64* %6
%48 = load i8*, i8** %5
%49 = call i64 @strtoul(i8* %48, i32* null, i32 16)
%50 = load i64, i64* %6
%51 = sub i64 %49, %50
store i64 %51, i64* %7
%52 = load i64, i64* %6
%53 = inttoptr i64 %52 to i8*
%54 = load i64, i64* %7
%55 = call i64 @munlock(i8* %53, i64 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %43
%58 = load i32, i32* @errno
store i32 %58, i32* %4
br label %59
59: ; preds = %57, %43
br label %13
60: ; preds = %13
%61 = load i32*, i32** %2
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %4
store i32 %63, i32* @errno
%64 = load i32, i32* %4
%65 = icmp ne i32 %64, 0
%66 = zext i1 %65 to i64
%67 = select i1 %65, i32 -1, i32 0
store i32 %67, i32* %1
br label %68
68: ; preds = %60, %11
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @mln_unlock_code() {
%1 = alloca [1024 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = icmp eq i32* %2, null
br i1 %4, label %42, label %5
5: ; preds = %0
%6 = call i64 @fgets(i8* %3, i32 1024, i32* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %37, label %8
8: ; preds = %5, %33
%9 = phi i32 [ %34, %33 ], [ 0, %5 ]
%10 = call i8* @strchr(i8* %3, i8 signext 32)
%11 = icmp eq i8* %10, null
br i1 %11, label %33, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 114
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %10, i64 2
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 119
br i1 %19, label %33, label %20
20: ; preds = %16, %12
%21 = call i8* @strchr(i8* %3, i8 signext 45)
%22 = icmp eq i8* %21, null
br i1 %22, label %33, label %23
23: ; preds = %20
%24 = getelementptr inbounds i8, i8* %21, i64 1
store i8 0, i8* %21
%25 = call i64 @strtoul(i8* nocapture %3, i32* null, i32 16)
%26 = call i64 @strtoul(i8* nocapture %24, i32* null, i32 16)
%27 = sub i64 %26, %25
%28 = inttoptr i64 %25 to i8*
%29 = call i64 @munlock(i8* %28, i64 %27)
%30 = icmp eq i64 %29, 0
%31 = load i32, i32* @errno
%32 = select i1 %30, i32 %9, i32 %31
br label %33
33: ; preds = %20, %16, %8, %23
%34 = phi i32 [ %32, %23 ], [ %9, %8 ], [ %9, %16 ], [ %9, %20 ]
%35 = call i64 @fgets(i8* %3, i32 1024, i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %8
37: ; preds = %33, %5
%38 = phi i32 [ 0, %5 ], [ %34, %33 ]
%39 = call i32 @fclose(i32* %2)
store i32 %38, i32* @errno
%40 = icmp ne i32 %38, 0
%41 = sext i1 %40 to i32
br label %42
42: ; preds = %0, %37
%43 = phi i32 [ %41, %37 ], [ -1, %0 ]
ret i32 %43
}