#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ getkey(char *, char *, char *);
scalar_t__ getsection(char *, char *);
int /*<<< orphan*/ isremark(char *);
int readline(char *, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
int getconfigstr(const char *section, const char *keyname, char *keyvalue,
                 unsigned int len, const char *filename) {
  int step = 0;
  FILE *stream;
  char sec[2048] = "";
  char ken[2048] = "";
  char kev[2048] = "";
  char line[2048] = "";
  if ((stream = fopen(filename, "r")) == NULL)
    return -1;
  while (!feof(stream)) {
    if (readline(line, stream) == -1) {
      fclose(stream);
      return -2;
    }
    if (!isremark(line)) {
      if (step == 0) {
        if (getsection(line, sec) == 0) {
          if (strcmp(sec, section) == 0)
            step = 1;
        }
      } else {
        if (getkey(line, ken, kev) == 0) {
          if (strcmp(ken, keyname) == 0) {
            strncpy(keyvalue, kev, len);
            fclose(stream);
            return 0;
          }
        }
      }
    }
  }
  fclose(stream);
  return -2;
}


x86 assembly (clang, -O0):
<getconfigstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x840(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
lea    -0x1040(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
lea    -0x1840(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
lea    -0x2040(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
mov    -0x30(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    123b <getconfigstr+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <getconfigstr+0x1dd>
jmp    1240 <getconfigstr+0xa0>
mov    -0x40(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    125e <getconfigstr+0xbe>
jmp    136d <getconfigstr+0x1cd>
lea    -0x2040(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <readline@plt>
cmp    $0xffffffff,%eax
jne    128c <getconfigstr+0xec>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    137d <getconfigstr+0x1dd>
lea    -0x2040(%rbp),%rdi
call   10c0 <isremark@plt>
cmp    $0x0,%eax
jne    1368 <getconfigstr+0x1c8>
cmpl   $0x0,-0x34(%rbp)
jne    12f3 <getconfigstr+0x153>
lea    -0x2040(%rbp),%rdi
lea    -0x840(%rbp),%rsi
call   1090 <getsection@plt>
cmp    $0x0,%rax
jne    12ee <getconfigstr+0x14e>
lea    -0x840(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12e9 <getconfigstr+0x149>
movl   $0x1,-0x34(%rbp)
jmp    12ee <getconfigstr+0x14e>
jmp    1363 <getconfigstr+0x1c3>
lea    -0x2040(%rbp),%rdi
lea    -0x1040(%rbp),%rsi
lea    -0x1840(%rbp),%rdx
call   10a0 <getkey@plt>
cmp    $0x0,%rax
jne    135e <getconfigstr+0x1be>
lea    -0x1040(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1359 <getconfigstr+0x1b9>
mov    -0x20(%rbp),%rdi
lea    -0x1840(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    137d <getconfigstr+0x1dd>
jmp    135e <getconfigstr+0x1be>
jmp    1363 <getconfigstr+0x1c3>
jmp    1368 <getconfigstr+0x1c8>
jmp    1240 <getconfigstr+0xa0>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getconfigstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
mov    %r8,%rbx
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0x820(%rsp),%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x1820(%rsp),%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x20(%rsp),%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x1020(%rsp),%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xdec(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1240 <getconfigstr+0xa0>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
mov    $0xfffffffe,%r12d
test   %eax,%eax
je     125b <getconfigstr+0xbb>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1246 <getconfigstr+0xa6>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x2028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,0x18(%rsp)
mov    %r15,0x10(%rsp)
mov    %r14d,0xc(%rsp)
xor    %r15d,%r15d
lea    0x1020(%rsp),%r14
jmp    129c <getconfigstr+0xfc>
lea    0x820(%rsp),%rsi
call   1090 <getsection@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     12f2 <getconfigstr+0x152>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    1236 <getconfigstr+0x96>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <readline@plt>
cmp    $0xffffffff,%eax
je     1236 <getconfigstr+0x96>
mov    %r14,%rdi
call   10c0 <isremark@plt>
test   %eax,%eax
jne    1290 <getconfigstr+0xf0>
mov    %r14,%rdi
test   %r15d,%r15d
je     1277 <getconfigstr+0xd7>
lea    0x1820(%rsp),%rbp
mov    %rbp,%rsi
lea    0x20(%rsp),%rdx
call   10a0 <getkey@plt>
mov    $0x1,%r15d
test   %rax,%rax
jne    1290 <getconfigstr+0xf0>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1290 <getconfigstr+0xf0>
jmp    1310 <getconfigstr+0x170>
lea    0x820(%rsp),%rdi
mov    0x18(%rsp),%rsi
call   1070 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
sete   %r15b
jmp    1290 <getconfigstr+0xf0>
lea    0x20(%rsp),%rsi
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%edx
call   1030 <strncpy@plt>
xor    %r12d,%r12d
jmp    1236 <getconfigstr+0x96>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @getconfigstr(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca [2048 x i8]
%15 = alloca [2048 x i8]
%16 = alloca [2048 x i8]
%17 = alloca [2048 x i8]
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32 0, i32* %12
%18 = bitcast [2048 x i8]* %14 to i8*
%19 = bitcast [2048 x i8]* %15 to i8*
%20 = bitcast [2048 x i8]* %16 to i8*
%21 = bitcast [2048 x i8]* %17 to i8*
%22 = load i8*, i8** %11
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %13
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %5
store i32 -1, i32* %6
br label %85
26: ; preds = %5
br label %27
27: ; preds = %81, %26
%28 = load i32*, i32** %13
%29 = call i32 @feof(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %82
32: ; preds = %27
%33 = getelementptr inbounds [2048 x i8], [2048 x i8]* %17, i64 0, i64 0
%34 = load i32*, i32** %13
%35 = call i32 @readline(i8* %33, i32* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32*, i32** %13
%39 = call i32 @fclose(i32* %38)
store i32 -2, i32* %6
br label %85
40: ; preds = %32
%41 = getelementptr inbounds [2048 x i8], [2048 x i8]* %17, i64 0, i64 0
%42 = call i32 @isremark(i8* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %81, label %44
44: ; preds = %40
%45 = load i32, i32* %12
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %60
47: ; preds = %44
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %17, i64 0, i64 0
%49 = getelementptr inbounds [2048 x i8], [2048 x i8]* %14, i64 0, i64 0
%50 = call i64 @getsection(i8* %48, i8* %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = getelementptr inbounds [2048 x i8], [2048 x i8]* %14, i64 0, i64 0
%54 = load i8*, i8** %7
%55 = call i64 @strcmp(i8* %53, i8* %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %52
store i32 1, i32* %12
br label %58
58: ; preds = %57, %52
br label %59
59: ; preds = %58, %47
br label %80
60: ; preds = %44
%61 = getelementptr inbounds [2048 x i8], [2048 x i8]* %17, i64 0, i64 0
%62 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%63 = getelementptr inbounds [2048 x i8], [2048 x i8]* %16, i64 0, i64 0
%64 = call i64 @getkey(i8* %61, i8* %62, i8* %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %79
66: ; preds = %60
%67 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%68 = load i8*, i8** %8
%69 = call i64 @strcmp(i8* %67, i8* %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %66
%72 = load i8*, i8** %9
%73 = getelementptr inbounds [2048 x i8], [2048 x i8]* %16, i64 0, i64 0
%74 = load i32, i32* %10
%75 = call i32 @strncpy(i8* %72, i8* %73, i32 %74)
%76 = load i32*, i32** %13
%77 = call i32 @fclose(i32* %76)
store i32 0, i32* %6
br label %85
78: ; preds = %66
br label %79
79: ; preds = %78, %60
br label %80
80: ; preds = %79, %59
br label %81
81: ; preds = %80, %40
br label %27
82: ; preds = %27
%83 = load i32*, i32** %13
%84 = call i32 @fclose(i32* %83)
store i32 -2, i32* %6
br label %85
85: ; preds = %82, %71, %37, %25
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @getconfigstr(i8* %0, i8* %1, i8* %2, i32 %3, i8* nocapture %4) {
%6 = alloca [2048 x i8]
%7 = alloca [2048 x i8]
%8 = alloca [2048 x i8]
%9 = alloca [2048 x i8]
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%14 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %50, label %16
16: ; preds = %5
%17 = call i32 @feof(i32* %14)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %16, %43
%20 = phi i32 [ %44, %43 ], [ 0, %16 ]
%21 = call i32 @readline(i8* %13, i32* %14)
%22 = icmp eq i32 %21, -1
br i1 %22, label %47, label %23
23: ; preds = %19
%24 = call i32 @isremark(i8* %13)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %43
26: ; preds = %23
%27 = icmp eq i32 %20, 0
br i1 %27, label %28, label %35
28: ; preds = %26
%29 = call i64 @getsection(i8* %13, i8* %10)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %43
31: ; preds = %28
%32 = call i64 @strcmp(i8* %10, i8* %0)
%33 = icmp eq i64 %32, 0
%34 = zext i1 %33 to i32
br label %43
35: ; preds = %26
%36 = call i64 @getkey(i8* %13, i8* %11, i8* %12)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = call i64 @strcmp(i8* %11, i8* %1)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @strncpy(i8* %2, i8* %12, i32 %3)
br label %47
43: ; preds = %31, %28, %38, %35, %23
%44 = phi i32 [ %20, %23 ], [ 0, %28 ], [ 1, %38 ], [ 1, %35 ], [ %34, %31 ]
%45 = call i32 @feof(i32* %14)
%46 = icmp eq i32 %45, 0
br i1 %46, label %19, label %47
47: ; preds = %43, %19, %16, %41
%48 = phi i32 [ 0, %41 ], [ -2, %16 ], [ -2, %19 ], [ -2, %43 ]
%49 = call i32 @fclose(i32* %14)
br label %50
50: ; preds = %47, %5
%51 = phi i32 [ -1, %5 ], [ %48, %47 ]
ret i32 %51
}