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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ xmalloc(int);
char *get_program_name(pid_t pid) {
  char file_name[64];
  char status_info[256];
  int fd;
  int rval;
  char *open_paren;
  char *close_paren;
  char *result;
  snprintf(file_name, sizeof(file_name), "/proc/%d/stat", (int)pid);
  fd = open(file_name, O_RDONLY);
  if (fd == -1)
    return NULL;
  rval = read(fd, status_info, sizeof(status_info) - 1);
  close(fd);
  if (rval <= 0)
    return NULL;
  status_info[rval] = '\0';
  open_paren = strchr(status_info, '(');
  close_paren = strchr(status_info, ')');
  if (open_paren == NULL || close_paren == NULL || close_paren < open_paren)
    return NULL;
  result = (char *)xmalloc(close_paren - open_paren);
  strncpy(result, open_paren + 1, close_paren - open_paren - 1);
  result[close_paren - open_paren - 1] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<get_program_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xe6b(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0xffffffff,-0x154(%rbp)
jne    11cc <get_program_name+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    1302 <get_program_name+0x192>
mov    -0x154(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0x158(%rbp)
mov    -0x154(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x158(%rbp)
jg     120e <get_program_name+0x9e>
movq   $0x0,-0x8(%rbp)
jmp    1302 <get_program_name+0x192>
movslq -0x158(%rbp),%rax
movb   $0x0,-0x150(%rbp,%rax,1)
lea    -0x150(%rbp),%rdi
mov    $0x28,%esi
call   1040 <strchr@plt>
mov    %rax,-0x160(%rbp)
lea    -0x150(%rbp),%rdi
mov    $0x29,%esi
call   1040 <strchr@plt>
mov    %rax,-0x168(%rbp)
cmpq   $0x0,-0x160(%rbp)
je     127d <get_program_name+0x10d>
cmpq   $0x0,-0x168(%rbp)
je     127d <get_program_name+0x10d>
mov    -0x168(%rbp),%rax
cmp    -0x160(%rbp),%rax
jae    128a <get_program_name+0x11a>
movq   $0x0,-0x8(%rbp)
jmp    1302 <get_program_name+0x192>
mov    -0x168(%rbp),%rax
mov    -0x160(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x170(%rbp)
mov    -0x170(%rbp),%rdi
mov    -0x160(%rbp),%rsi
add    $0x1,%rsi
mov    -0x168(%rbp),%rax
mov    -0x160(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x170(%rbp),%rax
mov    -0x168(%rbp),%rcx
mov    -0x160(%rbp),%rdx
sub    %rdx,%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x170(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x148,%rsp
mov    %rdi,%rcx
lea    0xe79(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1050 <snprintf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1234 <get_program_name+0xc4>
mov    %eax,%ebx
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
test   %ebp,%ebp
jle    1234 <get_program_name+0xc4>
mov    %ebp,%eax
movb   $0x0,0x40(%rsp,%rax,1)
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1040 <strchr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x29,%esi
call   1040 <strchr@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     1236 <get_program_name+0xc6>
mov    %rax,%r15
test   %rax,%rax
je     1236 <get_program_name+0xc6>
sub    %r14,%r15
jb     1236 <get_program_name+0xc6>
mov    %r15d,%edi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
add    $0x1,%r14
lea    -0x1(%r15),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r15,1)
jmp    1236 <get_program_name+0xc6>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x148,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
define i8* @get_program_name(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [64 x i8]
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i64 %0, i64* %3
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%12 = load i64, i64* %3
%13 = trunc i64 %12 to i32
%14 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %1
store i8* null, i8** %2
br label %77
21: ; preds = %1
%22 = load i32, i32* %6
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 255)
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %7
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %21
store i8* null, i8** %2
br label %77
30: ; preds = %21
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = call i8* @strchr(i8* %34, i8 signext 40)
store i8* %35, i8** %8
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%37 = call i8* @strchr(i8* %36, i8 signext 41)
store i8* %37, i8** %9
%38 = load i8*, i8** %8
%39 = icmp eq i8* %38, null
br i1 %39, label %47, label %40
40: ; preds = %30
%41 = load i8*, i8** %9
%42 = icmp eq i8* %41, null
br i1 %42, label %47, label %43
43: ; preds = %40
%44 = load i8*, i8** %9
%45 = load i8*, i8** %8
%46 = icmp ult i8* %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43, %40, %30
store i8* null, i8** %2
br label %77
48: ; preds = %43
%49 = load i8*, i8** %9
%50 = load i8*, i8** %8
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
%54 = trunc i64 %53 to i32
%55 = call i64 @xmalloc(i32 %54)
%56 = inttoptr i64 %55 to i8*
store i8* %56, i8** %10
%57 = load i8*, i8** %10
%58 = load i8*, i8** %8
%59 = getelementptr inbounds i8, i8* %58, i64 1
%60 = load i8*, i8** %9
%61 = load i8*, i8** %8
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = sub nsw i64 %64, 1
%66 = trunc i64 %65 to i32
%67 = call i32 @strncpy(i8* %57, i8* %59, i32 %66)
%68 = load i8*, i8** %10
%69 = load i8*, i8** %9
%70 = load i8*, i8** %8
%71 = ptrtoint i8* %69 to i64
%72 = ptrtoint i8* %70 to i64
%73 = sub i64 %71, %72
%74 = sub nsw i64 %73, 1
%75 = getelementptr inbounds i8, i8* %68, i64 %74
store i8 0, i8* %75
%76 = load i8*, i8** %10
store i8* %76, i8** %2
br label %77
77: ; preds = %48, %47, %29, %20
%78 = load i8*, i8** %2
ret i8* %78
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
define i8* @get_program_name(i64 %0) {
%2 = alloca [64 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = trunc i64 %0 to i32
%7 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %37, label %11
11: ; preds = %1
%12 = call i32 @read(i32 %9, i8* %5, i32 255)
%13 = call i32 @close(i32 %9)
%14 = icmp slt i32 %12, 1
br i1 %14, label %37, label %15
15: ; preds = %11
%16 = zext i32 %12 to i64
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i8* @strchr(i8* %5, i8 signext 40)
%19 = call i8* @strchr(i8* %5, i8 signext 41)
%20 = icmp eq i8* %18, null
%21 = icmp eq i8* %19, null
%22 = select i1 %20, i1 true, i1 %21
%23 = icmp ult i8* %19, %18
%24 = select i1 %22, i1 true, i1 %23
br i1 %24, label %37, label %25
25: ; preds = %15
%26 = ptrtoint i8* %19 to i64
%27 = ptrtoint i8* %18 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = call i64 @xmalloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
%32 = getelementptr inbounds i8, i8* %18, i64 1
%33 = add nsw i64 %28, -1
%34 = trunc i64 %33 to i32
%35 = call i32 @strncpy(i8* %31, i8* %32, i32 %34)
%36 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %36
br label %37
37: ; preds = %15, %11, %1, %25
%38 = phi i8* [ %31, %25 ], [ null, %1 ], [ null, %11 ], [ null, %15 ]
ret i8* %38
}