#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*/ cmdline;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncmp(char const *, char *, unsigned int);
unsigned int strnlen(char const *, int);
char *get_cmdline(const char *param, unsigned int *out_len) {
  static char cmdline[4096];
  unsigned int i;
  unsigned int param_len;
  unsigned int size;
  int r;
  int fd;
  *out_len = 0;
  param_len = strnlen(param, 128);
  if (param_len >= 128)
    return NULL;
  fd = open("/proc/cmdline", O_RDONLY);
  if (fd == -1) {
    printf("open(/proc/cmdline): %s\n", strerror(errno));
    return NULL;
  }
  memset(cmdline, 0, sizeof(cmdline));
  r = read(fd, cmdline, sizeof(cmdline) - 1);
  if (r <= 0 || r >= (int)sizeof(cmdline)) {
    printf("read(/proc/cmdline): %s\n", strerror(errno));
    close(fd);
    return NULL;
  }
  close(fd);
  size = (unsigned int)r;
  i = 0;
  do {
    char c = cmdline[i];
    if (c != param[0]) {
      if (c == '\0' || c == '\n')
        return NULL;
    } else if (!strncmp(param, &cmdline[i], param_len)) {
      unsigned int start = i;
      while (++i < size) {
        c = cmdline[i];
        if (c == '\n' || c == '\0' || c == ' ') {
          unsigned int len = i - start;
          if (len >= sizeof(cmdline) - 1)
            return NULL;
          *out_len = len;
          cmdline[i] = '\0';
          return cmdline + start;
        }
      }
    }
  } while (++i < size);
  return NULL;
}


