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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logMallocError();
char *strdup(char *);
size_t strlen(char const *);
scalar_t__ strncmp(char *, char const *, size_t);
char *strtok(char *, char *);
char *getBootParameters(const char *name) {
  size_t nameLength = strlen(name);
  char *parameters;
  if ((parameters = strdup(""))) {
    const char *path = "/proc/cmdline";
    FILE *file;
    if ((file = fopen(path, "r"))) {
      char buffer[0X1000];
      char *line = fgets(buffer, sizeof(buffer), file);
      if (line) {
        char *token;
        while ((token = strtok(line, " \n"))) {
          line = NULL;
          if ((strncmp(token, name, nameLength) == 0) &&
              (token[nameLength] == '=')) {
            char *newParameters = strdup(token + nameLength + 1);
            if (newParameters) {
              free(parameters);
              parameters = newParameters;
            } else {
              logMallocError();
            }
          }
        }
      }
      fclose(file);
    }
  } else {
    logMallocError();
  }
  return parameters;
}


x86 assembly (clang, -O0):
<getBootParameters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe5f(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12e9 <getBootParameters+0x159>
lea    0xe33(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12e4 <getBootParameters+0x154>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1000,%esi
call   1070 <fgets@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
je     12db <getBootParameters+0x14b>
jmp    121e <getBootParameters+0x8e>
mov    -0x1038(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x1040(%rbp)
cmp    $0x0,%rax
je     12d6 <getBootParameters+0x146>
movq   $0x0,-0x1038(%rbp)
mov    -0x1040(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12d1 <getBootParameters+0x141>
mov    -0x1040(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3d,%eax
jne    12d1 <getBootParameters+0x141>
mov    -0x1040(%rbp),%rdi
add    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x1048(%rbp)
cmpq   $0x0,-0x1048(%rbp)
je     12c5 <getBootParameters+0x135>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1048(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    12cc <getBootParameters+0x13c>
mov    $0x0,%al
call   10a0 <logMallocError@plt>
jmp    12d1 <getBootParameters+0x141>
jmp    121e <getBootParameters+0x8e>
jmp    12db <getBootParameters+0x14b>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
jmp    12f0 <getBootParameters+0x160>
mov    $0x0,%al
call   10a0 <logMallocError@plt>
mov    -0x18(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getBootParameters>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%r15
call   1060 <strlen@plt>
mov    %rax,%rbx
lea    0xe5f(%rip),%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
je     1218 <getBootParameters+0x88>
mov    %rax,%r14
lea    0xe39(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1288 <getBootParameters+0xf8>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1280 <getBootParameters+0xf0>
lea    0xe11(%rip),%rsi
mov    %rax,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1280 <getBootParameters+0xf0>
mov    %rax,%rbp
lea    0xdfa(%rip),%r13
jmp    1242 <getBootParameters+0xb2>
xor    %r14d,%r14d
xor    %eax,%eax
call   10a0 <logMallocError@plt>
jmp    1288 <getBootParameters+0xf8>
xor    %eax,%eax
call   10a0 <logMallocError@plt>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r13,%rsi
call   1090 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1280 <getBootParameters+0xf0>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1230 <getBootParameters+0xa0>
cmpb   $0x3d,0x0(%rbp,%rbx,1)
jne    1230 <getBootParameters+0xa0>
add    %rbx,%rbp
add    $0x1,%rbp
mov    %rbp,%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
je     1224 <getBootParameters+0x94>
mov    %rax,%rbp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%r14
jmp    1230 <getBootParameters+0xa0>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [14 x i8] c"/proc/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c" \0A\00"
define noalias i8* @getBootParameters(i8* %0) {
%2 = alloca [4096 x i8]
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %40, label %6
6: ; preds = %1
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %42, label %9
9: ; preds = %6
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%11 = call i8* @fgets(i8* %10, i32 4096, i32* %7)
%12 = icmp eq i8* %11, null
br i1 %12, label %37, label %13
13: ; preds = %9
%14 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %37, label %16
16: ; preds = %13, %33
%17 = phi i8* [ %35, %33 ], [ %14, %13 ]
%18 = phi i8* [ %34, %33 ], [ %4, %13 ]
%19 = call i64 @strncmp(i8* %17, i8* %0, i64 %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %17, i64 %3
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 61
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = getelementptr inbounds i8, i8* %22, i64 1
%27 = call i8* @strdup(i8* %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @free(i8* %18)
br label %33
31: ; preds = %25
%32 = call i32 (...) @logMallocError()
br label %33
33: ; preds = %29, %31, %21, %16
%34 = phi i8* [ %18, %21 ], [ %18, %16 ], [ %27, %29 ], [ %18, %31 ]
%35 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %16
37: ; preds = %33, %13, %9
%38 = phi i8* [ %4, %9 ], [ %4, %13 ], [ %34, %33 ]
%39 = call i32 @fclose(i32* %7)
br label %42
40: ; preds = %1
%41 = tail call i32 (...) @logMallocError()
br label %42
42: ; preds = %6, %37, %40
%43 = phi i8* [ null, %40 ], [ %38, %37 ], [ %4, %6 ]
ret i8* %43
}