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

/* Variables and functions */
char *Basename(char *);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
size_t errno;
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ get_log_fd();
int /*<<< orphan*/ memchr(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *sys_errlist;
char *get_prog_name() {
  FILE *p = fopen("/proc/self/cmdline", "rb");
  assert(p && "Failed to read /proc");
  static char s[128];
  memset(s, 0, sizeof(s));
  int nread = fread(s, 1, sizeof(s), p);
  if (nread < 0 || !memchr(s, 0, sizeof(s))) {
    dprintf(get_log_fd(),
            "libpregrind.so: "
            "fread() from /proc/self/exe failed: %s\n",
            sys_errlist[errno]);
    abort();
  }
  return Basename(s);
}


x86 assembly (clang, -O0):
<get_prog_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe61(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x8(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0xd(%rbp)
je     11c9 <get_prog_name+0x39>
mov    $0x1,%al
mov    %al,-0xd(%rbp)
jmp    11c9 <get_prog_name+0x39>
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
lea    0x2ea3(%rip),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rcx
lea    0x2e8c(%rip),%rdi
mov    $0x1,%esi
mov    $0x80,%edx
call   1040 <fread@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     122c <get_prog_name+0x9c>
lea    0x2e69(%rip),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memchr@plt>
cmp    $0x0,%eax
jne    125f <get_prog_name+0xcf>
mov    $0x0,%al
call   1090 <get_log_fd@plt>
mov    %eax,%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdd8(%rip),%rsi
call   1060 <dprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0x2e1a(%rip),%rdi
call   10a0 <Basename@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_prog_name>:
push   %r14
push   %rbx
push   %rax
lea    0xe65(%rip),%rdi
lea    0xe71(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1080 <assert@plt>
lea    0x2ec1(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %eax,%eax
js     1211 <get_prog_name+0x81>
lea    0x2e92(%rip),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memchr@plt>
test   %eax,%eax
je     1211 <get_prog_name+0x81>
lea    0x2e7b(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10a0 <Basename@plt>
xor    %eax,%eax
call   1090 <get_log_fd@plt>
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2daf(%rip),%rdx
mov    (%rdx),%rdx
mov    (%rcx,%rdx,4),%edx
lea    0xde0(%rip),%rsi
mov    %eax,%edi
call   1060 <dprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [21 x i8] c"Failed to read /proc\00"
@get_prog_name.s = internal global [128 x i8] zeroinitializer
@.str.3 = [56 x i8] c"libpregrind.so: fread() from /proc/self/exe failed: %s\0A\00"
@sys_errlist = global i32* null
@errno = global i64 0
define i8* @get_prog_name() {
%1 = alloca i32*
%2 = alloca i32
%3 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp ne i32* %4, null
br i1 %5, label %6, label %7
6: ; preds = %0
br label %7
7: ; preds = %6, %0
%8 = phi i1 [ false, %0 ], [ true, %6 ]
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = call i32 @memset(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0), i32 0, i32 128)
%12 = load i32*, i32** %1
%13 = call i32 @fread(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0), i32 1, i32 128, i32* %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = call i32 @memchr(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0), i32 0, i32 128)
%18 = icmp ne i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16, %7
%20 = call i32 (...) @get_log_fd()
%21 = load i32*, i32** @sys_errlist
%22 = load i64, i64* @errno
%23 = getelementptr inbounds i32, i32* %21, i64 %22
%24 = load i32, i32* %23
%25 = call i32 @dprintf(i32 %20, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %16
%28 = call i8* @Basename(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0))
ret i8* %28
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@.str.1 = [3 x i8] c"rb\00"
@get_prog_name.s = internal global [128 x i8] zeroinitializer
@.str.3 = [56 x i8] c"libpregrind.so: fread() from /proc/self/exe failed: %s\0A\00"
@sys_errlist = global i32* null
@errno = global i64 0
define i8* @get_prog_name() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp ne i32* %1, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @memset(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0), i32 0, i32 128)
%6 = tail call i32 @fread(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0), i32 1, i32 128, i32* %1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = tail call i32 @memchr(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0), i32 0, i32 128)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %8, %0
%12 = tail call i32 (...) @get_log_fd()
%13 = load i32*, i32** @sys_errlist
%14 = load i64, i64* @errno
%15 = getelementptr inbounds i32, i32* %13, i64 %14
%16 = load i32, i32* %15
%17 = tail call i32 @dprintf(i32 %12, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %8
%20 = tail call i8* @Basename(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_prog_name.s, i64 0, i64 0))
ret i8* %20
}