#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*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ rc_setvar(char *, int, char **);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *strdup(char *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
void sys_initpath(char *exe) {
  char *buf, *home, *p;
  home = strdup(exe);
  p = strrchr(home, '/');
  if (p)
    *p = 0;
  else {
    buf = ".";
    rc_setvar("rcpath", 1, &buf);
    rc_setvar("savedir", 1, &buf);
    return;
  }
  buf = malloc(strlen(home) + 8);
  sprintf(buf, ".;%s/", home);
  rc_setvar("rcpath", 1, &buf);
  sprintf(buf, ".");
  rc_setvar("savedir", 1, &buf);
  free(buf);
}


x86 assembly (clang, -O0):
<sys_initpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b2 <sys_initpath+0x42>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    11ec <sys_initpath+0x7c>
lea    0xe47(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe3e(%rip),%rdi
mov    $0x1,%esi
lea    -0x10(%rbp),%rdx
call   1060 <rc_setvar@plt>
lea    0xe30(%rip),%rdi
mov    $0x1,%esi
lea    -0x10(%rbp),%rdx
call   1060 <rc_setvar@plt>
jmp    1260 <sys_initpath+0xf0>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x8,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    0xde0(%rip),%rdi
mov    $0x1,%esi
lea    -0x10(%rbp),%rdx
call   1060 <rc_setvar@plt>
mov    -0x10(%rbp),%rdi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    0xdc0(%rip),%rdi
mov    $0x1,%esi
lea    -0x10(%rbp),%rdx
call   1060 <rc_setvar@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_initpath>:
push   %rbx
sub    $0x10,%rsp
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11ff <sys_initpath+0x8f>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x8(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
lea    0xe62(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
lea    0xe3f(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    $0x1,%esi
mov    %rbx,%rdx
call   1060 <rc_setvar@plt>
mov    0x8(%rsp),%rax
movw   $0x2e,(%rax)
lea    0xe23(%rip),%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1060 <rc_setvar@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1238 <sys_initpath+0xc8>
lea    0xdfa(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xdf0(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    $0x1,%esi
mov    %rbx,%rdx
call   1060 <rc_setvar@plt>
lea    0xdde(%rip),%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1060 <rc_setvar@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [7 x i8] c"rcpath\00"
@.str.2 = [8 x i8] c"savedir\00"
@.str.3 = [6 x i8] c".;%s/\00"
define void @sys_initpath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @strdup(i8* %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i8*, i8** %5
store i8 0, i8* %13
br label %17
14: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3
%15 = call i32 @rc_setvar(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i8** %3)
%16 = call i32 @rc_setvar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 1, i8** %3)
br label %31
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i64 @strlen(i8* %18)
%20 = add nsw i64 %19, 8
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = load i8*, i8** %4
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = call i32 @rc_setvar(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i8** %3)
%26 = load i8*, i8** %3
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @rc_setvar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 1, i8** %3)
%29 = load i8*, i8** %3
%30 = call i32 @free(i8* %29)
br label %31
31: ; preds = %17, %14
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [7 x i8] c"rcpath\00"
@.str.2 = [8 x i8] c"savedir\00"
@.str.3 = [6 x i8] c".;%s/\00"
define void @sys_initpath(i8* nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %4, i8 signext 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %18, label %7
7: ; preds = %1
store i8 0, i8* %5
%8 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%9 = add nsw i64 %8, 8
%10 = tail call i8* @malloc(i64 %9)
store i8* %10, i8** %2
%11 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%12 = call i32 @rc_setvar(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i8** %2)
%13 = bitcast i8** %2 to i16**
%14 = load i16*, i16** %13
store i16 46, i16* %14
%15 = call i32 @rc_setvar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 1, i8** %2)
%16 = load i8*, i8** %2
%17 = call i32 @free(i8* %16)
br label %21
18: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
%19 = call i32 @rc_setvar(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i8** %2)
%20 = call i32 @rc_setvar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 1, i8** %2)
br label %21
21: ; preds = %7, %18
ret void
}