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

/* Variables and functions */
int /*<<< orphan*/ crStrncpy(char *, char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int sscanf(char *, char *, int *, char *);
int /*<<< orphan*/ system(char *);
char *tmpnam(int /*<<< orphan*/ *);
void crGetProcName(char *name, int maxLen) {
  FILE *f;
  pid_t pid = getpid();
  char *tmp, command[1000];
  *name = 0;
  tmp = tmpnam(NULL);
  if (!tmp)
    return;
  sprintf(command, "ps > %s", tmp);
  system(command);
  f = fopen(tmp, "r");
  if (f) {
    char buffer[1000], cmd[1000];
    while (!feof(f)) {
      int id, k;
      fgets(buffer, 999, f);
      k = sscanf(buffer, "%d %*s %*s %999s", &id, cmd);
      if (k == 2 && id == pid) {
        crStrncpy(name, cmd, maxLen);
        break;
      }
    }
    fclose(f);
  }
  remove(tmp);
}


x86 assembly (clang, -O0):
<crGetProcName>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc00,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
call   10d0 <tmpnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f0 <crGetProcName+0x40>
jmp    12dc <crGetProcName+0x12c>
lea    -0x410(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdfe(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1070 <system@plt>
mov    -0x28(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d3 <crGetProcName+0x123>
jmp    1237 <crGetProcName+0x87>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1255 <crGetProcName+0xa5>
jmp    12ca <crGetProcName+0x11a>
lea    -0x800(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3e7,%esi
call   1080 <fgets@plt>
lea    -0x800(%rbp),%rdi
lea    -0xbf0(%rbp),%rcx
lea    0xd8b(%rip),%rsi
lea    -0xbf4(%rbp),%rdx
call   10a0 <sscanf@plt>
mov    %eax,-0xbf8(%rbp)
cmpl   $0x2,-0xbf8(%rbp)
jne    12c5 <crGetProcName+0x115>
mov    -0xbf4(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    12c5 <crGetProcName+0x115>
mov    -0x8(%rbp),%rdi
lea    -0xbf0(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <crStrncpy@plt>
jmp    12ca <crGetProcName+0x11a>
jmp    1237 <crGetProcName+0x87>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <remove@plt>
add    $0xc00,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crGetProcName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xbe8,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x10(%rsp)
movb   $0x0,0x0(%rbp)
xor    %edi,%edi
call   10d0 <tmpnam@plt>
test   %rax,%rax
je     12bf <crGetProcName+0x10f>
mov    %rax,%r13
lea    0xe10(%rip),%rsi
lea    0x800(%rsp),%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
call   1070 <system@plt>
lea    0xdf6(%rip),%rsi
mov    %r13,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b7 <crGetProcName+0x107>
mov    %rax,%rbx
mov    %rbp,0x18(%rsp)
mov    %r14d,0xc(%rsp)
mov    %rax,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    12af <crGetProcName+0xff>
lea    0x410(%rsp),%rbp
lea    0xdbf(%rip),%r14
lea    0x14(%rsp),%r15
lea    0x20(%rsp),%r12
jmp    126c <crGetProcName+0xbc>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    12af <crGetProcName+0xff>
mov    %rbp,%rdi
mov    $0x3e7,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   10a0 <sscanf@plt>
cmp    $0x2,%eax
jne    1260 <crGetProcName+0xb0>
mov    0x10(%rsp),%eax
cmp    %eax,0x14(%rsp)
jne    1260 <crGetProcName+0xb0>
lea    0x20(%rsp),%rsi
mov    0x18(%rsp),%rdi
mov    0xc(%rsp),%edx
call   1060 <crStrncpy@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1030 <remove@plt>
add    $0xbe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"ps > %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"%d %*s %*s %999s\00"
define void @crGetProcName(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [1000 x i8]
%9 = alloca [1000 x i8]
%10 = alloca [1000 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%13 = call i32 (...) @getpid()
store i32 %13, i32* %6
%14 = load i8*, i8** %3
store i8 0, i8* %14
%15 = call i8* @tmpnam(i32* null)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %2
br label %60
19: ; preds = %2
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%21 = load i8*, i8** %7
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%24 = call i32 @system(i8* %23)
%25 = load i8*, i8** %7
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %5
%27 = load i32*, i32** %5
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %57
29: ; preds = %19
br label %30
30: ; preds = %53, %29
%31 = load i32*, i32** %5
%32 = call i32 @feof(i32* %31)
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br i1 %34, label %35, label %54
35: ; preds = %30
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%37 = load i32*, i32** %5
%38 = call i32 @fgets(i8* %36, i32 999, i32* %37)
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%40 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%41 = call i32 @sscanf(i8* %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32* %11, i8* %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, 2
br i1 %43, label %44, label %53
44: ; preds = %35
%45 = load i32, i32* %11
%46 = load i32, i32* %6
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = load i8*, i8** %3
%50 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%51 = load i32, i32* %4
%52 = call i32 @crStrncpy(i8* %49, i8* %50, i32 %51)
br label %54
53: ; preds = %44, %35
br label %30
54: ; preds = %48, %30
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
br label %57
57: ; preds = %54, %19
%58 = load i8*, i8** %7
%59 = call i32 @remove(i8* %58)
br label %60
60: ; preds = %57, %18
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"ps > %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"%d %*s %*s %999s\00"
define void @crGetProcName(i8* %0, i32 %1) {
%3 = alloca [1000 x i8]
%4 = alloca [1000 x i8]
%5 = alloca [1000 x i8]
%6 = alloca i32
%7 = tail call i32 (...) @getpid()
%8 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
store i8 0, i8* %0
%9 = tail call i8* @tmpnam(i32* null)
%10 = icmp eq i8* %9, null
br i1 %10, label %39, label %11
11: ; preds = %2
%12 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %9)
%13 = call i32 @system(i8* %8)
%14 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %37, label %16
16: ; preds = %11
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%19 = call i32 @feof(i32* %14)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %16
%22 = bitcast i32* %6 to i8*
br label %23
23: ; preds = %21, %32
%24 = call i32 @fgets(i8* %17, i32 999, i32* %14)
%25 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32* %6, i8* %18)
%26 = icmp eq i32 %25, 2
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, %7
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @crStrncpy(i8* %0, i8* %18, i32 %1)
br label %35
32: ; preds = %23
%33 = call i32 @feof(i32* %14)
%34 = icmp eq i32 %33, 0
br i1 %34, label %23, label %35
35: ; preds = %32, %16, %30
%36 = call i32 @fclose(i32* %14)
br label %37
37: ; preds = %35, %11
%38 = call i32 @remove(i8* %9)
br label %39
39: ; preds = %2, %37
ret void
}