#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int O_CLOEXEC;
int O_NOCTTY;
int O_RDONLY;
scalar_t__ errno;
int /*<<< orphan*/ eslib_string_sprintf(char *, int, int /*<<< orphan*/ *,
                                        char *, ...);
int /*<<< orphan*/ getpid();
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
char *eslib_proc_getname() {
  static char name[32];
  static int once = 1;
  if (once) {
    char buf[64];
    int fd;
    if (eslib_string_sprintf(buf, sizeof(buf), NULL, "/proc/%d/cmdline",
                             getpid()))
      goto err;
    fd = open(buf, O_CLOEXEC | O_RDONLY | O_NOCTTY);
    if (fd == -1) {
      printf("could not open %s\n", buf);
      goto err;
    }
    while (1) {
      int r = read(fd, name, 32 - 1);
      if (r == -1 && (errno == EINTR || errno == EAGAIN))
        continue;
      else if (r > 0)
        break;
      else
        goto err;
    }
    once = 0;
  }
  name[32 - 1] = '\0';
  return name;
err:
  once = 0;
  eslib_string_sprintf(name, 32, NULL, "no-procname");
  return name;
}


x86 assembly (clang, -O0):
<eslib_proc_getname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
cmpl   $0x0,0x2ee9(%rip)
je     1263 <eslib_proc_getname+0x113>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%r8d
mov    $0x40,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    0xe75(%rip),%rcx
mov    $0x0,%al
call   1030 <eslib_string_sprintf@plt>
cmp    $0x0,%eax
je     11a0 <eslib_proc_getname+0x50>
jmp    127a <eslib_proc_getname+0x12a>
lea    -0x50(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2e34(%rip),%rax
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11e6 <eslib_proc_getname+0x96>
lea    -0x50(%rbp),%rsi
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
jmp    127a <eslib_proc_getname+0x12a>
jmp    11eb <eslib_proc_getname+0x9b>
mov    -0x54(%rbp),%edi
lea    0x2e6b(%rip),%rsi
mov    $0x1f,%edx
call   1060 <read@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    1245 <eslib_proc_getname+0xf5>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
je     1240 <eslib_proc_getname+0xf0>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
jne    1245 <eslib_proc_getname+0xf5>
jmp    11eb <eslib_proc_getname+0x9b>
cmpl   $0x0,-0x58(%rbp)
jle    1254 <eslib_proc_getname+0x104>
jmp    1259 <eslib_proc_getname+0x109>
jmp    127a <eslib_proc_getname+0x12a>
movl   $0x0,0x2de5(%rip)
movb   $0x0,0x2e15(%rip)
lea    0x2def(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <eslib_proc_getname+0x15d>
movl   $0x0,0x2dc4(%rip)
lea    0x2dd5(%rip),%rdi
mov    $0x20,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    0xd89(%rip),%rcx
mov    $0x0,%al
call   1030 <eslib_string_sprintf@plt>
lea    0x2db7(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_proc_getname>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
cmpb   $0x0,0x2f1c(%rip)
je     116f <eslib_proc_getname+0x1f>
movb   $0x0,0x2f12(%rip)
jmp    11b8 <eslib_proc_getname+0x68>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe83(%rip),%rcx
mov    %rsp,%rdi
mov    $0x40,%esi
xor    %edx,%edx
mov    %eax,%r8d
xor    %eax,%eax
call   1030 <eslib_string_sprintf@plt>
test   %eax,%eax
je     11cd <eslib_proc_getname+0x7d>
movb   $0x1,0x2ee4(%rip)
lea    0x2ebd(%rip),%rdi
lea    0xe7a(%rip),%rcx
mov    $0x20,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <eslib_string_sprintf@plt>
lea    0x2ea1(%rip),%rax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2de4(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1248 <eslib_proc_getname+0xf8>
mov    %eax,%ebx
lea    0x2e62(%rip),%r14
mov    0x2dcb(%rip),%r15
mov    0x2dbc(%rip),%r12
mov    0x2d9d(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1f,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    125c <eslib_proc_getname+0x10c>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1220 <eslib_proc_getname+0xd0>
cmp    0x0(%r13),%rax
je     1220 <eslib_proc_getname+0xd0>
jmp    1195 <eslib_proc_getname+0x45>
lea    0xdc2(%rip),%rdi
mov    %rsp,%rsi
call   1050 <printf@plt>
jmp    1195 <eslib_proc_getname+0x45>
test   %eax,%eax
jle    1195 <eslib_proc_getname+0x45>
movb   $0x1,0x2e15(%rip)
jmp    1166 <eslib_proc_getname+0x16>

LLVM IR (-O0):
@eslib_proc_getname.name = internal global [32 x i8] zeroinitializer
@eslib_proc_getname.once = internal global i32 1
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_CLOEXEC = global i32 0
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [19 x i8] c"could not open %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.2 = [12 x i8] c"no-procname\00"
define i8* @eslib_proc_getname() {
%1 = alloca i8*
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @eslib_proc_getname.once
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %47
7: ; preds = %0
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, i32, i32*, i8*, ...) @eslib_string_sprintf(i8* %8, i32 64, i32* null, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %7
br label %48
13: ; preds = %7
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* @O_CLOEXEC
%16 = load i32, i32* @O_RDONLY
%17 = or i32 %15, %16
%18 = load i32, i32* @O_NOCTTY
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %48
26: ; preds = %13
br label %27
27: ; preds = %26, %40
%28 = load i32, i32* %3
%29 = call i32 @read(i32 %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0), i32 31)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %41
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36, %32
br label %27
41: ; preds = %36, %27
%42 = load i32, i32* %4
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
br label %46
45: ; preds = %41
br label %48
46: ; preds = %44
store i32 0, i32* @eslib_proc_getname.once
br label %47
47: ; preds = %46, %0
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 31)
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0), i8** %1
br label %50
48: ; preds = %45, %23, %12
store i32 0, i32* @eslib_proc_getname.once
%49 = call i32 (i8*, i32, i32*, i8*, ...) @eslib_string_sprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0), i32 32, i32* null, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0), i8** %1
br label %50
50: ; preds = %48, %47
%51 = load i8*, i8** %1
ret i8* %51
}

