#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef size_t ssize_t;
struct TYPE_3__ {
  int rm_so;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ re_postname;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ EX_SOFTWARE;
int /*<<< orphan*/ NAME_MAX;
int /*<<< orphan*/ REG_EXTENDED;
int /*<<< orphan*/ fatal(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ procfs_generic_close(int *);
int procfs_generic_open(int /*<<< orphan*/, char *);
size_t read(int, char *, int);
int regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ regerror(int, int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char *, int, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
char *strdup(char *);
char *procfs_get_procname(pid_t pid) {
  char buffer[NAME_MAX + 45];
  regex_t re_postname;
  regmatch_t re_match;
  int re_error;
  ssize_t len;
  char *pos;
  int fd;
  fd = procfs_generic_open(pid, "status");
  if (fd < 0)
    return NULL;
  len = read(fd, buffer, sizeof(buffer) - 1);
  if (len < 0) {
    procfs_generic_close(&fd);
    return NULL;
  }
  procfs_generic_close(&fd);
  memset(&re_postname, 0, sizeof(re_postname));
  re_error =
      regcomp(&re_postname, "( [[:digit:]]{1,5}){4} [[:digit:]]+,[[:digit:]]+ ",
              REG_EXTENDED);
  if (re_error) {
    regerror(re_error, &re_postname, buffer, sizeof(buffer));
    fatal(EX_SOFTWARE, "failed to compile regex: %s", buffer);
  }
  buffer[len] = '\0';
  re_error = regexec(&re_postname, buffer, 1, &re_match, 0);
  if (re_error) {
    regerror(re_error, &re_postname, buffer, sizeof(buffer));
    fatal(EX_SOFTWARE, "regex match failed: %s", buffer);
  }
  regfree(&re_postname);
  pos = buffer + re_match.rm_so;
  *pos = '\0';
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<procfs_get_procname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
add    $0x2d,%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe18(%rip),%rsi
call   1030 <procfs_generic_open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    120e <procfs_get_procname+0x6e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    134d <procfs_get_procname+0x1ad>
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x44(%rbp),%edi
sub    $0x1,%rax
mov    %eax,%edx
call   1070 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jae    1250 <procfs_get_procname+0xb0>
lea    -0x44(%rbp),%rdi
call   10b0 <procfs_generic_close@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    134d <procfs_get_procname+0x1ad>
lea    -0x44(%rbp),%rdi
call   10b0 <procfs_generic_close@plt>
lea    -0x24(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rdi
lea    0xd8a(%rip),%rsi
call   1060 <regcomp@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12be <procfs_get_procname+0x11e>
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rax
mov    -0x2c(%rbp),%edi
mov    %eax,%ecx
lea    -0x24(%rbp),%rsi
call   1080 <regerror@plt>
mov    -0x50(%rbp),%rdx
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
call   10c0 <fatal@plt>
mov    -0x50(%rbp),%rsi
mov    -0x38(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    -0x24(%rbp),%rdi
mov    $0x1,%edx
lea    -0x28(%rbp),%rcx
xor    %r8d,%r8d
call   1090 <regexec@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     131b <procfs_get_procname+0x17b>
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rax
mov    -0x2c(%rbp),%edi
mov    %eax,%ecx
lea    -0x24(%rbp),%rsi
call   1080 <regerror@plt>
mov    -0x50(%rbp),%rdx
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
call   10c0 <fatal@plt>
lea    -0x24(%rbp),%rdi
call   1040 <regfree@plt>
mov    -0x50(%rbp),%rdi
movslq -0x28(%rbp),%rcx
mov    %rdi,%rax
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procfs_get_procname>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
lea    0x2d(%rbx),%r14d
mov    %rsp,%r12
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe2c(%rip),%rsi
call   1030 <procfs_generic_open@plt>
mov    %eax,-0x2c(%rbp)
test   %eax,%eax
js     12bd <procfs_get_procname+0x11d>
add    $0x2c,%ebx
mov    %eax,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
mov    %rax,%r15
lea    -0x2c(%rbp),%rdi
call   10b0 <procfs_generic_close@plt>
lea    -0x24(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0xde5(%rip),%rsi
mov    %rbx,%rdi
call   1060 <regcomp@plt>
test   %eax,%eax
je     1257 <procfs_get_procname+0xb7>
lea    -0x24(%rbp),%rsi
mov    %eax,%edi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1080 <regerror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    %r12,%rdx
call   10c0 <fatal@plt>
movb   $0x0,(%r12,%r15,1)
lea    -0x24(%rbp),%rdi
lea    -0x28(%rbp),%rcx
mov    %r12,%rsi
mov    $0x1,%edx
xor    %r8d,%r8d
call   1090 <regexec@plt>
test   %eax,%eax
je     12a1 <procfs_get_procname+0x101>
lea    -0x24(%rbp),%rsi
mov    %eax,%edi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1080 <regerror@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    %r12,%rdx
call   10c0 <fatal@plt>
lea    -0x24(%rbp),%rdi
call   1040 <regfree@plt>
movslq -0x28(%rbp),%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rdi
call   10a0 <strdup@plt>
jmp    12bf <procfs_get_procname+0x11f>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@NAME_MAX = global i32 0
@.str = [7 x i8] c"status\00"
@.str.1 = [50 x i8] c"( [[:digit:]]{1,5}){4} [[:digit:]]+,[[:digit:]]+ \00"
@REG_EXTENDED = global i32 0
@EX_SOFTWARE = global i32 0
@.str.2 = [28 x i8] c"failed to compile regex: %s\00"
@.str.3 = [23 x i8] c"regex match failed: %s\00"
define i8* @procfs_get_procname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.TYPE_3__
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
%13 = load i32, i32* @NAME_MAX
%14 = add nsw i32 %13, 45
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %4
%17 = alloca i8, i64 %15
store i64 %15, i64* %5
%18 = load i32, i32* %3
%19 = call i32 @procfs_generic_open(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %12
br label %65
23: ; preds = %1
%24 = load i32, i32* %11
%25 = sub i64 %15, 1
%26 = trunc i64 %25 to i32
%27 = call i64 @read(i32 %24, i8* %17, i32 %26)
store i64 %27, i64* %9
%28 = load i64, i64* %9
%29 = icmp ult i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @procfs_generic_close(i32* %11)
store i8* null, i8** %2
store i32 1, i32* %12
br label %65
32: ; preds = %23
%33 = call i32 @procfs_generic_close(i32* %11)
%34 = call i32 @memset(i32* %6, i32 0, i32 4)
%35 = load i32, i32* @REG_EXTENDED
%36 = call i32 @regcomp(i32* %6, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = load i32, i32* %8
%41 = trunc i64 %15 to i32
%42 = call i32 @regerror(i32 %40, i32* %6, i8* %17, i32 %41)
%43 = load i32, i32* @EX_SOFTWARE
%44 = call i32 @fatal(i32 %43, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %45
45: ; preds = %39, %32
%46 = load i64, i64* %9
%47 = getelementptr inbounds i8, i8* %17, i64 %46
store i8 0, i8* %47
%48 = call i32 @regexec(i32* %6, i8* %17, i32 1, %struct.TYPE_3__* %7, i32 0)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %45
%52 = load i32, i32* %8
%53 = trunc i64 %15 to i32
%54 = call i32 @regerror(i32 %52, i32* %6, i8* %17, i32 %53)
%55 = load i32, i32* @EX_SOFTWARE
%56 = call i32 @fatal(i32 %55, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %17)
br label %57
57: ; preds = %51, %45
%58 = call i32 @regfree(i32* %6)
%59 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%60 = load i32, i32* %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %17, i64 %61
store i8* %62, i8** %10
%63 = load i8*, i8** %10
store i8 0, i8* %63
%64 = call i8* @strdup(i8* %17)
store i8* %64, i8** %2
store i32 1, i32* %12
br label %65
65: ; preds = %57, %30, %22
%66 = load i8*, i8** %4
%67 = load i8*, i8** %2
ret i8* %67
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@NAME_MAX = global i32 0
@.str = [7 x i8] c"status\00"
@.str.1 = [50 x i8] c"( [[:digit:]]{1,5}){4} [[:digit:]]+,[[:digit:]]+ \00"
@REG_EXTENDED = global i32 0
@EX_SOFTWARE = global i32 0
@.str.2 = [28 x i8] c"failed to compile regex: %s\00"
@.str.3 = [23 x i8] c"regex match failed: %s\00"
define noalias i8* @procfs_get_procname(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.TYPE_3__
%4 = alloca i32
%5 = load i32, i32* @NAME_MAX
%6 = add nsw i32 %5, 45
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast i32* %2 to i8*
%10 = bitcast %struct.TYPE_3__* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = tail call i32 @procfs_generic_open(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %41, label %14
14: ; preds = %1
%15 = add i32 %5, 44
%16 = call i64 @read(i32 %12, i8* %8, i32 %15)
%17 = call i32 @procfs_generic_close(i32* %4)
%18 = call i32 @memset(i32* %2, i32 0, i32 4)
%19 = load i32, i32* @REG_EXTENDED
%20 = call i32 @regcomp(i32* %2, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %14
%23 = call i32 @regerror(i32 %20, i32* %2, i8* %8, i32 %6)
%24 = load i32, i32* @EX_SOFTWARE
%25 = call i32 @fatal(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %26
26: ; preds = %22, %14
%27 = getelementptr inbounds i8, i8* %8, i64 %16
store i8 0, i8* %27
%28 = call i32 @regexec(i32* %2, i8* %8, i32 1, %struct.TYPE_3__* %3, i32 0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = call i32 @regerror(i32 %28, i32* %2, i8* %8, i32 %6)
%32 = load i32, i32* @EX_SOFTWARE
%33 = call i32 @fatal(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %34
34: ; preds = %30, %26
%35 = call i32 @regfree(i32* %2)
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 0
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %8, i64 %38
store i8 0, i8* %39
%40 = call i8* @strdup(i8* %8)
br label %41
41: ; preds = %1, %34
%42 = phi i8* [ %40, %34 ], [ null, %1 ]
ret i8* %42
}