#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*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ readlink(char *, char *, int);
int strlen(char *);
char *strrchr(char *, char);
int getrpath(char *buff, int buff_size) {
  char *p;
  memset(buff, 0, buff_size);
  if (readlink("/proc/self/exe", buff, buff_size) <= 0)
    return -1;
  if ((p = strrchr(buff, '/')) == NULL)
    return -1;
  memset(p, 0, strlen(p));
  return 0;
}


x86 assembly (clang, -O0):
<getrpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe95(%rip),%rdi
call   1030 <readlink@plt>
cmp    $0x0,%rax
jg     1186 <getrpath+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <getrpath+0x93>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ae <getrpath+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <getrpath+0x93>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrpath>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
lea    0xea8(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <readlink@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jle    1197 <getrpath+0x57>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1197 <getrpath+0x57>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1060 <memset@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
define i32 @getrpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = load i32, i32* %5
%9 = call i32 @memset(i8* %7, i32 0, i32 %8)
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %11)
%13 = icmp sle i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %25
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i8* @strrchr(i8* %16, i8 signext 47)
store i8* %17, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %25
20: ; preds = %15
%21 = load i8*, i8** %6
%22 = load i8*, i8** %6
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @memset(i8* %21, i32 0, i32 %23)
store i32 0, i32* %3
br label %25
25: ; preds = %20, %19, %14
%26 = load i32, i32* %3
ret i32 %26
}

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