x86 assembly (clang, -O0):
<get_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x80,%esi
call   1060 <strnlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x80,-0x20(%rbp)
jb     11c5 <get_cmdline+0x45>
movq   $0x0,-0x8(%rbp)
jmp    13e1 <get_cmdline+0x261>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1211 <get_cmdline+0x91>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e1 <get_cmdline+0x261>
lea    0x2e58(%rip),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x2c(%rbp),%edi
lea    0x2e42(%rip),%rsi
mov    $0xfff,%edx
call   1080 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    1252 <get_cmdline+0xd2>
cmpl   $0x1000,-0x28(%rbp)
jl     1284 <get_cmdline+0x104>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdbd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e1 <get_cmdline+0x261>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
lea    0x2dcb(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x2d(%rbp)
movsbl -0x2d(%rbp),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12ea <get_cmdline+0x16a>
movsbl -0x2d(%rbp),%eax
cmp    $0x0,%eax
je     12d8 <get_cmdline+0x158>
movsbl -0x2d(%rbp),%eax
cmp    $0xa,%eax
jne    12e5 <get_cmdline+0x165>
movq   $0x0,-0x8(%rbp)
jmp    13e1 <get_cmdline+0x261>
jmp    13c2 <get_cmdline+0x242>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
lea    0x2d78(%rip),%rsi
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    13bd <get_cmdline+0x23d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    -0x24(%rbp),%eax
jae    13b8 <get_cmdline+0x238>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
lea    0x2d40(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x2d(%rbp)
movsbl -0x2d(%rbp),%eax
cmp    $0xa,%eax
je     135d <get_cmdline+0x1dd>
movsbl -0x2d(%rbp),%eax
cmp    $0x0,%eax
je     135d <get_cmdline+0x1dd>
movsbl -0x2d(%rbp),%eax
cmp    $0x20,%eax
jne    13b3 <get_cmdline+0x233>
mov    -0x1c(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    $0xfff,%rax
jb     1382 <get_cmdline+0x202>
movq   $0x0,-0x8(%rbp)
jmp    13e1 <get_cmdline+0x261>
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
lea    0x2cd9(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
lea    0x2cc9(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    13e1 <get_cmdline+0x261>
jmp    1312 <get_cmdline+0x192>
jmp    13bd <get_cmdline+0x23d>
jmp    13c2 <get_cmdline+0x242>
jmp    13c7 <get_cmdline+0x247>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    -0x24(%rbp),%eax
jb     1299 <get_cmdline+0x119>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
movl   $0x0,(%rsi)
mov    $0x80,%esi
call   1060 <strnlen@plt>
cmp    $0x7f,%eax
jbe    11b0 <get_cmdline+0x30>
xor    %edx,%edx
jmp    1324 <get_cmdline+0x1a4>
mov    %eax,%r15d
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1230 <get_cmdline+0xb0>
mov    %eax,%r14d
lea    0x2e99(%rip),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0xfff,%edx
call   1080 <read@plt>
lea    -0x1000(%rax),%ecx
cmp    $0xfffff000,%ecx
ja     1254 <get_cmdline+0xd4>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0e(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %edx,%edx
jmp    1324 <get_cmdline+0x1a4>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc9(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %edx,%edx
jmp    1324 <get_cmdline+0x1a4>
mov    %rbp,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x8(%rsp),%rdi
mov    %edi,%r14d
xor    %r13d,%r13d
xor    %edx,%edx
lea    0x2df6(%rip),%rsi
jmp    12ad <get_cmdline+0x12d>
nopl   0x0(%rax)
lea    (%rsi,%r12,1),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
mov    0x8(%rsp),%rdi
lea    0x2dd2(%rip),%rsi
xor    %edx,%edx
test   %eax,%eax
je     12c2 <get_cmdline+0x142>
add    $0x1,%r13d
cmp    %edi,%r13d
jae    1324 <get_cmdline+0x1a4>
mov    %r13d,%r12d
mov    (%r12,%rsi,1),%al
cmp    (%rbx),%al
je     1280 <get_cmdline+0x100>
test   %al,%al
je     1324 <get_cmdline+0x1a4>
cmp    $0xa,%al
jne    12a4 <get_cmdline+0x124>
jmp    1324 <get_cmdline+0x1a4>
mov    %rbp,%r8
add    $0x1,%r12
xor    %eax,%eax
movabs $0x100000401,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %r14,%r12
jae    12ff <get_cmdline+0x17f>
movzbl (%r12,%rsi,1),%ecx
add    $0x1,%r12
add    $0x1,%eax
cmp    $0x20,%rcx
ja     12e0 <get_cmdline+0x160>
bt     %rcx,%rbp
jae    12e0 <get_cmdline+0x160>
jmp    1304 <get_cmdline+0x184>
mov    %r12d,%r13d
jmp    12a4 <get_cmdline+0x124>
cmp    $0xffe,%eax
ja     11a9 <get_cmdline+0x29>
add    $0xffffffffffffffff,%r12
mov    %r12d,%ecx
mov    0x10(%rsp),%rdx
mov    %eax,(%rdx)
movb   $0x0,(%rcx,%rsi,1)
mov    %r8,%rdx
mov    %rdx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@get_cmdline.cmdline = internal global [4096 x i8] zeroinitializer
@.str = [14 x i8] c"/proc/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"open(/proc/cmdline): %s\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"read(/proc/cmdline): %s\0A\00"
define i8* @get_cmdline(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%14 = load i32*, i32** %5
store i32 0, i32* %14
%15 = load i8*, i8** %4
%16 = call i32 @strnlen(i8* %15, i32 128)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp uge i32 %17, 128
br i1 %18, label %19, label %20
19: ; preds = %2
store i8* null, i8** %3
br label %128
20: ; preds = %2
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i8* null, i8** %3
br label %128
29: ; preds = %20
%30 = call i32 @memset(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 0), i32 0, i32 4096)
%31 = load i32, i32* %10
%32 = call i32 @read(i32 %31, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 0), i32 4095)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp sle i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = load i32, i32* %9
%37 = icmp sge i32 %36, 4096
br i1 %37, label %38, label %44
38: ; preds = %35, %29
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %10
%43 = call i32 @close(i32 %42)
store i8* null, i8** %3
br label %128
44: ; preds = %35
%45 = load i32, i32* %10
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %9
store i32 %47, i32* %8
store i32 0, i32* %6
br label %48
48: ; preds = %122, %44
%49 = load i32, i32* %6
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %50
%52 = load i8, i8* %51
store i8 %52, i8* %11
%53 = load i8, i8* %11
%54 = sext i8 %53 to i32
%55 = load i8*, i8** %4
%56 = getelementptr inbounds i8, i8* %55, i64 0
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %54, %58
br i1 %59, label %60, label %70
60: ; preds = %48
%61 = load i8, i8* %11
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 0
br i1 %63, label %68, label %64
64: ; preds = %60
%65 = load i8, i8* %11
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 10
br i1 %67, label %68, label %69
68: ; preds = %64, %60
store i8* null, i8** %3
br label %128
69: ; preds = %64
br label %121
70: ; preds = %48
%71 = load i8*, i8** %4
%72 = load i32, i32* %6
%73 = zext i32 %72 to i64
%74 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %73
%75 = load i32, i32* %7
%76 = call i32 @strncmp(i8* %71, i8* %74, i32 %75)
%77 = icmp ne i32 %76, 0
br i1 %77, label %120, label %78
78: ; preds = %70
%79 = load i32, i32* %6
store i32 %79, i32* %12
br label %80
80: ; preds = %118, %78
%81 = load i32, i32* %6
%82 = add i32 %81, 1
store i32 %82, i32* %6
%83 = load i32, i32* %8
%84 = icmp ult i32 %82, %83
br i1 %84, label %85, label %119
85: ; preds = %80
%86 = load i32, i32* %6
%87 = zext i32 %86 to i64
%88 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %87
%89 = load i8, i8* %88
store i8 %89, i8* %11
%90 = load i8, i8* %11
%91 = sext i8 %90 to i32
%92 = icmp eq i32 %91, 10
br i1 %92, label %101, label %93
93: ; preds = %85
%94 = load i8, i8* %11
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 0
br i1 %96, label %101, label %97
97: ; preds = %93
%98 = load i8, i8* %11
%99 = sext i8 %98 to i32
%100 = icmp eq i32 %99, 32
br i1 %100, label %101, label %118
101: ; preds = %97, %93, %85
%102 = load i32, i32* %6
%103 = load i32, i32* %12
%104 = sub i32 %102, %103
store i32 %104, i32* %13
%105 = load i32, i32* %13
%106 = zext i32 %105 to i64
%107 = icmp uge i64 %106, 4095
br i1 %107, label %108, label %109
108: ; preds = %101
store i8* null, i8** %3
br label %128
109: ; preds = %101
%110 = load i32, i32* %13
%111 = load i32*, i32** %5
store i32 %110, i32* %111
%112 = load i32, i32* %6
%113 = zext i32 %112 to i64
%114 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %113
store i8 0, i8* %114
%115 = load i32, i32* %12
%116 = zext i32 %115 to i64
%117 = getelementptr inbounds i8, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 0), i64 %116
store i8* %117, i8** %3
br label %128
118: ; preds = %97
br label %80
119: ; preds = %80
br label %120
120: ; preds = %119, %70
br label %121
121: ; preds = %120, %69
br label %122
122: ; preds = %121
%123 = load i32, i32* %6
%124 = add i32 %123, 1
store i32 %124, i32* %6
%125 = load i32, i32* %8
%126 = icmp ult i32 %124, %125
br i1 %126, label %48, label %127
127: ; preds = %122
store i8* null, i8** %3
br label %128
128: ; preds = %127, %109, %108, %68, %38, %25, %19
%129 = load i8*, i8** %3
ret i8* %129
}

LLVM IR (-O2):
@get_cmdline.cmdline = internal global [4096 x i8] zeroinitializer
@.str = [14 x i8] c"/proc/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"open(/proc/cmdline): %s\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"read(/proc/cmdline): %s\0A\00"
define i8* @get_cmdline(i8* %0, i32* nocapture writeonly %1) {
store i32 0, i32* %1
%3 = tail call i32 @strnlen(i8* %0, i32 128)
%4 = icmp ugt i32 %3, 127
br i1 %4, label %57, label %5
5: ; preds = %2
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %57
13: ; preds = %5
%14 = tail call i32 @memset(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 0), i32 0, i32 4096)
%15 = tail call i32 @read(i32 %7, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 0), i32 4095)
%16 = add i32 %15, -4096
%17 = icmp ult i32 %16, -4095
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = tail call i32 @close(i32 %7)
br label %57
23: ; preds = %13
%24 = tail call i32 @close(i32 %7)
%25 = zext i32 %15 to i64
br label %26
26: ; preds = %53, %23
%27 = phi i32 [ 0, %23 ], [ %55, %53 ]
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = load i8, i8* %0
%32 = icmp eq i8 %30, %31
br i1 %32, label %34, label %33
33: ; preds = %26
switch i8 %30, label %53 [
i8 10, label %57
i8 0, label %57
]
34: ; preds = %26
%35 = tail call i32 @strncmp(i8* %0, i8* %29, i32 %3)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %53
37: ; preds = %34, %41
%38 = phi i64 [ %39, %41 ], [ %28, %34 ]
%39 = add nuw nsw i64 %38, 1
%40 = icmp ult i64 %39, %25
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %39
%43 = load i8, i8* %42
switch i8 %43, label %37 [
i8 32, label %44
i8 10, label %44
i8 0, label %44
]
44: ; preds = %41, %41, %41
%45 = trunc i64 %39 to i32
%46 = sub i32 %45, %27
%47 = icmp ugt i32 %46, 4094
br i1 %47, label %57, label %48
48: ; preds = %44
%49 = and i64 %39, 4294967295
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_cmdline.cmdline, i64 0, i64 %49
store i32 %46, i32* %1
store i8 0, i8* %50
br label %57
51: ; preds = %37
%52 = trunc i64 %39 to i32
br label %53
53: ; preds = %51, %33, %34
%54 = phi i32 [ %27, %33 ], [ %27, %34 ], [ %52, %51 ]
%55 = add i32 %54, 1
%56 = icmp ult i32 %55, %15
br i1 %56, label %26, label %57
57: ; preds = %33, %33, %53, %44, %48, %2, %18, %9
%58 = phi i8* [ null, %9 ], [ null, %18 ], [ null, %2 ], [ null, %44 ], [ %29, %48 ], [ null, %53 ], [ null, %33 ], [ null, %33 ]
ret i8* %58
}