#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
scalar_t__ strncasecmp(char *, char *, int);
char *get_program_name(char *argv0) {
  char *start = argv0 + strlen(argv0);
  if (strncasecmp(start - 4, ".exe", 4) == 0) {
    start -= 4;
    *start = '\0';
  }
  while (start != argv0) {
    if (*(start - 1) == '/')
      break;
    start--;
  }
  if (strlen(start) == 0) {
    fprintf(stderr, "Program name is empty.\n");
    exit(1);
  }
  return start;
}


x86 assembly (clang, -O0):
<get_program_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe86(%rip),%rsi
mov    $0x4,%edx
call   1050 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11a1 <get_program_name+0x61>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11a6 <get_program_name+0x66>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     11db <get_program_name+0x9b>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
jne    11ca <get_program_name+0x8a>
jmp    11db <get_program_name+0x9b>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    11a6 <get_program_name+0x66>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    120d <get_program_name+0xcd>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_name>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r15
lea    (%r14,%rax,1),%rbx
add    $0xfffffffffffffffc,%rbx
lea    0xea1(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <strncasecmp@plt>
test   %rax,%rax
je     1179 <get_program_name+0x39>
add    %r14,%r15
mov    %r15,%rbx
jmp    1180 <get_program_name+0x40>
movb   $0x0,(%rbx)
nopl   0x0(%rax)
cmp    %r14,%rbx
je     1199 <get_program_name+0x59>
lea    -0x1(%rbx),%rax
cmpb   $0x2f,-0x1(%rbx)
mov    %rax,%rbx
jne    1180 <get_program_name+0x40>
add    $0x1,%rax
mov    %rax,%r14
cmpb   $0x0,(%r14)
je     11a8 <get_program_name+0x68>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c".exe\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Program name is empty.\0A\00"
define i8* @get_program_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
%7 = getelementptr inbounds i8, i8* %4, i64 %6
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 -4
%10 = call i64 @strncasecmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 -4
store i8* %14, i8** %3
%15 = load i8*, i8** %3
store i8 0, i8* %15
br label %16
16: ; preds = %12, %1
br label %17
17: ; preds = %28, %16
%18 = load i8*, i8** %3
%19 = load i8*, i8** %2
%20 = icmp ne i8* %18, %19
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 -1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 47
br i1 %26, label %27, label %28
27: ; preds = %21
br label %31
28: ; preds = %21
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %3
br label %17
31: ; preds = %27, %17
%32 = load i8*, i8** %3
%33 = call i64 @strlen(i8* %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
@.str = [5 x i8] c".exe\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Program name is empty.\0A\00"
define i8* @get_program_name(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = getelementptr inbounds i8, i8* %4, i64 -4
%6 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i8 0, i8* %5
br label %9
9: ; preds = %8, %1
%10 = phi i8* [ %5, %8 ], [ %4, %1 ]
%11 = ptrtoint i8* %10 to i64
%12 = sub i64 %2, %11
%13 = getelementptr i8, i8* %10, i64 %12
br label %14
14: ; preds = %17, %9
%15 = phi i8* [ %10, %9 ], [ %18, %17 ]
%16 = icmp eq i8* %15, %0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %15, i64 -1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %14
21: ; preds = %17, %14
%22 = phi i8* [ %15, %17 ], [ %13, %14 ]
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
ret i8* %22
}