#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
size_t strlen(char *);
char *read_proc_cmdline(void) {
  FILE *f = fopen("/proc/cmdline", "r");
  char *cmdline = NULL;
  size_t len;
  if (!f)
    goto out;
  if (getline(&cmdline, &len, f) < 0)
    goto out;
  len = strlen(cmdline);
  if (cmdline[len - 1] == '\n')
    cmdline[len - 1] = '\0';
out:
  if (f)
    fclose(f);
  return cmdline;
}


x86 assembly (clang, -O0):
<read_proc_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1177 <read_proc_cmdline+0x37>
jmp    11d2 <read_proc_cmdline+0x92>
mov    -0x8(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <getline@plt>
cmp    $0x0,%rax
jge    1197 <read_proc_cmdline+0x57>
jmp    11d2 <read_proc_cmdline+0x92>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11cd <read_proc_cmdline+0x8d>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11d2 <read_proc_cmdline+0x92>
cmpq   $0x0,-0x8(%rbp)
je     11e6 <read_proc_cmdline+0xa6>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_cmdline>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xeb2(%rip),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
movq   $0x0,0x8(%rsp)
test   %rax,%rax
je     11b1 <read_proc_cmdline+0x71>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1060 <getline@plt>
test   %rax,%rax
js     11a2 <read_proc_cmdline+0x62>
mov    0x8(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,0x10(%rsp)
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    11a2 <read_proc_cmdline+0x62>
movb   $0x0,-0x1(%r14,%rax,1)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rax
jmp    11b3 <read_proc_cmdline+0x73>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @read_proc_cmdline() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i64
%4 = 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* %4, i32** %1
store i8* null, i8** %2
%5 = load i32*, i32** %1
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
br label %29
8: ; preds = %0
%9 = load i32*, i32** %1
%10 = call i64 @getline(i8** %2, i64* %3, i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
br label %29
13: ; preds = %8
%14 = load i8*, i8** %2
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %3
%16 = load i8*, i8** %2
%17 = load i64, i64* %3
%18 = sub i64 %17, 1
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = load i8*, i8** %2
%25 = load i64, i64* %3
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %23, %13
br label %29
29: ; preds = %28, %12, %7
%30 = load i32*, i32** %1
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32*, i32** %1
%34 = call i32 @fclose(i32* %33)
br label %35
35: ; preds = %32, %29
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @read_proc_cmdline() {
%1 = alloca i8*
%2 = alloca i64
%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 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%5 = bitcast i64* %2 to i8*
%6 = icmp eq i32* %3, null
br i1 %6, label %21, label %7
7: ; preds = %0
%8 = call i64 @getline(i8** %1, i64* %2, i32* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = load i8*, i8** %1
%12 = call i64 @strlen(i8* dereferenceable(1) %11)
store i64 %12, i64* %2
%13 = add i64 %12, -1
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %17, label %18
17: ; preds = %10
store i8 0, i8* %14
br label %18
18: ; preds = %7, %17, %10
%19 = call i32 @fclose(i32* %3)
%20 = load i8*, i8** %1
br label %21
21: ; preds = %0, %18
%22 = phi i8* [ null, %0 ], [ %20, %18 ]
ret i8* %22
}