#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*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *);
void ipm_get_exec_cmdline(char *cmdl, char *rpath) {
  int i, ii, fd, rv, blen;
  FILE *fh;
  char *cp, *pp, *up, cbuf[4096];
  cmdl[0] = '\0';
  fh = fopen("/proc/self/cmdline", "r");
  if (fh) {
    ii = 0;
    fgets(cmdl, 4096, fh);
    for (i = 1; i < 4096; i++) {
      if (cmdl[i] == 0 && ii == 0) {
        cmdl[i] = ' ';
        ii = 1;
      } else if (cmdl[i] == 0 && ii == 1) {
        break;
      } else {
        ii = 0;
      }
    }
    fclose(fh);
  } else {
    sprintf(cmdl, "unknown");
  }
  rv = readlink("/proc/self/exe", rpath, 4096);
  if (rv < 1)
    sprintf(rpath, "unknown");
}


x86 assembly (clang, -O0):
<ipm_get_exec_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
lea    0xe8f(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1241 <ipm_get_exec_cmdline+0xf1>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x1000,-0x14(%rbp)
jge    1233 <ipm_get_exec_cmdline+0xe3>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11f0 <ipm_get_exec_cmdline+0xa0>
cmpl   $0x0,-0x18(%rbp)
jne    11f0 <ipm_get_exec_cmdline+0xa0>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
movl   $0x1,-0x18(%rbp)
jmp    1220 <ipm_get_exec_cmdline+0xd0>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1214 <ipm_get_exec_cmdline+0xc4>
cmpl   $0x1,-0x18(%rbp)
jne    1214 <ipm_get_exec_cmdline+0xc4>
jmp    1233 <ipm_get_exec_cmdline+0xe3>
movl   $0x0,-0x18(%rbp)
jmp    1220 <ipm_get_exec_cmdline+0xd0>
jmp    1225 <ipm_get_exec_cmdline+0xd5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ac <ipm_get_exec_cmdline+0x5c>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1251 <ipm_get_exec_cmdline+0x101>
mov    -0x8(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rsi
lea    0xdc1(%rip),%rdi
mov    $0x1000,%edx
call   1030 <readlink@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jge    1283 <ipm_get_exec_cmdline+0x133>
mov    -0x10(%rbp),%rdi
lea    0xd97(%rip),%rsi
call   1070 <sprintf@plt>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipm_get_exec_cmdline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
movb   $0x0,(%rdi)
lea    0xea8(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f8 <ipm_get_exec_cmdline+0xb8>
mov    %rax,%r15
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %ecx,%ecx
mov    $0x2,%eax
jmp    119f <ipm_get_exec_cmdline+0x5f>
nopl   0x0(%rax)
test   %bl,%bl
sete   %bl
xor    %ecx,%ecx
test   %bl,%dl
jne    11ee <ipm_get_exec_cmdline+0xae>
add    $0x2,%rax
movzbl -0x1(%r12,%rax,1),%esi
test   %sil,%sil
sete   %bl
test   %ecx,%ecx
sete   %dl
and    %bl,%dl
cmp    $0x1,%dl
jne    11c0 <ipm_get_exec_cmdline+0x80>
movb   $0x20,-0x1(%r12,%rax,1)
jmp    11ca <ipm_get_exec_cmdline+0x8a>
nop
cmp    $0x1,%ecx
jne    11ca <ipm_get_exec_cmdline+0x8a>
test   %sil,%sil
je     11ee <ipm_get_exec_cmdline+0xae>
cmp    $0x1000,%rax
je     11ee <ipm_get_exec_cmdline+0xae>
movzbl (%r12,%rax,1),%ebx
test   %bl,%bl
jne    1190 <ipm_get_exec_cmdline+0x50>
mov    %edx,%ecx
xor    $0x1,%cl
je     1190 <ipm_get_exec_cmdline+0x50>
movb   $0x20,(%r12,%rax,1)
mov    $0x1,%ecx
jmp    119b <ipm_get_exec_cmdline+0x5b>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1206 <ipm_get_exec_cmdline+0xc6>
movabs $0x6e776f6e6b6e75,%rax
mov    %rax,(%r12)
lea    0xe08(%rip),%rdi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <readlink@plt>
test   %eax,%eax
jg     122b <ipm_get_exec_cmdline+0xeb>
movabs $0x6e776f6e6b6e75,%rax
mov    %rax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"unknown\00"
@.str.3 = [15 x i8] c"/proc/self/exe\00"
define void @ipm_get_exec_cmdline(i8* %0, i8* %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 i8*
%13 = alloca i8*
%14 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
%17 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %64
20: ; preds = %2
store i32 0, i32* %6
%21 = load i8*, i8** %3
%22 = load i32*, i32** %10
%23 = call i32 @fgets(i8* %21, i32 4096, i32* %22)
store i32 1, i32* %5
br label %24
24: ; preds = %58, %20
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 4096
br i1 %26, label %27, label %61
27: ; preds = %24
%28 = load i8*, i8** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %27
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i8*, i8** %3
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 32, i8* %42
store i32 1, i32* %6
br label %57
43: ; preds = %35, %27
%44 = load i8*, i8** %3
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %43
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, 1
br i1 %53, label %54, label %55
54: ; preds = %51
br label %61
55: ; preds = %51, %43
store i32 0, i32* %6
br label %56
56: ; preds = %55
br label %57
57: ; preds = %56, %38
br label %58
58: ; preds = %57
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %24
61: ; preds = %54, %24
%62 = load i32*, i32** %10
%63 = call i32 @fclose(i32* %62)
br label %67
64: ; preds = %2
%65 = load i8*, i8** %3
%66 = call i32 @sprintf(i8* %65, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %64, %61
%68 = load i8*, i8** %4
%69 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %68, i32 4096)
store i32 %69, i32* %8
%70 = load i32, i32* %8
%71 = icmp slt i32 %70, 1
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = load i8*, i8** %4
%74 = call i32 @sprintf(i8* %73, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %75
75: ; preds = %72, %67
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c"/proc/self/exe\00"
define void @ipm_get_exec_cmdline(i8* %0, i8* nocapture %1) {
store i8 0, i8* %0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 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 %36, label %5
5: ; preds = %2
%6 = tail call i32 @fgets(i8* %0, i32 4096, i32* %3)
br label %7
7: ; preds = %31, %5
%8 = phi i64 [ 1, %5 ], [ %33, %31 ]
%9 = phi i32 [ 0, %5 ], [ %32, %31 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %8
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
%13 = icmp eq i32 %9, 0
%14 = and i1 %13, %12
br i1 %14, label %15, label %16
15: ; preds = %7
store i8 32, i8* %10
br label %19
16: ; preds = %7
%17 = icmp eq i32 %9, 1
%18 = and i1 %17, %12
br i1 %18, label %34, label %19
19: ; preds = %16, %15
%20 = add nuw nsw i64 %8, 1
%21 = icmp eq i64 %20, 4096
br i1 %21, label %34, label %22
22: ; preds = %19
%23 = xor i1 %14, true
%24 = getelementptr inbounds i8, i8* %0, i64 %20
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
%27 = and i1 %26, %23
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = and i1 %14, %26
br i1 %29, label %34, label %31
30: ; preds = %22
store i8 32, i8* %24
br label %31
31: ; preds = %30, %28
%32 = phi i32 [ 1, %30 ], [ 0, %28 ]
%33 = add nuw nsw i64 %8, 2
br label %7
34: ; preds = %28, %16, %19
%35 = tail call i32 @fclose(i32* %3)
br label %38
36: ; preds = %2
%37 = bitcast i8* %0 to i64*
store i64 31093567915781749, i64* %37
br label %38
38: ; preds = %36, %34
%39 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 4096)
%40 = icmp slt i32 %39, 1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = bitcast i8* %1 to i64*
store i64 31093567915781749, i64* %42
br label %43
43: ; preds = %41, %38
ret void
}