#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ isCommentLine(char *);
scalar_t__ isLabel(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readKV(int /*<<< orphan*/ *, char const *, char *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
int configz_readLKV(const char *configfile, const char *label, const char *key,
                    char *value, int num) {
  if (num < 1) {
    return -1;
  }
  FILE *file = fopen(configfile, "r");
  if (file == NULL) {
    return -1;
  }
  char buffer[1024];
  memset(buffer, 0, 1024);
  int label_len = strlen(label);
  int label_num = 0;
  while (fgets(buffer, 1024, file) != NULL) {
    if (isCommentLine(buffer)) {
      continue;
    }
    if (isLabel(buffer) && strlen(buffer) == label_len + 2 &&
        strncmp(buffer + 1, label, label_len) == 0) {
      label_num++;
      if (label_num == num) {
        break;
      }
    }
  }
  if (label_num != num) {
    fclose(file);
    return -1;
  }
  return readKV(file, key, value);
}


x86 assembly (clang, -O0):
<configz_readLKV>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jge    11c5 <configz_readLKV+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <configz_readLKV+0x182>
mov    -0x10(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f0 <configz_readLKV+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <configz_readLKV+0x182>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x444(%rbp)
movl   $0x0,-0x448(%rbp)
lea    -0x440(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12da <configz_readLKV+0x14a>
lea    -0x440(%rbp),%rdi
call   10a0 <isCommentLine@plt>
cmp    $0x0,%rax
je     1256 <configz_readLKV+0xc6>
jmp    121c <configz_readLKV+0x8c>
lea    -0x440(%rbp),%rdi
call   1090 <isLabel@plt>
cmp    $0x0,%rax
je     12d5 <configz_readLKV+0x145>
lea    -0x440(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x444(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jne    12d5 <configz_readLKV+0x145>
lea    -0x440(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x444(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12d5 <configz_readLKV+0x145>
mov    -0x448(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x448(%rbp)
mov    -0x448(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    12d0 <configz_readLKV+0x140>
jmp    12da <configz_readLKV+0x14a>
jmp    12d5 <configz_readLKV+0x145>
jmp    121c <configz_readLKV+0x8c>
mov    -0x448(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     12fe <configz_readLKV+0x16e>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <configz_readLKV+0x182>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <readKV@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<configz_readLKV>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    $0xffffffff,%ebp
test   %r8d,%r8d
jle    129d <configz_readLKV+0x10d>
mov    %r8d,%r13d
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
lea    0xe3e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     129d <configz_readLKV+0x10d>
mov    %rax,%rbx
mov    %r15,0x10(%rsp)
mov    %r14,0x18(%rsp)
lea    0x20(%rsp),%rbp
movl   $0x0,0xc(%rsp)
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %eax,%r14d
add    $0x2,%r14d
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1273 <configz_readLKV+0xe3>
mov    %rbp,%rdi
call   10a0 <isCommentLine@plt>
test   %rax,%rax
jne    1210 <configz_readLKV+0x80>
mov    %rbp,%rdi
call   1090 <isLabel@plt>
test   %rax,%rax
je     1210 <configz_readLKV+0x80>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    %r14d,%eax
jne    1210 <configz_readLKV+0x80>
lea    0x21(%rsp),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1210 <configz_readLKV+0x80>
mov    0xc(%rsp),%eax
add    $0x1,%eax
mov    %eax,0xc(%rsp)
cmp    %r13d,%eax
jne    1210 <configz_readLKV+0x80>
jmp    127a <configz_readLKV+0xea>
cmp    %r13d,0xc(%rsp)
jne    1290 <configz_readLKV+0x100>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
mov    0x18(%rsp),%rdx
call   1080 <readKV@plt>
mov    %eax,%ebp
jmp    129d <configz_readLKV+0x10d>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @configz_readLKV(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca [1024 x i8]
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%16 = load i32, i32* %11
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 -1, i32* %6
br label %78
19: ; preds = %5
%20 = load i8*, i8** %7
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %12
%22 = load i32*, i32** %12
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %6
br label %78
25: ; preds = %19
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 1024)
%28 = load i8*, i8** %8
%29 = call i32 @strlen(i8* %28)
store i32 %29, i32* %14
store i32 0, i32* %15
br label %30
30: ; preds = %65, %39, %25
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%32 = load i32*, i32** %12
%33 = call i32* @fgets(i8* %31, i32 1024, i32* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %66
35: ; preds = %30
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%37 = call i64 @isCommentLine(i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
br label %30
40: ; preds = %35
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%42 = call i64 @isLabel(i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %65
44: ; preds = %40
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = load i32, i32* %14
%48 = add nsw i32 %47, 2
%49 = icmp eq i32 %46, %48
br i1 %49, label %50, label %65
50: ; preds = %44
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = load i8*, i8** %8
%54 = load i32, i32* %14
%55 = call i64 @strncmp(i8* %52, i8* %53, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %65
57: ; preds = %50
%58 = load i32, i32* %15
%59 = add nsw i32 %58, 1
store i32 %59, i32* %15
%60 = load i32, i32* %15
%61 = load i32, i32* %11
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %57
br label %66
64: ; preds = %57
br label %65
65: ; preds = %64, %50, %44, %40
br label %30
66: ; preds = %63, %30
%67 = load i32, i32* %15
%68 = load i32, i32* %11
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = load i32*, i32** %12
%72 = call i32 @fclose(i32* %71)
store i32 -1, i32* %6
br label %78
73: ; preds = %66
%74 = load i32*, i32** %12
%75 = load i8*, i8** %9
%76 = load i8*, i8** %10
%77 = call i32 @readKV(i32* %74, i8* %75, i8* %76)
store i32 %77, i32* %6
br label %78
78: ; preds = %73, %70, %24, %18
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @configz_readLKV(i8* nocapture %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca [1024 x i8]
%7 = icmp slt i32 %4, 1
br i1 %7, label %47, label %8
8: ; preds = %5
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %47, label %11
11: ; preds = %8
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 1024)
%14 = call i32 @strlen(i8* %1)
%15 = add nsw i32 %14, 2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1
br label %17
17: ; preds = %36, %11
%18 = phi i32 [ %37, %36 ], [ 0, %11 ]
br label %19
19: ; preds = %35, %17
br label %20
20: ; preds = %19, %23
%21 = call i32* @fgets(i8* %12, i32 1024, i32* %9)
%22 = icmp eq i32* %21, null
br i1 %22, label %39, label %23
23: ; preds = %20
%24 = call i64 @isCommentLine(i8* %12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %20
26: ; preds = %23
%27 = call i64 @isLabel(i8* %12)
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %26
%30 = call i32 @strlen(i8* %12)
%31 = icmp eq i32 %30, %15
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i64 @strncmp(i8* %16, i8* %1, i32 %14)
%34 = icmp eq i64 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %32, %29, %26
br label %19
36: ; preds = %32
%37 = add nuw nsw i32 %18, 1
%38 = icmp eq i32 %37, %4
br i1 %38, label %43, label %17
39: ; preds = %20
%40 = icmp eq i32 %18, %4
br i1 %40, label %43, label %41
41: ; preds = %39
%42 = call i32 @fclose(i32* %9)
br label %45
43: ; preds = %36, %39
%44 = call i32 @readKV(i32* %9, i8* %2, i8* %3)
br label %45
45: ; preds = %43, %41
%46 = phi i32 [ -1, %41 ], [ %44, %43 ]
br label %47
47: ; preds = %45, %8, %5
%48 = phi i32 [ -1, %5 ], [ %46, %45 ], [ -1, %8 ]
ret i32 %48
}