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

/* Variables and functions */
int /*<<< orphan*/ debug(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strchr(char *, char);
size_t strcspn(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(char *, char *, int);
int strlen(char const *);
char *strstr(char *, char const *);
char const *get_cmdline(char const *name) {
  FILE *fh = fopen("/proc/cmdline", "r");
  if (!fh) {
    debug("Error opening /proc/cmdline: %s", strerror(errno));
    return NULL;
  }
  int len = strlen(name);
  char *r = NULL, buf[1024];
  static char val[256];
  if (fgets(buf, 1024, fh)) {
    r = buf;
    while ((r = strstr(r, name))) {
      if (r == buf || r[-1] == ' ') {
        if (r[len] == '=') {
          r += len + 1;
          break;
        } else if (strchr("\n ", r[len]) || r[len] == '\0') {
          r += len;
          break;
        }
      }
      r++;
    }
    if (r) {
      strlcpy(val, r, sizeof(val));
      val[strcspn(val, " \n")] = '\0';
      r = val;
    }
  }
  if (ferror(fh)) {
    debug("Error reading /proc/cmdline: %s", strerror(errno));
  }
  fclose(fh);
  return r;
}


x86 assembly (clang, -O0):
<get_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe3a(%rip),%rdi
lea    0xe41(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120a <get_cmdline+0x5a>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe18(%rip),%rdi
call   1060 <debug@plt>
movq   $0x0,-0x8(%rbp)
jmp    13af <get_cmdline+0x1ff>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     136f <get_cmdline+0x1bf>
lea    -0x430(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     131e <get_cmdline+0x16e>
mov    -0x28(%rbp),%rax
lea    -0x430(%rbp),%rcx
cmp    %rcx,%rax
je     1288 <get_cmdline+0xd8>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x20,%eax
jne    130d <get_cmdline+0x15d>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3d,%eax
jne    12b6 <get_cmdline+0x106>
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    131e <get_cmdline+0x16e>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xd6b(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1070 <strchr@plt>
cmp    $0x0,%rax
jne    12ed <get_cmdline+0x13d>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1303 <get_cmdline+0x153>
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    131e <get_cmdline+0x16e>
jmp    1308 <get_cmdline+0x158>
jmp    130d <get_cmdline+0x15d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1248 <get_cmdline+0x98>
cmpq   $0x0,-0x28(%rbp)
je     136a <get_cmdline+0x1ba>
mov    -0x28(%rbp),%rsi
lea    0x2d6c(%rip),%rdi
mov    $0x100,%edx
call   10a0 <strlcpy@plt>
lea    0x2d5b(%rip),%rdi
lea    0xce7(%rip),%rsi
call   1080 <strcspn@plt>
mov    %rax,%rcx
lea    0x2d45(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2d3a(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    136f <get_cmdline+0x1bf>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     139e <get_cmdline+0x1ee>
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xc9d(%rip),%rdi
call   1060 <debug@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r13
lea    0xe35(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     126f <get_cmdline+0xbf>
mov    %rax,%r15
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     126b <get_cmdline+0xbb>
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r13,%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
je     126b <get_cmdline+0xbb>
mov    %rax,%rbx
movslq %ebp,%r14
jmp    1237 <get_cmdline+0x87>
nopl   (%rax)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   10d0 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     126b <get_cmdline+0xbb>
cmp    %r12,%rbx
je     1242 <get_cmdline+0x92>
cmpb   $0x20,-0x1(%rbx)
jne    1220 <get_cmdline+0x70>
movzbl (%rbx,%r14,1),%eax
cmp    $0x3d,%al
je     128f <get_cmdline+0xdf>
lea    (%rbx,%r14,1),%rbp
movsbl %al,%esi
lea    0xdd7(%rip),%rdi
call   1070 <strchr@plt>
test   %rax,%rax
jne    1297 <get_cmdline+0xe7>
cmpb   $0x0,0x0(%rbp)
jne    1220 <get_cmdline+0x70>
jmp    1297 <get_cmdline+0xe7>
xor    %ebx,%ebx
jmp    12c1 <get_cmdline+0x111>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd8c(%rip),%rdi
mov    %eax,%esi
call   1060 <debug@plt>
xor    %ebx,%ebx
jmp    12f2 <get_cmdline+0x142>
lea    (%r14,%rbx,1),%rbp
add    $0x1,%rbp
lea    0x2e02(%rip),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x100,%edx
call   10a0 <strlcpy@plt>
lea    0xd7e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcspn@plt>
movb   $0x0,(%rax,%rbx,1)
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     12ea <get_cmdline+0x13a>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd53(%rip),%rdi
mov    %eax,%esi
call   1060 <debug@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"Error opening /proc/cmdline: %s\00"
@errno = global i32 0
@get_cmdline.val = internal global [256 x i8] zeroinitializer
@.str.3 = [3 x i8] c"\0A \00"
@.str.4 = [3 x i8] c" \0A\00"
@.str.5 = [32 x i8] c"Error reading /proc/cmdline: %s\00"
define i8* @get_cmdline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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 %15, label %11
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i8* null, i8** %2
br label %100
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %5
store i8* null, i8** %6
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %4
%20 = call i64 @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %88
22: ; preds = %15
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
store i8* %23, i8** %6
br label %24
24: ; preds = %76, %22
%25 = load i8*, i8** %6
%26 = load i8*, i8** %3
%27 = call i8* @strstr(i8* %25, i8* %26)
store i8* %27, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %79
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = icmp eq i8* %30, %31
br i1 %32, label %39, label %33
33: ; preds = %29
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i64 -1
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 32
br i1 %38, label %39, label %76
39: ; preds = %33, %29
%40 = load i8*, i8** %6
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 61
br i1 %46, label %47, label %53
47: ; preds = %39
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
%50 = load i8*, i8** %6
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %6
br label %79
53: ; preds = %39
%54 = load i8*, i8** %6
%55 = load i32, i32* %5
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = call i64 @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8 signext %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %69, label %61
61: ; preds = %53
%62 = load i8*, i8** %6
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %61, %53
%70 = load i32, i32* %5
%71 = load i8*, i8** %6
%72 = sext i32 %70 to i64
%73 = getelementptr inbounds i8, i8* %71, i64 %72
store i8* %73, i8** %6
br label %79
74: ; preds = %61
br label %75
75: ; preds = %74
br label %76
76: ; preds = %75, %33
%77 = load i8*, i8** %6
%78 = getelementptr inbounds i8, i8* %77, i32 1
store i8* %78, i8** %6
br label %24
79: ; preds = %69, %47, %24
%80 = load i8*, i8** %6
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %87
82: ; preds = %79
%83 = load i8*, i8** %6
%84 = call i32 @strlcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 0), i8* %83, i32 256)
%85 = call i64 @strcspn(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%86 = getelementptr inbounds [256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 %85
store i8 0, i8* %86
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 0), i8** %6
br label %87
87: ; preds = %82, %79
br label %88
88: ; preds = %87, %15
%89 = load i32*, i32** %4
%90 = call i64 @ferror(i32* %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %88
%93 = load i32, i32* @errno
%94 = call i32 @strerror(i32 %93)
%95 = call i32 @debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %94)
br label %96
96: ; preds = %92, %88
%97 = load i32*, i32** %4
%98 = call i32 @fclose(i32* %97)
%99 = load i8*, i8** %6
store i8* %99, i8** %2
br label %100
100: ; preds = %96, %11
%101 = load i8*, i8** %2
ret i8* %101
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"Error opening /proc/cmdline: %s\00"
@errno = global i32 0
@get_cmdline.val = internal global [256 x i8] zeroinitializer
@.str.3 = [3 x i8] c"\0A \00"
@.str.4 = [3 x i8] c" \0A\00"
@.str.5 = [32 x i8] c"Error reading /proc/cmdline: %s\00"
define i8* @get_cmdline(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %59
9: ; preds = %1
%10 = tail call i32 @strlen(i8* %0)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = call i64 @fgets(i8* %11, i32 1024, i32* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %49, label %14
14: ; preds = %9
%15 = call i8* @strstr(i8* dereferenceable(1) %11, i8* dereferenceable(1) %0)
%16 = icmp eq i8* %15, null
br i1 %16, label %49, label %17
17: ; preds = %14
%18 = sext i32 %10 to i64
br label %19
19: ; preds = %17, %36
%20 = phi i8* [ %15, %17 ], [ %38, %36 ]
%21 = icmp eq i8* %20, %11
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %20, i64 -1
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 32
br i1 %25, label %26, label %36
26: ; preds = %22, %19
%27 = getelementptr inbounds i8, i8* %20, i64 %18
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 61
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = call i64 @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8 signext %28)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %30
%34 = load i8, i8* %27
%35 = icmp eq i8 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %33, %22
%37 = getelementptr inbounds i8, i8* %20, i64 1
%38 = call i8* @strstr(i8* dereferenceable(1) %37, i8* dereferenceable(1) %0)
%39 = icmp eq i8* %38, null
br i1 %39, label %49, label %19
40: ; preds = %26
%41 = add nsw i32 %10, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %20, i64 %42
br label %44
44: ; preds = %30, %33, %40
%45 = phi i8* [ %43, %40 ], [ %27, %33 ], [ %27, %30 ]
%46 = call i32 @strlcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 0), i8* %45, i32 256)
%47 = call i64 @strcspn(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%48 = getelementptr inbounds [256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %36, %14, %44, %9
%50 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @get_cmdline.val, i64 0, i64 0), %44 ], [ null, %9 ], [ null, %14 ], [ null, %36 ]
%51 = call i64 @ferror(i32* %3)
%52 = icmp eq i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = call i32 @strerror(i32 %54)
%56 = call i32 @debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %53, %49
%58 = call i32 @fclose(i32* %3)
br label %59
59: ; preds = %57, %5
%60 = phi i8* [ %50, %57 ], [ null, %5 ]
ret i8* %60
}