#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 /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
char *malloc(int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strrchr(char const *, char);
char *prog_of_core(const char *filename) {
  char *progname, *sp;
  long len;
  if (!(sp = strrchr(filename, '.')))
    return NULL;
  if (sp == filename)
    return NULL;
  if (strcmp(sp, ".core") != 0)
    return NULL;
  len = sp - filename + 1;
  if (len < 1)
    abort();
  if (!(progname = malloc(sizeof(char) * (unsigned long)len)))
    err(EX_OSERR, "could not malloc program name");
  strncpy(progname, filename, (size_t)len - 1);
  progname[len] = '\0';
  return progname;
}


x86 assembly (clang, -O0):
<prog_of_core>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1195 <prog_of_core+0x35>
movq   $0x0,-0x8(%rbp)
jmp    1257 <prog_of_core+0xf7>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11b0 <prog_of_core+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1257 <prog_of_core+0xf7>
mov    -0x20(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11d7 <prog_of_core+0x77>
movq   $0x0,-0x8(%rbp)
jmp    1257 <prog_of_core+0xf7>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x1,-0x28(%rbp)
jge    11fc <prog_of_core+0x9c>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    122e <prog_of_core+0xce>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1070 <err@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prog_of_core>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2e,%esi
call   1050 <strrchr@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11d8 <prog_of_core+0x78>
mov    %rax,%r15
sub    %r14,%r15
je     11d8 <prog_of_core+0x78>
lea    0xe78(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1199 <prog_of_core+0x39>
xor    %ebx,%ebx
jmp    11d8 <prog_of_core+0x78>
test   %r15,%r15
js     11e1 <prog_of_core+0x81>
lea    0x1(%r15),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c4 <prog_of_core+0x64>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1070 <err@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <strncpy@plt>
movb   $0x0,0x1(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [6 x i8] c".core\00"
@EX_OSERR = global i32 0
@.str.1 = [30 x i8] c"could not malloc program name\00"
define i8* @prog_of_core(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strrchr(i8* %7, i8 signext 46)
store i8* %8, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %51
11: ; preds = %1
%12 = load i8*, i8** %5
%13 = load i8*, i8** %3
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i8* null, i8** %2
br label %51
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* null, i8** %2
br label %51
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = load i8*, i8** %3
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = add nsw i64 %26, 1
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = icmp slt i64 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %21
%33 = load i64, i64* %6
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %4
%37 = icmp ne i8* %36, null
br i1 %37, label %41, label %38
38: ; preds = %32
%39 = load i32, i32* @EX_OSERR
%40 = call i32 @err(i32 %39, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %38, %32
%42 = load i8*, i8** %4
%43 = load i8*, i8** %3
%44 = load i64, i64* %6
%45 = sub i64 %44, 1
%46 = call i32 @strncpy(i8* %42, i8* %43, i64 %45)
%47 = load i8*, i8** %4
%48 = load i64, i64* %6
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %4
store i8* %50, i8** %2
br label %51
51: ; preds = %41, %20, %15, %10
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@.str = [6 x i8] c".core\00"
@EX_OSERR = global i32 0
@.str.1 = [30 x i8] c"could not malloc program name\00"
define i8* @prog_of_core(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%3 = icmp eq i8* %2, null
%4 = icmp eq i8* %2, %0
%5 = or i1 %3, %4
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %6
%10 = ptrtoint i8* %2 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = add nsw i64 %12, 1
%14 = icmp slt i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %9
%18 = trunc i64 %13 to i32
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @EX_OSERR
%23 = tail call i32 @err(i32 %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %17
%25 = tail call i32 @strncpy(i8* %19, i8* %0, i64 %12)
%26 = getelementptr inbounds i8, i8* %19, i64 %13
store i8 0, i8* %26
br label %27
27: ; preds = %6, %1, %24
%28 = phi i8* [ %19, %24 ], [ null, %1 ], [ null, %6 ]
ret i8* %28
}