#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 *clone(char *);
scalar_t__ strlen(char *);
char *get_program_path(char *argv0) {
  char *end;
  int empty = 1;
  argv0 = clone(argv0);
  for (end = argv0 + strlen(argv0); argv0 <= end; end--) {
    if (*end == '/') {
      *end = '\0';
      empty = 0;
      break;
    }
  }
  if (empty || strlen(argv0) == 0)
    return clone(".");
  return argv0;
}


x86 assembly (clang, -O0):
<get_program_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <clone@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11a6 <get_program_path+0x86>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1190 <get_program_path+0x70>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x1c(%rbp)
jmp    11a6 <get_program_path+0x86>
jmp    1195 <get_program_path+0x75>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    115f <get_program_path+0x3f>
cmpl   $0x0,-0x1c(%rbp)
jne    11c3 <get_program_path+0xa3>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    11d8 <get_program_path+0xb8>
lea    0xe36(%rip),%rdi
call   1030 <clone@plt>
mov    %rax,-0x8(%rbp)
jmp    11e0 <get_program_path+0xc0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_path>:
push   %rbx
call   1030 <clone@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
js     115d <get_program_path+0x3d>
add    %rbx,%rax
nopl   0x0(%rax)
cmpb   $0x2f,(%rax)
je     1150 <get_program_path+0x30>
add    $0xffffffffffffffff,%rax
cmp    %rax,%rbx
jbe    1140 <get_program_path+0x20>
jmp    115d <get_program_path+0x3d>
movb   $0x0,(%rax)
cmpb   $0x0,(%rbx)
je     115d <get_program_path+0x3d>
mov    %rbx,%rax
pop    %rbx
ret
lea    0xe9c(%rip),%rdi
pop    %rbx
jmp    1030 <clone@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @get_program_path(i8* %0) {
%2 = tail call i8* @clone(i8* %0)
%3 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%4 = icmp slt i64 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = getelementptr inbounds i8, i8* %2, i64 %3
br label %7
7: ; preds = %5, %11
%8 = phi i8* [ %12, %11 ], [ %6, %5 ]
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %8, i64 -1
%13 = icmp ugt i8* %2, %12
br i1 %13, label %17, label %7
14: ; preds = %7
store i8 0, i8* %8
%15 = load i8, i8* %2
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11, %1, %14
%18 = tail call i8* @clone(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %14, %17
%20 = phi i8* [ %18, %17 ], [ %2, %14 ]
ret i8* %20
}