#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 *, char *);
char *strchr(char *, char);
scalar_t__ strcmp(char const *, char *);
char *strdup(char *);
char *strtok(char *, char *);
char *find_key(const char *key) {
  FILE *f = fopen("/system/build.prop", "r");
  char buf[1024];
  char *mkey, *value;
  if (!f)
    return NULL;
  while (fgets(buf, 1024, f) != NULL) {
    if (strchr(buf, '\r'))
      *(strchr(buf, '\r')) = '\0';
    if (strchr(buf, '\n'))
      *(strchr(buf, '\n')) = '\0';
    mkey = strtok(buf, "=");
    if (!mkey)
      continue;
    value = strtok(NULL, "=");
    if (!value)
      continue;
    if (strcmp(key, mkey) == 0) {
      fclose(f);
      return strdup(value);
    }
  }
  fclose(f);
  return NULL;
}


x86 assembly (clang, -O0):
<find_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <find_key+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    12d5 <find_key+0x165>
jmp    11b3 <find_key+0x43>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12c4 <find_key+0x154>
lea    -0x420(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1201 <find_key+0x91>
lea    -0x420(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
movb   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1230 <find_key+0xc0>
lea    -0x420(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
movb   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    125d <find_key+0xed>
jmp    11b3 <find_key+0x43>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdad(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    1287 <find_key+0x117>
jmp    11b3 <find_key+0x43>
mov    -0x10(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12bf <find_key+0x14f>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x430(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12d5 <find_key+0x165>
jmp    11b3 <find_key+0x43>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
lea    0xe75(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     125e <find_key+0xee>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1256 <find_key+0xe6>
mov    %rsp,%r13
lea    0xe4f(%rip),%r12
jmp    11e5 <find_key+0x75>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1256 <find_key+0xe6>
mov    %r13,%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11fa <find_key+0x8a>
movb   $0x0,(%rax)
mov    %r13,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     120f <find_key+0x9f>
movb   $0x0,(%rax)
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     11d0 <find_key+0x60>
mov    %rax,%rbp
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     11d0 <find_key+0x60>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d0 <find_key+0x60>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1090 <strdup@plt>
jmp    1260 <find_key+0xf0>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/system/build.prop\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"=\00"
define i8* @find_key(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 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** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %57
12: ; preds = %1
br label %13
13: ; preds = %53, %42, %37, %12
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i32* @fgets(i8* %14, i32 1024, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %54
18: ; preds = %13
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = call i8* @strchr(i8* %19, i8 signext 13)
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i8* @strchr(i8* %23, i8 signext 13)
store i8 0, i8* %24
br label %25
25: ; preds = %22, %18
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = call i8* @strchr(i8* %26, i8 signext 10)
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%31 = call i8* @strchr(i8* %30, i8 signext 10)
store i8 0, i8* %31
br label %32
32: ; preds = %29, %25
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = call i8* @strtok(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %38, label %37
37: ; preds = %32
br label %13
38: ; preds = %32
%39 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %39, i8** %7
%40 = load i8*, i8** %7
%41 = icmp ne i8* %40, null
br i1 %41, label %43, label %42
42: ; preds = %38
br label %13
43: ; preds = %38
%44 = load i8*, i8** %3
%45 = load i8*, i8** %6
%46 = call i64 @strcmp(i8* %44, i8* %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i32*, i32** %4
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %7
%52 = call i8* @strdup(i8* %51)
store i8* %52, i8** %2
br label %57
53: ; preds = %43
br label %13
54: ; preds = %13
%55 = load i32*, i32** %4
%56 = call i32 @fclose(i32* %55)
store i8* null, i8** %2
br label %57
57: ; preds = %54, %48, %11
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [19 x i8] c"/system/build.prop\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"=\00"
define noalias i8* @find_key(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = icmp eq i32* %3, null
br i1 %5, label %34, label %6
6: ; preds = %1
%7 = call i32* @fgets(i8* %4, i32 1024, i32* %3)
%8 = icmp eq i32* %7, null
br i1 %8, label %32, label %9
9: ; preds = %6, %20
%10 = call i8* @strchr(i8* %4, i8 signext 13)
%11 = icmp eq i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %9
store i8 0, i8* %10
br label %13
13: ; preds = %12, %9
%14 = call i8* @strchr(i8* %4, i8 signext 10)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13
store i8 0, i8* %14
br label %17
17: ; preds = %16, %13
%18 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17, %23, %26
%21 = call i32* @fgets(i8* %4, i32 1024, i32* %3)
%22 = icmp eq i32* %21, null
br i1 %22, label %32, label %9
23: ; preds = %17
%24 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %20, label %26
26: ; preds = %23
%27 = call i64 @strcmp(i8* %0, i8* %18)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %20
29: ; preds = %26
%30 = call i32 @fclose(i32* %3)
%31 = call i8* @strdup(i8* %24)
br label %34
32: ; preds = %20, %6
%33 = call i32 @fclose(i32* %3)
br label %34
34: ; preds = %1, %32, %29
%35 = phi i8* [ %31, %29 ], [ null, %32 ], [ null, %1 ]
ret i8* %35
}