#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*/ PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *get_taskname(pid_t pid, char *pname) {
  char fname[PATH_MAX + 1];
  FILE *fp;
  int nitems;
  unsigned rpid;
  char rname[80], *p;
  sprintf(fname, "/proc/%d/stat", pid);
  fp = fopen(fname, "r");
  if (fp == NULL) {
    sprintf(pname, "?%d?", pid);
    return pname;
  }
  nitems = fscanf(fp, "%10u (%80s)", &rpid, rname);
  if (nitems != 2) {
    sprintf(pname, "?%d?", pid);
    fclose(fp);
    return pname;
  }
  p = strchr(rname, ')');
  if (p)
    *p = 0;
  strncpy(pname, rname, 15);
  pname[15] = 0;
  fclose(fp);
  return pname;
}


x86 assembly (clang, -O0):
<get_taskname>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0xa8(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edx
lea    0xe52(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0xa8(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ff <get_taskname+0x9f>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe2d(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
jmp    12c2 <get_taskname+0x162>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rcx
lea    0xe04(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x2,-0x34(%rbp)
je     125a <get_taskname+0xfa>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xddb(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
jmp    12c2 <get_taskname+0x162>
lea    -0x90(%rbp),%rdi
mov    $0x29,%esi
call   1050 <strchr@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
je     128a <get_taskname+0x12a>
mov    -0x98(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,0xf(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_taskname>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe69(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1080 <sprintf@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1203 <get_taskname+0xa3>
mov    %rax,%rbx
lea    0xe55(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x70(%rbp),%rcx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x2,%eax
jne    1217 <get_taskname+0xb7>
lea    -0x70(%rbp),%rdi
mov    $0x29,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11eb <get_taskname+0x8b>
movb   $0x0,(%rax)
lea    -0x70(%rbp),%rsi
mov    %r14,%rdi
mov    $0xf,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xf(%r14)
jmp    1229 <get_taskname+0xc9>
lea    0xe06(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1080 <sprintf@plt>
jmp    1231 <get_taskname+0xd1>
lea    0xdf2(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"?%d?\00"
@.str.3 = [12 x i8] c"%10u (%80s)\00"
define i8* @get_taskname(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [80 x i8]
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%14 = load i32, i32* @PATH_MAX
%15 = add nsw i32 %14, 1
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %6
%18 = alloca i8, i64 %16
store i64 %16, i64* %7
%19 = load i32, i32* %4
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %2
%25 = load i8*, i8** %5
%26 = load i32, i32* %4
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %5
store i8* %28, i8** %3
store i32 1, i32* %13
br label %58
29: ; preds = %2
%30 = load i32*, i32** %8
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%32 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %10, i8* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %42
35: ; preds = %29
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32*, i32** %8
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %5
store i8* %41, i8** %3
store i32 1, i32* %13
br label %58
42: ; preds = %29
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%44 = call i8* @strchr(i8* %43, i8 signext 41)
store i8* %44, i8** %12
%45 = load i8*, i8** %12
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = load i8*, i8** %12
store i8 0, i8* %48
br label %49
49: ; preds = %47, %42
%50 = load i8*, i8** %5
%51 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%52 = call i32 @strncpy(i8* %50, i8* %51, i32 15)
%53 = load i8*, i8** %5
%54 = getelementptr inbounds i8, i8* %53, i64 15
store i8 0, i8* %54
%55 = load i32*, i32** %8
%56 = call i32 @fclose(i32* %55)
%57 = load i8*, i8** %5
store i8* %57, i8** %3
store i32 1, i32* %13
br label %58
58: ; preds = %49, %35, %24
%59 = load i8*, i8** %6
%60 = load i8*, i8** %3
ret i8* %60
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"?%d?\00"
@.str.3 = [12 x i8] c"%10u (%80s)\00"
define i8* @get_taskname(i32 %0, i8* returned %1) {
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast i32* %3 to i8*
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%11 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %30
16: ; preds = %2
%17 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %3, i8* %10)
%18 = icmp eq i32 %17, 2
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%21 = call i32 @fclose(i32* %12)
br label %30
22: ; preds = %16
%23 = call i8* @strchr(i8* %10, i8 signext 41)
%24 = icmp eq i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %22
store i8 0, i8* %23
br label %26
26: ; preds = %25, %22
%27 = call i32 @strncpy(i8* %1, i8* %10, i32 15)
%28 = getelementptr inbounds i8, i8* %1, i64 15
store i8 0, i8* %28
%29 = call i32 @fclose(i32* %12)
br label %30
30: ; preds = %26, %19, %14
ret i8* %1
}