#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*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *getenv(char *);
int /*<<< orphan*/ log_msg(char *, char const *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
int get_cmd_line_value(char *get_value, int max_len, char *key) {
  const char *cmdline_path;
  FILE *cmdline_file;
  char cmdline[1024];
  int ret = -1;
  int keylen;
  char *key_and_value;
  char *value;
  cmdline_path = getenv("CMDLINE_PATH");
  cmdline_path = cmdline_path ? cmdline_path : "/proc/cmdline";
  cmdline_file = fopen(cmdline_path, "r");
  if (!cmdline_file) {
    log_msg("Could not open %s\n", cmdline_path);
    return -1;
  }
  if (fgets(cmdline, 1024, cmdline_file)) {
    key_and_value = strtok(cmdline, " ,");
    keylen = strlen(key);
    while (key_and_value != NULL) {
      if (!strncmp(key_and_value, key, keylen)) {
        value = strtok(key_and_value, "=");
        value = strtok(NULL, "=");
        if (value) {
          strncpy(get_value, value, max_len);
          ret = 0;
        }
        break;
      }
      key_and_value = strtok(NULL, " ,");
    }
  }
  fclose(cmdline_file);
  return ret;
}


x86 assembly (clang, -O0):
<get_cmd_line_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x434(%rbp)
ff ff ff
lea    0xe49(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11db <get_cmd_line_value+0x4b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x450(%rbp)
jmp    11ee <get_cmd_line_value+0x5e>
lea    0xe2b(%rip),%rax
mov    %rax,-0x450(%rbp)
jmp    11ee <get_cmd_line_value+0x5e>
mov    -0x450(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1234 <get_cmd_line_value+0xa4>
mov    -0x28(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1090 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <get_cmd_line_value+0x1ad>
lea    -0x430(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     132b <get_cmd_line_value+0x19b>
lea    -0x430(%rbp),%rdi
lea    0xdcf(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x440(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x438(%rbp)
cmpq   $0x0,-0x440(%rbp)
je     1326 <get_cmd_line_value+0x196>
mov    -0x440(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x438(%rbp),%edx
call   1050 <strncmp@plt>
cmp    $0x0,%eax
jne    130a <get_cmd_line_value+0x17a>
mov    -0x440(%rbp),%rdi
lea    0xd7c(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x448(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd65(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
je     1305 <get_cmd_line_value+0x175>
mov    -0x10(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
movl   $0x0,-0x434(%rbp)
jmp    1326 <get_cmd_line_value+0x196>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd1b(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x440(%rbp)
jmp    127c <get_cmd_line_value+0xec>
jmp    132b <get_cmd_line_value+0x19b>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmd_line_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe4f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe4d(%rip),%rbx
cmovne %rax,%rbx
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1266 <get_cmd_line_value+0xd6>
mov    %rax,%r13
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     12b2 <get_cmd_line_value+0x122>
mov    %r14,0x8(%rsp)
lea    0xe24(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10b0 <strtok@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1070 <strlen@plt>
test   %rbx,%rbx
je     12b2 <get_cmd_line_value+0x122>
mov    %eax,%r14d
mov    %r15d,0x4(%rsp)
lea    0xdf7(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <strncmp@plt>
test   %eax,%eax
je     127d <get_cmd_line_value+0xed>
xor    %edi,%edi
mov    %r15,%rsi
call   10b0 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1240 <get_cmd_line_value+0xb0>
jmp    12b2 <get_cmd_line_value+0x122>
lea    0xdb0(%rip),%rdi
mov    %rbx,%rsi
call   1090 <log_msg@plt>
mov    $0xffffffff,%r12d
jmp    12ba <get_cmd_line_value+0x12a>
lea    0xdaf(%rip),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10b0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10b0 <strtok@plt>
test   %rax,%rax
je     12b2 <get_cmd_line_value+0x122>
mov    0x8(%rsp),%rdi
mov    %rax,%rsi
mov    0x4(%rsp),%edx
call   1040 <strncpy@plt>
xor    %r12d,%r12d
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r12d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"CMDLINE_PATH\00"
@.str.1 = [14 x i8] c"/proc/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"Could not open %s\0A\00"
@.str.4 = [3 x i8] c" ,\00"
@.str.5 = [2 x i8] c"=\00"
define i32 @get_cmd_line_value(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 -1, i32* %11
%15 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i8*, i8** %8
br label %21
20: ; preds = %3
br label %21
21: ; preds = %20, %18
%22 = phi i8* [ %19, %18 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %20 ]
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %9
%25 = load i32*, i32** %9
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = load i8*, i8** %8
%29 = call i32 @log_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %68
30: ; preds = %21
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%32 = load i32*, i32** %9
%33 = call i64 @fgets(i8* %31, i32 1024, i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %64
35: ; preds = %30
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%37 = call i8* @strtok(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i8* %37, i8** %13
%38 = load i8*, i8** %7
%39 = call i32 @strlen(i8* %38)
store i32 %39, i32* %12
br label %40
40: ; preds = %61, %35
%41 = load i8*, i8** %13
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %63
43: ; preds = %40
%44 = load i8*, i8** %13
%45 = load i8*, i8** %7
%46 = load i32, i32* %12
%47 = call i32 @strncmp(i8* %44, i8* %45, i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %61, label %49
49: ; preds = %43
%50 = load i8*, i8** %13
%51 = call i8* @strtok(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %51, i8** %14
%52 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %52, i8** %14
%53 = load i8*, i8** %14
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i8*, i8** %5
%57 = load i8*, i8** %14
%58 = load i32, i32* %6
%59 = call i32 @strncpy(i8* %56, i8* %57, i32 %58)
store i32 0, i32* %11
br label %60
60: ; preds = %55, %49
br label %63
61: ; preds = %43
%62 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i8* %62, i8** %13
br label %40
63: ; preds = %60, %40
br label %64
64: ; preds = %63, %30
%65 = load i32*, i32** %9
%66 = call i32 @fclose(i32* %65)
%67 = load i32, i32* %11
store i32 %67, i32* %4
br label %68
68: ; preds = %64, %27
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@.str = [13 x i8] c"CMDLINE_PATH\00"
@.str.1 = [14 x i8] c"/proc/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"Could not open %s\0A\00"
@.str.4 = [3 x i8] c" ,\00"
@.str.5 = [2 x i8] c"=\00"
define i32 @get_cmd_line_value(i8* %0, i32 %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %6
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @log_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %36
13: ; preds = %3
%14 = call i64 @fgets(i8* %5, i32 1024, i32* %9)
%15 = icmp eq i64 %14, 0
br i1 %15, label %33, label %16
16: ; preds = %13
%17 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @strlen(i8* %2)
%19 = icmp eq i8* %17, null
br i1 %19, label %33, label %20
20: ; preds = %16, %30
%21 = phi i8* [ %31, %30 ], [ %17, %16 ]
%22 = call i32 @strncmp(i8* %21, i8* %2, i32 %18)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = call i8* @strtok(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%26 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = call i32 @strncpy(i8* %0, i8* %26, i32 %1)
br label %33
30: ; preds = %20
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %20
33: ; preds = %30, %16, %28, %24, %13
%34 = phi i32 [ 0, %28 ], [ -1, %24 ], [ -1, %13 ], [ -1, %16 ], [ -1, %30 ]
%35 = call i32 @fclose(i32* %9)
br label %36
36: ; preds = %33, %11
%37 = phi i32 [ %34, %33 ], [ -1, %11 ]
ret i32 %37
}