#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int line_get_value(char **, char *);
int /*<<< orphan*/ line_starts_with(char *, char const *);
int deviceinfo_from_deviceinfo(char **dest, const char *key) {
  FILE *f;
  char *line = NULL;
  size_t len = 0;
  ssize_t c;
  int rv = -2;
  f = fopen("/etc/device_info", "rb");
  if (f == NULL)
    return -1;
  while ((c = getline(&line, &len, f)) != -1) {
    if (!line_starts_with(line, key))
      continue;
    c = line_get_value(dest, line);
    if (c < 0)
      continue;
    rv = c;
    break;
  }
  fclose(f);
  if (line)
    free(line);
  return rv;
}


x86 assembly (clang, -O0):
<deviceinfo_from_deviceinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xfffffffe,-0x38(%rbp)
lea    0xe72(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <deviceinfo_from_deviceinfo+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <deviceinfo_from_deviceinfo+0xda>
jmp    11ba <deviceinfo_from_deviceinfo+0x5a>
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <getline@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
je     1217 <deviceinfo_from_deviceinfo+0xb7>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <line_starts_with@plt>
cmp    $0x0,%eax
jne    11f2 <deviceinfo_from_deviceinfo+0x92>
jmp    11ba <deviceinfo_from_deviceinfo+0x5a>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <line_get_value@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1211 <deviceinfo_from_deviceinfo+0xb1>
jmp    11ba <deviceinfo_from_deviceinfo+0x5a>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1234 <deviceinfo_from_deviceinfo+0xd4>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deviceinfo_from_deviceinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe73(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1221 <deviceinfo_from_deviceinfo+0xc1>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
mov    $0xfffffffe,%r14d
cmp    $0xffffffff,%eax
je     1208 <deviceinfo_from_deviceinfo+0xa8>
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    11e3 <deviceinfo_from_deviceinfo+0x83>
xchg   %ax,%ax
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1208 <deviceinfo_from_deviceinfo+0xa8>
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
call   1050 <line_starts_with@plt>
test   %eax,%eax
je     11d0 <deviceinfo_from_deviceinfo+0x70>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <line_get_value@plt>
test   %eax,%eax
js     11d0 <deviceinfo_from_deviceinfo+0x70>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1227 <deviceinfo_from_deviceinfo+0xc7>
call   1030 <free@plt>
jmp    1227 <deviceinfo_from_deviceinfo+0xc7>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/device_info\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @deviceinfo_from_deviceinfo(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* null, i8** %7
store i64 0, i64* %8
store i32 -2, i32* %10
%11 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %45
15: ; preds = %2
br label %16
16: ; preds = %32, %25, %15
%17 = load i32*, i32** %6
%18 = call i32 @getline(i8** %7, i64* %8, i32* %17)
store i32 %18, i32* %9
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %35
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = load i8*, i8** %5
%23 = call i32 @line_starts_with(i8* %21, i8* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %20
br label %16
26: ; preds = %20
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %7
%29 = call i32 @line_get_value(i8** %27, i8* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %16
33: ; preds = %26
%34 = load i32, i32* %9
store i32 %34, i32* %10
br label %35
35: ; preds = %33, %16
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i8*, i8** %7
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %35
%44 = load i32, i32* %10
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %14
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/device_info\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @deviceinfo_from_deviceinfo(i8** %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %30, label %9
9: ; preds = %2
%10 = call i32 @getline(i8** %3, i64* %4, i32* %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %23, label %12
12: ; preds = %9, %16
%13 = load i8*, i8** %3
%14 = call i32 @line_starts_with(i8* %13, i8* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12, %19
%17 = call i32 @getline(i8** %3, i64* %4, i32* %7)
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %12
19: ; preds = %12
%20 = load i8*, i8** %3
%21 = call i32 @line_get_value(i8** %0, i8* %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %16, label %23
23: ; preds = %16, %19, %9
%24 = phi i32 [ -2, %9 ], [ %21, %19 ], [ -2, %16 ]
%25 = call i32 @fclose(i32* %7)
%26 = load i8*, i8** %3
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = call i32 @free(i8* %26)
br label %30
30: ; preds = %23, %28, %2
%31 = phi i32 [ -1, %2 ], [ %24, %28 ], [ %24, %23 ]
ret i32 %31
}