#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*/ *);
scalar_t__ isCommentLine(char *);
scalar_t__ isLabel(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
char *strtok(char *, char *);
int /*<<< orphan*/ trim(char *);
int readKV(FILE *file, const char *key, char *value) {
  char buffer[1024];
  memset(buffer, 0, 1024);
  int key_len = strlen(key);
  while (fgets(buffer, 1024, file) != NULL) {
    if (isCommentLine(buffer)) {
      continue;
    }
    if (isLabel(buffer)) {
      break;
    }
    char *t_key = strtok(buffer, "=");
    if (t_key == NULL) {
      continue;
    }
    trim(t_key);
    if (strlen(t_key) != key_len || strcmp(t_key, key) != 0) {
      continue;
    }
    char *t_value = strtok(NULL, "#");
    if (t_value == NULL) {
      fclose(file);
      return -1;
    }
    trim(t_value);
    if (strlen(t_value) == 0) {
      fclose(file);
      return -1;
    }
    strcpy(value, t_value);
    fclose(file);
    return 0;
  }
  fclose(file);
  return -1;
}


x86 assembly (clang, -O0):
<readKV>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1333 <readKV+0x193>
lea    -0x420(%rbp),%rdi
call   10b0 <isCommentLine@plt>
cmp    $0x0,%rax
je     1213 <readKV+0x73>
jmp    11d9 <readKV+0x39>
lea    -0x420(%rbp),%rdi
call   10a0 <isLabel@plt>
cmp    $0x0,%rax
je     122e <readKV+0x8e>
jmp    1333 <readKV+0x193>
lea    -0x420(%rbp),%rdi
lea    0xdc4(%rip),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    125b <readKV+0xbb>
jmp    11d9 <readKV+0x39>
mov    -0x430(%rbp),%rdi
call   1060 <trim@plt>
mov    -0x430(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x424(%rbp),%eax
jne    1299 <readKV+0xf9>
mov    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     129e <readKV+0xfe>
jmp    11d9 <readKV+0x39>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd59(%rip),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    12d8 <readKV+0x138>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <readKV+0x1a3>
mov    -0x438(%rbp),%rdi
call   1060 <trim@plt>
mov    -0x438(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jne    130e <readKV+0x16e>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <readKV+0x1a3>
mov    -0x20(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1343 <readKV+0x1a3>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readKV>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12bb <readKV+0x11b>
mov    %r14,0x8(%rsp)
lea    0x10(%rsp),%rbp
lea    0xdfd(%rip),%r14
jmp    1229 <readKV+0x89>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12bb <readKV+0x11b>
mov    %rbp,%rdi
call   10b0 <isCommentLine@plt>
test   %rax,%rax
jne    1210 <readKV+0x70>
mov    %rbp,%rdi
call   10a0 <isLabel@plt>
test   %rax,%rax
jne    12bb <readKV+0x11b>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10c0 <strtok@plt>
test   %rax,%rax
je     1210 <readKV+0x70>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <trim@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    %r13d,%eax
jne    1210 <readKV+0x70>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1210 <readKV+0x70>
lea    0xd80(%rip),%rsi
xor    %edi,%edi
call   10c0 <strtok@plt>
test   %rax,%rax
mov    $0xffffffff,%ebp
je     12c0 <readKV+0x120>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <trim@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     12c0 <readKV+0x120>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
jmp    12c0 <readKV+0x120>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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