#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
char *malloc(size_t);
int open(char *, int /*<<< orphan*/);
size_t pread(int, char *, size_t, int /*<<< orphan*/);
char *realloc(char *, size_t);
char *get_cmdline(size_t *len) {
  int fd = open("/proc/self/cmdline", O_RDONLY);
  if (fd == -1)
    return NULL;
  size_t allocated = 4096;
  char *buffer = malloc(allocated);
  if (!buffer) {
    close(fd);
    return NULL;
  }
  ssize_t ret;
  while (1) {
    ret = pread(fd, buffer, allocated, 0);
    if (ret < 0) {
      if (errno == EINTR)
        continue;
      close(fd);
      return NULL;
    }
    if ((size_t)ret == allocated) {
      allocated *= 2;
      buffer = realloc(buffer, allocated);
    } else {
      break;
    }
  }
  close(fd);
  *len = ret;
  return buffer;
}


x86 assembly (clang, -O0):
<get_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118b <get_cmdline+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    126f <get_cmdline+0x11f>
movq   $0x1000,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c0 <get_cmdline+0x70>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <get_cmdline+0x11f>
jmp    11c5 <get_cmdline+0x75>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1070 <pread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    121a <get_cmdline+0xca>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <get_cmdline+0xb5>
jmp    11c5 <get_cmdline+0x75>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <get_cmdline+0x11f>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    124a <get_cmdline+0xfa>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    124f <get_cmdline+0xff>
jmp    1254 <get_cmdline+0x104>
jmp    11c5 <get_cmdline+0x75>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11ee <get_cmdline+0x9e>
mov    %eax,%r15d
mov    $0x1000,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11e6 <get_cmdline+0x96>
mov    %rax,%r12
mov    $0x1000,%ebp
mov    $0x1000,%edx
mov    %r15d,%edi
mov    %rax,%rsi
xor    %ecx,%ecx
call   1070 <pread@plt>
cmp    $0x1000,%rax
jne    11d6 <get_cmdline+0x86>
nopl   0x0(%rax)
add    %rbp,%rbp
mov    %r12,%rdi
mov    %rbp,%rsi
call   1050 <realloc@plt>
mov    %rax,%r12
mov    %r15d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1070 <pread@plt>
cmp    %rbp,%rax
je     11b0 <get_cmdline+0x60>
mov    %rax,%rbx
mov    %r15d,%edi
call   1030 <close@plt>
mov    %rbx,(%r14)
jmp    11f1 <get_cmdline+0xa1>
mov    %r15d,%edi
call   1030 <close@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i8* @get_cmdline(i64* %0) {
%2 = alloca i8*
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i64* %0, i64** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %55
13: ; preds = %1
store i64 4096, i64* %5
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
store i8* null, i8** %2
br label %55
21: ; preds = %13
br label %22
22: ; preds = %21, %33, %48
%23 = load i32, i32* %4
%24 = load i8*, i8** %6
%25 = load i64, i64* %5
%26 = call i64 @pread(i32 %23, i8* %24, i64 %25, i32 0)
store i64 %26, i64* %7
%27 = load i64, i64* %7
%28 = icmp ult i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %22
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %22
34: ; preds = %29
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
store i8* null, i8** %2
br label %55
37: ; preds = %22
%38 = load i64, i64* %7
%39 = load i64, i64* %5
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i64, i64* %5
%43 = mul i64 %42, 2
store i64 %43, i64* %5
%44 = load i8*, i8** %6
%45 = load i64, i64* %5
%46 = call i8* @realloc(i8* %44, i64 %45)
store i8* %46, i8** %6
br label %48
47: ; preds = %37
br label %49
48: ; preds = %41
br label %22
49: ; preds = %47
%50 = load i32, i32* %4
%51 = call i32 @close(i32 %50)
%52 = load i64, i64* %7
%53 = load i64*, i64** %3
store i64 %52, i64* %53
%54 = load i8*, i8** %6
store i8* %54, i8** %2
br label %55
55: ; preds = %49, %34, %18, %12
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define noalias i8* @get_cmdline(i64* nocapture writeonly %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i64 @pread(i32 %3, i8* %6, i64 4096, i32 0)
%10 = icmp eq i64 %9, 4096
br i1 %10, label %13, label %20
11: ; preds = %5
%12 = tail call i32 @close(i32 %3)
br label %24
13: ; preds = %8, %13
%14 = phi i8* [ %17, %13 ], [ %6, %8 ]
%15 = phi i64 [ %16, %13 ], [ 4096, %8 ]
%16 = shl i64 %15, 1
%17 = tail call i8* @realloc(i8* %14, i64 %16)
%18 = tail call i64 @pread(i32 %3, i8* %17, i64 %16, i32 0)
%19 = icmp eq i64 %18, %16
br i1 %19, label %13, label %20
20: ; preds = %13, %8
%21 = phi i8* [ %6, %8 ], [ %17, %13 ]
%22 = phi i64 [ %9, %8 ], [ %18, %13 ]
%23 = tail call i32 @close(i32 %3)
store i64 %22, i64* %0
br label %24
24: ; preds = %11, %20, %1
%25 = phi i8* [ null, %1 ], [ %21, %20 ], [ null, %11 ]
ret i8* %25
}