LLVM IR (-O2):
@eslib_proc_getname.name = internal global [32 x i8] zeroinitializer
@eslib_proc_getname.once = internal unnamed_addr global i1 false
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_CLOEXEC = global i32 0
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [19 x i8] c"could not open %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.2 = [12 x i8] c"no-procname\00"
define i8* @eslib_proc_getname() {
%1 = alloca [64 x i8]
%2 = load i1, i1* @eslib_proc_getname.once
br i1 %2, label %31, label %3
3: ; preds = %0
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 (i8*, i32, i32*, i8*, ...) @eslib_string_sprintf(i8* %4, i32 64, i32* null, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %32
8: ; preds = %3
%9 = load i32, i32* @O_CLOEXEC
%10 = load i32, i32* @O_RDONLY
%11 = or i32 %10, %9
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %4, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %32
18: ; preds = %8, %21
%19 = call i32 @read(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0), i32 31)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %22, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %18, label %32
28: ; preds = %18
%29 = icmp sgt i32 %19, 0
br i1 %29, label %30, label %32
30: ; preds = %28
store i1 true, i1* @eslib_proc_getname.once
br label %31
31: ; preds = %30, %0
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 31)
br label %34
32: ; preds = %21, %28, %16, %3
store i1 true, i1* @eslib_proc_getname.once
%33 = call i32 (i8*, i32, i32*, i8*, ...) @eslib_string_sprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0), i32 32, i32* null, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %31
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @eslib_proc_getname.name, i64 0, i64 0)
}