#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 strlen(char *);
char *get_program_name(char *argv[]) {
  char *name = argv[0] + strlen(argv[0]);
  while (name != argv[0]) {
    if (*name == '/' || *name == '\\') {
      name++;
      break;
    }
    name--;
  }
  return name;
}


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),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%rax
je     1196 <get_program_name+0x86>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1174 <get_program_name+0x64>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    1185 <get_program_name+0x75>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1196 <get_program_name+0x86>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1143 <get_program_name+0x33>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_name>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
add    %rbx,%rax
mov    (%r14),%rcx
cmp    %rcx,%rax
je     114f <get_program_name+0x3f>
nop
movzbl (%rax),%edx
cmp    $0x5c,%dl
je     114b <get_program_name+0x3b>
cmp    $0x2f,%dl
je     114b <get_program_name+0x3b>
add    $0xffffffffffffffff,%rax
cmp    %rcx,%rax
jne    1130 <get_program_name+0x20>
mov    %rcx,%rax
jmp    114f <get_program_name+0x3f>
add    $0x1,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @get_program_name(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
store i8** %0, i8*** %2
%4 = load i8**, i8*** %2
%5 = getelementptr inbounds i8*, i8** %4, i64 0
%6 = load i8*, i8** %5
%7 = load i8**, i8*** %2
%8 = getelementptr inbounds i8*, i8** %7, i64 0
%9 = load i8*, i8** %8
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %6, i64 %11
store i8* %12, i8** %3
br label %13
13: ; preds = %32, %1
%14 = load i8*, i8** %3
%15 = load i8**, i8*** %2
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = icmp ne i8* %14, %17
br i1 %18, label %19, label %35
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 47
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 92
br i1 %28, label %29, label %32
29: ; preds = %24, %19
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %3
br label %35
32: ; preds = %24
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i32 -1
store i8* %34, i8** %3
br label %13
35: ; preds = %29, %13
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
define i8* @get_program_name(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = tail call i32 @strlen(i8* %2)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %2, i64 %4
%6 = load i8*, i8** %0
%7 = icmp eq i8* %5, %6
br i1 %7, label %21, label %8
8: ; preds = %1
%9 = ptrtoint i8* %6 to i64
%10 = ptrtoint i8* %2 to i64
%11 = sub i64 %9, %10
%12 = getelementptr i8, i8* %2, i64 %11
br label %13
13: ; preds = %8, %18
%14 = phi i8* [ %19, %18 ], [ %5, %8 ]
%15 = load i8, i8* %14
switch i8 %15, label %18 [
i8 47, label %16
i8 92, label %16
]
16: ; preds = %13, %13
%17 = getelementptr inbounds i8, i8* %14, i64 1
br label %21
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %14, i64 -1
%20 = icmp eq i8* %19, %6
br i1 %20, label %21, label %13
21: ; preds = %18, %1, %16
%22 = phi i8* [ %17, %16 ], [ %5, %1 ], [ %12, %18 ]
ret i8* %22
}