#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*/ buf;

/* Variables and functions */
int atoi(char *);
int f_read_string(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strlcpy(char *, char *, int);
char *strrchr(char *, char);
char *psname(int pid, char *buffer, int maxlen) {
  char buf[512];
  char path[64];
  char *p;
  if (maxlen <= 0)
    return NULL;
  *buffer = 0;
  sprintf(path, "/proc/%d/stat", pid);
  if ((f_read_string(path, buf, sizeof(buf)) > 4) &&
      ((p = strrchr(buf, ')')) != NULL)) {
    *p = 0;
    if (((p = strchr(buf, '(')) != NULL) && (atoi(buf) == pid)) {
      strlcpy(buffer, p + 1, maxlen);
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<psname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     118c <psname+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1251 <psname+0xf1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x260(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe5c(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x260(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <f_read_string@plt>
cmp    $0x4,%eax
jle    1249 <psname+0xe9>
lea    -0x220(%rbp),%rdi
mov    $0x29,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x268(%rbp)
cmp    $0x0,%rax
je     1249 <psname+0xe9>
mov    -0x268(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x220(%rbp),%rdi
mov    $0x28,%esi
call   1030 <strchr@plt>
mov    %rax,-0x268(%rbp)
cmp    $0x0,%rax
je     1244 <psname+0xe4>
lea    -0x220(%rbp),%rdi
call   1070 <atoi@plt>
cmp    -0xc(%rbp),%eax
jne    1244 <psname+0xe4>
mov    -0x18(%rbp),%rdi
mov    -0x268(%rbp),%rsi
add    $0x1,%rsi
mov    -0x1c(%rbp),%edx
call   1050 <strlcpy@plt>
jmp    1249 <psname+0xe9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x240,%rsp
test   %edx,%edx
jle    1200 <psname+0xa0>
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%r14d
movb   $0x0,(%rsi)
lea    0xe76(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
call   1080 <sprintf@plt>
lea    0x40(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x200,%edx
call   1060 <f_read_string@plt>
cmp    $0x5,%eax
jl     1202 <psname+0xa2>
lea    0x40(%rsp),%rdi
mov    $0x29,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1202 <psname+0xa2>
movb   $0x0,(%rax)
lea    0x40(%rsp),%rdi
mov    $0x28,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1202 <psname+0xa2>
mov    %rax,%r15
lea    0x40(%rsp),%rdi
call   1070 <atoi@plt>
cmp    %r14d,%eax
jne    1202 <psname+0xa2>
add    $0x1,%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1050 <strlcpy@plt>
jmp    1202 <psname+0xa2>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x240,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
define i8* @psname(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca [64 x i8]
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i8* null, i8** %4
br label %46
14: ; preds = %3
%15 = load i8*, i8** %6
store i8 0, i8* %15
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%17 = load i32, i32* %5
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%21 = call i32 @f_read_string(i8* %19, i8* %20, i32 512)
%22 = icmp sgt i32 %21, 4
br i1 %22, label %23, label %44
23: ; preds = %14
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%25 = call i8* @strrchr(i8* %24, i8 signext 41)
store i8* %25, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i8*, i8** %10
store i8 0, i8* %28
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%30 = call i8* @strchr(i8* %29, i8 signext 40)
store i8* %30, i8** %10
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %43
32: ; preds = %27
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%34 = call i32 @atoi(i8* %33)
%35 = load i32, i32* %5
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i8*, i8** %6
%39 = load i8*, i8** %10
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i32, i32* %7
%42 = call i32 @strlcpy(i8* %38, i8* %40, i32 %41)
br label %43
43: ; preds = %37, %32, %27
br label %44
44: ; preds = %43, %23, %14
%45 = load i8*, i8** %6
store i8* %45, i8** %4
br label %46
46: ; preds = %44, %13
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
define i8* @psname(i32 %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = icmp slt i32 %2, 1
br i1 %8, label %25, label %9
9: ; preds = %3
store i8 0, i8* %1
%10 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = call i32 @f_read_string(i8* %7, i8* %6, i32 512)
%12 = icmp sgt i32 %11, 4
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = call i8* @strrchr(i8* %6, i8 signext 41)
%15 = icmp eq i8* %14, null
br i1 %15, label %25, label %16
16: ; preds = %13
store i8 0, i8* %14
%17 = call i8* @strchr(i8* %6, i8 signext 40)
%18 = icmp eq i8* %17, null
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = call i32 @atoi(i8* %6)
%21 = icmp eq i32 %20, %0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %17, i64 1
%24 = call i32 @strlcpy(i8* %1, i8* %23, i32 %2)
br label %25
25: ; preds = %9, %13, %22, %19, %16, %3
%26 = phi i8* [ null, %3 ], [ %1, %16 ], [ %1, %19 ], [ %1, %22 ], [ %1, %13 ], [ %1, %9 ]
ret i8* %26
}