#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 */
char *strdup(char *);
int strlen(char *);
char *strrchr(char *, char);
char *strstr(char *, char *);
char *imodProgName(char *fullname) {
  char *tail, *tailback, *exe;
  int indexe;
  tail = strrchr(fullname, '/');
  tailback = strrchr(fullname, '\\');
  if (tailback > tail)
    tail = tailback;
  if (!tail)
    return fullname;
  tail++;
  exe = strstr(tail, ".exe");
  indexe = strlen(tail) - 4;
  if (!exe || exe != tail + indexe)
    return tail;
  exe = strdup(tail);
  if (!exe)
    return tail;
  exe[indexe] = 0x00;
  return exe;
}


x86 assembly (clang, -O0):
<imodProgName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x5c,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1186 <imodProgName+0x46>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119e <imodProgName+0x5e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1236 <imodProgName+0xf6>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x4,%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f0 <imodProgName+0xb0>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
je     11fd <imodProgName+0xbd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1236 <imodProgName+0xf6>
mov    -0x18(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1222 <imodProgName+0xe2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1236 <imodProgName+0xf6>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imodProgName>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x5c,%esi
call   1040 <strrchr@plt>
cmp    %rbx,%rax
cmova  %rax,%rbx
test   %rbx,%rbx
je     11a4 <imodProgName+0x64>
add    $0x1,%rbx
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %r14,%r14
je     11a1 <imodProgName+0x61>
movslq %eax,%r15
lea    (%rbx,%r15,1),%rax
add    $0xfffffffffffffffc,%rax
cmp    %rax,%r14
je     11ad <imodProgName+0x6d>
mov    %rbx,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1050 <strdup@plt>
mov    %rbx,%r14
test   %rax,%rax
je     11a4 <imodProgName+0x64>
add    $0xfffffffffffffffc,%r15
movb   $0x0,(%rax,%r15,1)
mov    %rax,%r14
jmp    11a4 <imodProgName+0x64>

LLVM IR (-O0):
@.str = [5 x i8] c".exe\00"
define i8* @imodProgName(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %4
%10 = load i8*, i8** %3
%11 = call i8* @strrchr(i8* %10, i8 signext 92)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = load i8*, i8** %4
%14 = icmp ugt i8* %12, %13
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i8*, i8** %5
store i8* %16, i8** %4
br label %17
17: ; preds = %15, %1
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = load i8*, i8** %3
store i8* %21, i8** %2
br label %54
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = call i8* @strstr(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %6
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 4
store i32 %29, i32* %7
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %39
32: ; preds = %22
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = icmp ne i8* %33, %37
br i1 %38, label %39, label %41
39: ; preds = %32, %22
%40 = load i8*, i8** %4
store i8* %40, i8** %2
br label %54
41: ; preds = %32
%42 = load i8*, i8** %4
%43 = call i8* @strdup(i8* %42)
store i8* %43, i8** %6
%44 = load i8*, i8** %6
%45 = icmp ne i8* %44, null
br i1 %45, label %48, label %46
46: ; preds = %41
%47 = load i8*, i8** %4
store i8* %47, i8** %2
br label %54
48: ; preds = %41
%49 = load i8*, i8** %6
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %6
store i8* %53, i8** %2
br label %54
54: ; preds = %48, %46, %39, %20
%55 = load i8*, i8** %2
ret i8* %55
}

LLVM IR (-O2):
@.str = [5 x i8] c".exe\00"
define i8* @imodProgName(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 92)
%4 = icmp ugt i8* %3, %2
%5 = select i1 %4, i8* %3, i8* %2
%6 = icmp eq i8* %5, null
br i1 %6, label %22, label %7
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %5, i64 1
%9 = tail call i8* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @strlen(i8* %8)
%11 = icmp eq i8* %9, null
br i1 %11, label %22, label %12
12: ; preds = %7
%13 = add nsw i32 %10, -4
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %8, i64 %14
%16 = icmp eq i8* %9, %15
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = tail call i8* @strdup(i8* %8)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = getelementptr inbounds i8, i8* %18, i64 %14
store i8 0, i8* %21
br label %22
22: ; preds = %17, %7, %12, %1, %20
%23 = phi i8* [ %18, %20 ], [ %0, %1 ], [ %8, %12 ], [ %8, %7 ], [ %8, %17 ]
ret i8* %23
}