#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*/ tmp;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *, char *);
scalar_t__ strlen(char *);
scalar_t__ strspn(char *, char *);
int get_process_name(char *pid, char *buf) {
  if (strspn(pid, "0123456789") != strlen(pid)) {
    return 0;
  }
  char tmp[256];
  snprintf(tmp, sizeof(tmp), "/proc/%s/stat", pid);
  FILE *f = fopen(tmp, "r");
  if (f == NULL) {
    return 0;
  }
  if (fgets(tmp, sizeof(tmp), f) == NULL) {
    fclose(f);
    return 0;
  }
  fclose(f);
  int unused;
  sscanf(tmp, "%d (%[^)]s", &unused, buf);
  return 1;
}


x86 assembly (clang, -O0):
<get_process_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <strspn@plt>
mov    %rax,-0x138(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x138(%rbp),%rax
cmp    %rcx,%rax
je     11c2 <get_process_name+0x52>
movl   $0x0,-0x4(%rbp)
jmp    127d <get_process_name+0x10d>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe32(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1212 <get_process_name+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    127d <get_process_name+0x10d>
lea    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    124c <get_process_name+0xdc>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    127d <get_process_name+0x10d>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xdb1(%rip),%rsi
lea    -0x12c(%rbp),%rdx
call   1080 <sscanf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_process_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe76(%rip),%rsi
call   1060 <strspn@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %r15d,%r15d
cmp    %rax,%rbp
jne    1219 <get_process_name+0xa9>
lea    0xe62(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
lea    0xe54(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1219 <get_process_name+0xa9>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rbp,%rbp
je     1219 <get_process_name+0xa9>
lea    0xe1a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
mov    %r14,%rcx
call   1080 <sscanf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"0123456789\00"
@.str.1 = [14 x i8] c"/proc/%s/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"%d (%[^)]s\00"
define i32 @get_process_name(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i64 @strspn(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
%13 = icmp ne i64 %10, %12
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %38
15: ; preds = %2
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = load i8*, i8** %4
%18 = call i32 @snprintf(i8* %16, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %15
store i32 0, i32* %3
br label %38
24: ; preds = %15
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%26 = load i32*, i32** %7
%27 = call i32* @fgets(i8* %25, i32 256, i32* %26)
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %3
br label %38
32: ; preds = %24
%33 = load i32*, i32** %7
%34 = call i32 @fclose(i32* %33)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%36 = load i8*, i8** %5
%37 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %8, i8* %36)
store i32 1, i32* %3
br label %38
38: ; preds = %32, %29, %23, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [11 x i8] c"0123456789\00"
@.str.1 = [14 x i8] c"/proc/%s/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"%d (%[^)]s\00"
define i32 @get_process_name(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = tail call i64 @strspn(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %22
8: ; preds = %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %20, label %13
13: ; preds = %8
%14 = call i32* @fgets(i8* %9, i32 256, i32* %11)
%15 = icmp eq i32* %14, null
%16 = call i32 @fclose(i32* %11)
br i1 %15, label %20, label %17
17: ; preds = %13
%18 = bitcast i32* %4 to i8*
%19 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %4, i8* %1)
br label %20
20: ; preds = %13, %8, %17
%21 = phi i32 [ 1, %17 ], [ 0, %8 ], [ 0, %13 ]
br label %22
22: ; preds = %2, %20
%23 = phi i32 [ %21, %20 ], [ 0, %2 ]
ret i32 %23
}