#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int EILSEQ;
int /*<<< orphan*/ LOCK_SH;
int /*<<< orphan*/ LOCK_UN;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int errno;
char *find_key(char *, int /*<<< orphan*/, char const *, size_t, int);
scalar_t__ flock(int, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strpbrk(char *, char *);
char *read_key(const char *pathname, const char *key, int icase) {
  struct stat st;
  char *map, *off, *end, *str = NULL;
  off_t size;
  size_t len;
  int fd, err = 0;
  fd = open(pathname, O_RDONLY);
  if (fd < 0)
    return NULL;
  if (flock(fd, LOCK_SH) < 0) {
    err = errno;
    goto close;
  }
  if (fstat(fd, &st) < 0) {
    err = errno;
    goto unlock;
  }
  size = st.st_size;
  map = (char *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  if (!map || map == MAP_FAILED) {
    err = errno;
    goto unlock;
  }
  len = strlen(key);
  off = find_key(map, size, key, len, icase);
  if (!off) {
    err = EILSEQ;
    goto unmap;
  }
  end = strpbrk(off, "\r\n");
  if (!end) {
    err = EILSEQ;
    goto unmap;
  }
  str = (char *)malloc(end - off - len);
  if (!str) {
    err = EILSEQ;
    goto unmap;
  }
  memset(str, 0, end - off - len);
  strncpy(str, off + len + 1, end - off - len - 1);
unmap:
  munmap(map, size);
unlock:
  flock(fd, LOCK_UN);
close:
  close(fd);
  errno = err;
  return str;
}


x86 assembly (clang, -O0):
<read_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    120e <read_key+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    13ea <read_key+0x22a>
mov    -0x54(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
cmp    $0x0,%rax
jge    123a <read_key+0x7a>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    13ce <read_key+0x20e>
mov    -0x54(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0x0,%rax
jge    1261 <read_key+0xa1>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    13bd <read_key+0x1fd>
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    0x2d66(%rip),%rax
mov    (%rax),%ecx
mov    -0x54(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12af <read_key+0xef>
mov    -0x28(%rbp),%rax
mov    0x2d02(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <read_key+0x100>
mov    0x2d02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    13bd <read_key+0x1fd>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   10d0 <find_key@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1305 <read_key+0x145>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    13b1 <read_key+0x1f1>
mov    -0x30(%rbp),%rdi
lea    0xcf0(%rip),%rsi
call   1090 <strpbrk@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1335 <read_key+0x175>
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    13b1 <read_key+0x1f1>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
sub    -0x50(%rbp),%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    136b <read_key+0x1ab>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    13b1 <read_key+0x1f1>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
sub    -0x50(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    -0x50(%rbp),%rsi
add    $0x1,%rsi
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
sub    -0x50(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   10a0 <munmap@plt>
mov    -0x54(%rbp),%edi
mov    0x2be9(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
mov    -0x54(%rbp),%edi
call   1070 <close@plt>
mov    -0x58(%rbp),%ecx
mov    0x2bd8(%rip),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %rsi,%rbp
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
js     12ed <read_key+0x12d>
mov    %eax,%ebx
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
test   %rax,%rax
js     12ff <read_key+0x13f>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10e0 <fstat@plt>
test   %rax,%rax
js     12f1 <read_key+0x131>
mov    0x8(%rsp),%r13d
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r13d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     12f1 <read_key+0x131>
mov    %rax,%r15
mov    0x2d54(%rip),%rax
cmp    %r15,(%rax)
je     12f1 <read_key+0x131>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %r13d,%esi
mov    %rbp,%rdx
mov    %rax,%rcx
mov    %r12d,%r8d
call   10d0 <find_key@plt>
test   %rax,%rax
je     130d <read_key+0x14d>
mov    %rax,%r12
mov    %r13d,0x4(%rsp)
lea    0xd6c(%rip),%rsi
mov    %rax,%rdi
call   1090 <strpbrk@plt>
test   %rax,%rax
je     131b <read_key+0x15b>
mov    %rax,%r13
sub    %r14d,%r13d
sub    %r12d,%r13d
mov    %r13d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     131b <read_key+0x15b>
mov    %rax,%rbp
mov    %r14,0x10(%rsp)
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1060 <memset@plt>
mov    0x10(%rsp),%rax
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
add    $0xffffffff,%r13d
mov    %rbp,%rdi
mov    %r13d,%edx
call   1030 <strncpy@plt>
jmp    1327 <read_key+0x167>
xor    %ebp,%ebp
jmp    1358 <read_key+0x198>
mov    0x2cc0(%rip),%rax
mov    (%rax),%r14d
xor    %ebp,%ebp
jmp    1337 <read_key+0x177>
mov    0x2cb2(%rip),%rax
mov    (%rax),%r14d
xor    %ebp,%ebp
jmp    1347 <read_key+0x187>
mov    0x2cd4(%rip),%rax
mov    (%rax),%r14d
xor    %ebp,%ebp
jmp    132c <read_key+0x16c>
mov    0x2cc6(%rip),%rax
mov    (%rax),%r14d
xor    %ebp,%ebp
mov    0x4(%rsp),%r13d
mov    %r15,%rdi
mov    %r13d,%esi
call   10a0 <munmap@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2c63(%rip),%rax
mov    %r14d,(%rax)
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@LOCK_SH = global i32 0
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EILSEQ = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@LOCK_UN = global i32 0
define i8* @read_key(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %12
store i32 0, i32* %16
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %15
%20 = load i32, i32* %15
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %3
store i8* null, i8** %4
br label %127
23: ; preds = %3
%24 = load i32, i32* %15
%25 = load i32, i32* @LOCK_SH
%26 = call i64 @flock(i32 %24, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i32, i32* @errno
store i32 %29, i32* %16
br label %122
30: ; preds = %23
%31 = load i32, i32* %15
%32 = call i64 @fstat(i32 %31, %struct.stat* %8)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = load i32, i32* @errno
store i32 %35, i32* %16
br label %118
36: ; preds = %30
%37 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %13
%39 = load i32, i32* %13
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* %15
%43 = call i64 @mmap(i32* null, i32 %39, i32 %40, i32 %41, i32 %42, i32 0)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %51
47: ; preds = %36
%48 = load i8*, i8** %9
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47, %36
%52 = load i32, i32* @errno
store i32 %52, i32* %16
br label %118
53: ; preds = %47
%54 = load i8*, i8** %6
%55 = call i64 @strlen(i8* %54)
store i64 %55, i64* %14
%56 = load i8*, i8** %9
%57 = load i32, i32* %13
%58 = load i8*, i8** %6
%59 = load i64, i64* %14
%60 = load i32, i32* %7
%61 = call i8* @find_key(i8* %56, i32 %57, i8* %58, i64 %59, i32 %60)
store i8* %61, i8** %10
%62 = load i8*, i8** %10
%63 = icmp ne i8* %62, null
br i1 %63, label %66, label %64
64: ; preds = %53
%65 = load i32, i32* @EILSEQ
store i32 %65, i32* %16
br label %114
66: ; preds = %53
%67 = load i8*, i8** %10
%68 = call i8* @strpbrk(i8* %67, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %68, i8** %11
%69 = load i8*, i8** %11
%70 = icmp ne i8* %69, null
br i1 %70, label %73, label %71
71: ; preds = %66
%72 = load i32, i32* @EILSEQ
store i32 %72, i32* %16
br label %114
73: ; preds = %66
%74 = load i8*, i8** %11
%75 = load i8*, i8** %10
%76 = ptrtoint i8* %74 to i64
%77 = ptrtoint i8* %75 to i64
%78 = sub i64 %76, %77
%79 = load i64, i64* %14
%80 = sub i64 %78, %79
%81 = trunc i64 %80 to i32
%82 = call i64 @malloc(i32 %81)
%83 = inttoptr i64 %82 to i8*
store i8* %83, i8** %12
%84 = load i8*, i8** %12
%85 = icmp ne i8* %84, null
br i1 %85, label %88, label %86
86: ; preds = %73
%87 = load i32, i32* @EILSEQ
store i32 %87, i32* %16
br label %114
88: ; preds = %73
%89 = load i8*, i8** %12
%90 = load i8*, i8** %11
%91 = load i8*, i8** %10
%92 = ptrtoint i8* %90 to i64
%93 = ptrtoint i8* %91 to i64
%94 = sub i64 %92, %93
%95 = load i64, i64* %14
%96 = sub i64 %94, %95
%97 = trunc i64 %96 to i32
%98 = call i32 @memset(i8* %89, i32 0, i32 %97)
%99 = load i8*, i8** %12
%100 = load i8*, i8** %10
%101 = load i64, i64* %14
%102 = getelementptr inbounds i8, i8* %100, i64 %101
%103 = getelementptr inbounds i8, i8* %102, i64 1
%104 = load i8*, i8** %11
%105 = load i8*, i8** %10
%106 = ptrtoint i8* %104 to i64
%107 = ptrtoint i8* %105 to i64
%108 = sub i64 %106, %107
%109 = load i64, i64* %14
%110 = sub i64 %108, %109
%111 = sub i64 %110, 1
%112 = trunc i64 %111 to i32
%113 = call i32 @strncpy(i8* %99, i8* %103, i32 %112)
br label %114
114: ; preds = %88, %86, %71, %64
%115 = load i8*, i8** %9
%116 = load i32, i32* %13
%117 = call i32 @munmap(i8* %115, i32 %116)
br label %118
118: ; preds = %114, %51, %34
%119 = load i32, i32* %15
%120 = load i32, i32* @LOCK_UN
%121 = call i64 @flock(i32 %119, i32 %120)
br label %122
122: ; preds = %118, %28
%123 = load i32, i32* %15
%124 = call i32 @close(i32 %123)
%125 = load i32, i32* %16
store i32 %125, i32* @errno
%126 = load i8*, i8** %12
store i8* %126, i8** %4
br label %127
127: ; preds = %122, %22
%128 = load i8*, i8** %4
ret i8* %128
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@LOCK_SH = global i32 0
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EILSEQ = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@LOCK_UN = global i32 0
define i8* @read_key(i8* nocapture %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %74, label %9
9: ; preds = %3
%10 = load i32, i32* @LOCK_SH
%11 = tail call i64 @flock(i32 %7, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* @errno
br label %70
15: ; preds = %9
%16 = call i64 @fstat(i32 %7, %struct.stat* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32, i32* @errno
br label %65
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i64 @mmap(i32* null, i32 %22, i32 %23, i32 %24, i32 %7, i32 0)
%26 = inttoptr i64 %25 to i8*
%27 = icmp eq i64 %25, 0
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %28, %26
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = load i32, i32* @errno
br label %65
33: ; preds = %20
%34 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%35 = tail call i8* @find_key(i8* %26, i32 %22, i8* %1, i64 %34, i32 %2)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = load i32, i32* @EILSEQ
br label %61
39: ; preds = %33
%40 = tail call i8* @strpbrk(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = load i32, i32* @EILSEQ
br label %61
44: ; preds = %39
%45 = ptrtoint i8* %40 to i64
%46 = ptrtoint i8* %35 to i64
%47 = add i64 %34, %46
%48 = sub i64 %45, %47
%49 = trunc i64 %48 to i32
%50 = tail call i64 @malloc(i32 %49)
%51 = inttoptr i64 %50 to i8*
%52 = icmp eq i64 %50, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = load i32, i32* @EILSEQ
br label %61
55: ; preds = %44
%56 = tail call i32 @memset(i8* %51, i32 0, i32 %49)
%57 = getelementptr inbounds i8, i8* %35, i64 %34
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = add i32 %49, -1
%60 = tail call i32 @strncpy(i8* %51, i8* %58, i32 %59)
br label %61
61: ; preds = %55, %53, %42, %37
%62 = phi i8* [ %51, %55 ], [ null, %53 ], [ null, %42 ], [ null, %37 ]
%63 = phi i32 [ 0, %55 ], [ %54, %53 ], [ %43, %42 ], [ %38, %37 ]
%64 = tail call i32 @munmap(i8* %26, i32 %22)
br label %65
65: ; preds = %61, %31, %18
%66 = phi i8* [ null, %18 ], [ null, %31 ], [ %62, %61 ]
%67 = phi i32 [ %19, %18 ], [ %32, %31 ], [ %63, %61 ]
%68 = load i32, i32* @LOCK_UN
%69 = tail call i64 @flock(i32 %7, i32 %68)
br label %70
70: ; preds = %65, %13
%71 = phi i8* [ null, %13 ], [ %66, %65 ]
%72 = phi i32 [ %14, %13 ], [ %67, %65 ]
%73 = tail call i32 @close(i32 %7)
store i32 %72, i32* @errno
br label %74
74: ; preds = %3, %70
%75 = phi i8* [ %71, %70 ], [ null, %3 ]
ret i8* %75
}