#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 */

/* Variables and functions */
int getpid();
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strdup(char *);
char *strrchr(char *, char);
char const *getprogname(void) {
  static char *progname;
  if (!progname) {
    char buf[999];
    int len;
    sprintf(buf, "/proc/%d/exe", getpid());
    len = readlink(buf, buf, sizeof buf);
    if (len < 0 || len == sizeof buf)
      return NULL;
    buf[len] = 0;
    char *cp = strrchr(buf, '/');
    progname = strdup(cp ? cp + 1 : buf);
  }
  return progname;
}


x86 assembly (clang, -O0):
<getprogname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
cmpq   $0x0,0x2eed(%rip)
jne    124b <getprogname+0xfb>
lea    -0x3f0(%rbp),%rax
mov    %rax,-0x408(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x408(%rbp),%rdi
mov    %eax,%edx
lea    0xe72(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x3f0(%rbp),%rdi
lea    -0x3f0(%rbp),%rsi
mov    $0x3e7,%edx
call   1030 <readlink@plt>
mov    %eax,-0x3f4(%rbp)
cmpl   $0x0,-0x3f4(%rbp)
jl     11d1 <getprogname+0x81>
movslq -0x3f4(%rbp),%rax
cmp    $0x3e7,%rax
jne    11de <getprogname+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    1256 <getprogname+0x106>
movslq -0x3f4(%rbp),%rax
movb   $0x0,-0x3f0(%rbp,%rax,1)
lea    -0x3f0(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x400(%rbp)
cmpq   $0x0,-0x400(%rbp)
je     122a <getprogname+0xda>
mov    -0x400(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x410(%rbp)
jmp    1238 <getprogname+0xe8>
lea    -0x3f0(%rbp),%rax
mov    %rax,-0x410(%rbp)
mov    -0x410(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,0x2e05(%rip)
mov    0x2dfe(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getprogname>:
push   %rbx
sub    $0x3f0,%rsp
mov    0x2ef1(%rip),%rax
test   %rax,%rax
jne    11c8 <getprogname+0x78>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8e(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    $0x3e7,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     11c6 <getprogname+0x76>
cmp    $0x3e7,%eax
je     11c6 <getprogname+0x76>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,0x2e8c(%rip)
jmp    11c8 <getprogname+0x78>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@getprogname.progname = internal global i8* null
@.str = [13 x i8] c"/proc/%d/exe\00"
define i8* @getprogname() {
%1 = alloca i8*
%2 = alloca [999 x i8]
%3 = alloca i32
%4 = alloca i8*
%5 = load i8*, i8** @getprogname.progname
%6 = icmp ne i8* %5, null
br i1 %6, label %37, label %7
7: ; preds = %0
%8 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
%9 = call i32 (...) @getpid()
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
%12 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
%13 = call i32 @readlink(i8* %11, i8* %12, i32 999)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %7
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = icmp eq i64 %18, 999
br i1 %19, label %20, label %21
20: ; preds = %16, %7
store i8* null, i8** %1
br label %39
21: ; preds = %16
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
%26 = call i8* @strrchr(i8* %25, i8 signext 47)
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 1
br label %34
32: ; preds = %21
%33 = getelementptr inbounds [999 x i8], [999 x i8]* %2, i64 0, i64 0
br label %34
34: ; preds = %32, %29
%35 = phi i8* [ %31, %29 ], [ %33, %32 ]
%36 = call i8* @strdup(i8* %35)
store i8* %36, i8** @getprogname.progname
br label %37
37: ; preds = %34, %0
%38 = load i8*, i8** @getprogname.progname
store i8* %38, i8** %1
br label %39
39: ; preds = %37, %20
%40 = load i8*, i8** %1
ret i8* %40
}

LLVM IR (-O2):
@getprogname.progname = internal unnamed_addr global i8* null
@.str = [13 x i8] c"/proc/%d/exe\00"
define i8* @getprogname() {
%1 = alloca [999 x i8]
%2 = load i8*, i8** @getprogname.progname
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %21
4: ; preds = %0
%5 = getelementptr inbounds [999 x i8], [999 x i8]* %1, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @readlink(i8* %5, i8* %5, i32 999)
%9 = icmp slt i32 %8, 0
%10 = icmp eq i32 %8, 999
%11 = or i1 %9, %10
br i1 %11, label %20, label %12
12: ; preds = %4
%13 = zext i32 %8 to i64
%14 = getelementptr inbounds [999 x i8], [999 x i8]* %1, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i8* @strrchr(i8* %5, i8 signext 47)
%16 = icmp eq i8* %15, null
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = select i1 %16, i8* %5, i8* %17
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** @getprogname.progname
br label %21
20: ; preds = %4
br label %21
21: ; preds = %0, %12, %20
%22 = phi i8* [ null, %20 ], [ %19, %12 ], [ %2, %0 ]
ret i8* %22
}