#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *readname(pid_t pid) {
  static char name[256];
  char *pp = name, byte, path[256];
  FILE *file;
  int cntr = 0;
  size_t sz;
  snprintf(path, 255,
           "/proc"
           "/%d/cmdline",
           pid);
  file = fopen(path, "r");
  if (!file)
    return NULL;
  do {
    sz = fread(&byte, 1, 1, file);
    if (sz != 1)
      break;
    if (byte != '/')
      *pp++ = byte;
    else {
      pp = name;
      cntr = 0;
    }
  } while (byte && cntr++ < 255);
  name[cntr] = 0;
  fclose(file);
  return name;
}


x86 assembly (clang, -O0):
<readname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0xc(%rbp)
lea    0x2efb(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x12c(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xff,%esi
lea    0xe87(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    11b3 <readname+0x73>
movq   $0x0,-0x8(%rbp)
jmp    1294 <readname+0x154>
jmp    11b8 <readname+0x78>
mov    -0x128(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x1,-0x138(%rbp)
01
je     11e9 <readname+0xa9>
jmp    126b <readname+0x12b>
movsbl -0x19(%rbp),%eax
cmp    $0x2f,%eax
je     120f <readname+0xcf>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1224 <readname+0xe4>
lea    0x2e3a(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x12c(%rbp)
jmp    1229 <readname+0xe9>
movsbl -0x19(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x139(%rbp)
je     125d <readname+0x11d>
mov    -0x12c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x12c(%rbp)
cmp    $0xff,%eax
setl   %al
mov    %al,-0x139(%rbp)
mov    -0x139(%rbp),%al
test   $0x1,%al
jne    11b8 <readname+0x78>
movslq -0x12c(%rbp),%rcx
lea    0x2dd7(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x128(%rbp),%rdi
call   1040 <fclose@plt>
lea    0x2dc0(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readname>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %edi,%ecx
lea    0xea7(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1050 <snprintf@plt>
lea    0xe9f(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1202 <readname+0xc2>
mov    %rax,%r14
lea    0x2ec0(%rip),%r12
lea    0xf(%rsp),%r15
mov    %r12,%rbx
jmp    11b8 <readname+0x78>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %r13d,%eax
mov    %rbx,%rcx
lea    0x1(%rax),%r13d
mov    %rcx,%rbx
cmp    $0xff,%eax
jge    11eb <readname+0xab>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x1,%rax
jne    11eb <readname+0xab>
movzbl 0xf(%rsp),%edx
xor    %eax,%eax
mov    %r12,%rcx
cmp    $0x2f,%dl
je     11aa <readname+0x6a>
mov    %dl,(%rbx)
cmpb   $0x0,0xf(%rsp)
jne    11a0 <readname+0x60>
movslq %r13d,%rax
lea    0x2e5b(%rip),%r13
movb   $0x0,(%rax,%r13,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r13,%rax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@readname.name = internal global [256 x i8] zeroinitializer
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @readname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca [256 x i8]
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %3
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readname.name, i64 0, i64 0), i8** %4
store i32 0, i32* %8
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @snprintf(i8* %10, i32 255, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %1
store i8* null, i8** %2
br label %51
18: ; preds = %1
br label %19
19: ; preds = %43, %18
%20 = load i32*, i32** %7
%21 = call i64 @fread(i8* %5, i32 1, i32 1, i32* %20)
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = icmp ne i64 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %19
br label %45
25: ; preds = %19
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 47
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i8, i8* %5
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 %30, i8* %31
br label %34
33: ; preds = %25
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readname.name, i64 0, i64 0), i8** %4
store i32 0, i32* %8
br label %34
34: ; preds = %33, %29
br label %35
35: ; preds = %34
%36 = load i8, i8* %5
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
%42 = icmp slt i32 %40, 255
br label %43
43: ; preds = %39, %35
%44 = phi i1 [ false, %35 ], [ %42, %39 ]
br i1 %44, label %19, label %45
45: ; preds = %43, %24
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [256 x i8], [256 x i8]* @readname.name, i64 0, i64 %47
store i8 0, i8* %48
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @readname.name, i64 0, i64 0), i8** %2
br label %51
51: ; preds = %45, %17
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@readname.name = internal global [256 x i8] zeroinitializer
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @readname(i32 %0) {
%2 = alloca i8
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %30, label %8
8: ; preds = %1, %20
%9 = phi i8* [ %22, %20 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @readname.name, i64 0, i64 0), %1 ]
%10 = phi i32 [ %23, %20 ], [ 0, %1 ]
%11 = call i64 @fread(i8* %2, i32 1, i32 1, i32* %6)
%12 = icmp eq i64 %11, 1
br i1 %12, label %13, label %25
13: ; preds = %8
%14 = load i8, i8* %2
%15 = icmp eq i8 %14, 47
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %14, i8* %9
%18 = load i8, i8* %2
%19 = icmp eq i8 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %13, %16
%21 = phi i32 [ %10, %16 ], [ 0, %13 ]
%22 = phi i8* [ %17, %16 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @readname.name, i64 0, i64 0), %13 ]
%23 = add nsw i32 %21, 1
%24 = icmp slt i32 %21, 255
br i1 %24, label %8, label %25
25: ; preds = %16, %8, %20
%26 = phi i32 [ %10, %8 ], [ %23, %20 ], [ %10, %16 ]
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* @readname.name, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @fclose(i32* %6)
br label %30
30: ; preds = %1, %25
%31 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @readname.name, i64 0, i64 0), %25 ], [ null, %1 ]
ret i8* %31
}