#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 */
size_t readlink(char *, char *, size_t);
char *strrchr(char *, char);
size_t getHostAppDir(char *path, size_t bufSize) {
  if (path == NULL || bufSize == 0) {
    return 0;
  }
  size_t len;
  char sep;
  len = readlink("/proc/self/exe", path, bufSize - 1);
  sep = '/';
  path[len] = '\0';
  char *p = strrchr(path, sep);
  if (p != NULL) {
    p[1] = '\0';
    len = 1 + p - path;
  }
  return len;
}


x86 assembly (clang, -O0):
<getHostAppDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <getHostAppDir+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1153 <getHostAppDir+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11be <getHostAppDir+0x9e>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
lea    0xe9a(%rip),%rdi
call   1030 <readlink@plt>
mov    %rax,-0x20(%rbp)
movb   $0x2f,-0x21(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
movsbl -0x21(%rbp),%esi
call   1040 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b6 <getHostAppDir+0x96>
mov    -0x30(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHostAppDir>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1171 <getHostAppDir+0x51>
mov    %rsi,%rdx
test   %rsi,%rsi
je     1171 <getHostAppDir+0x51>
mov    %rdi,%r14
add    $0xffffffffffffffff,%rdx
lea    0xebf(%rip),%rdi
mov    %r14,%rsi
call   1030 <readlink@plt>
mov    %rax,%rbx
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1171 <getHostAppDir+0x51>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,0x1(%rax)
sub    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i64 @getHostAppDir(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %17, label %6
6: ; preds = %2
%7 = add i64 %1, -1
%8 = tail call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %7)
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %9
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %13
%14 = ptrtoint i8* %13 to i64
%15 = ptrtoint i8* %0 to i64
%16 = sub i64 %14, %15
br label %17
17: ; preds = %6, %12, %2
%18 = phi i64 [ 0, %2 ], [ %16, %12 ], [ %8, %6 ]
ret i64 %18
}