#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 */
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *realpath(char const *, int /*<<< orphan*/ *);
char *sdb_get_homedir();
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
int strlen(char const *);
char *sdb_realpath(const char *path) {
  if (!path)
    return NULL;
  if ((strlen(path) >= 2) && (path[0] == '~') && (path[1] == '/')) {
    char *homedir = sdb_get_homedir();
    char tmp[(homedir ? strlen(homedir) : 0) + strlen(path)];
    char *ret;
    if (!homedir)
      return NULL;
    snprintf(tmp, sizeof(tmp), "%s/%s", homedir, path + 2);
    ret = realpath(tmp, NULL);
    free(homedir);
    return ret;
  }
  return realpath(path, NULL);
}


x86 assembly (clang, -O0):
<sdb_realpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <sdb_realpath+0x24>
movq   $0x0,-0x8(%rbp)
jmp    129c <sdb_realpath+0x14c>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     128b <sdb_realpath+0x13b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    128b <sdb_realpath+0x13b>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    128b <sdb_realpath+0x13b>
mov    $0x0,%al
call   1070 <sdb_get_homedir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ce <sdb_realpath+0x7e>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
jmp    11d8 <sdb_realpath+0x88>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    11d8 <sdb_realpath+0x88>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1234 <sdb_realpath+0xe4>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    127f <sdb_realpath+0x12f>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
add    $0x2,%r8
lea    0xdaf(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <realpath@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
jmp    129c <sdb_realpath+0x14c>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <realpath@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdb_realpath>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     1205 <sdb_realpath+0xb5>
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     11ef <sdb_realpath+0x9f>
cmpb   $0x7e,(%rbx)
jne    11ef <sdb_realpath+0x9f>
cmpb   $0x2f,0x1(%rbx)
jne    11ef <sdb_realpath+0x9f>
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <sdb_get_homedir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1199 <sdb_realpath+0x49>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rsp,%r12
add    %r15d,%eax
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%r15
sub    %rcx,%r15
mov    %r15,%rsp
test   %r14,%r14
je     1217 <sdb_realpath+0xc7>
add    $0x2,%rbx
lea    0xe38(%rip),%rdx
mov    %r15,%rdi
mov    %eax,%esi
mov    %r14,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <realpath@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1219 <sdb_realpath+0xc9>
mov    %rbx,%rdi
xor    %esi,%esi
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <realpath@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
mov    %r12,%rsp
jmp    1207 <sdb_realpath+0xb7>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
define i8* @sdb_realpath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %62
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = icmp sge i32 %14, 2
br i1 %15, label %16, label %59
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 126
br i1 %21, label %22, label %59
22: ; preds = %16
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 47
br i1 %27, label %28, label %59
28: ; preds = %22
%29 = call i8* (...) @sdb_get_homedir()
store i8* %29, i8** %4
%30 = load i8*, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = call i32 @strlen(i8* %33)
br label %36
35: ; preds = %28
br label %36
36: ; preds = %35, %32
%37 = phi i32 [ %34, %32 ], [ 0, %35 ]
%38 = load i8*, i8** %3
%39 = call i32 @strlen(i8* %38)
%40 = add nsw i32 %37, %39
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %5
%43 = alloca i8, i64 %41
store i64 %41, i64* %6
%44 = load i8*, i8** %4
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %36
store i8* null, i8** %2
store i32 1, i32* %8
br label %57
47: ; preds = %36
%48 = trunc i64 %41 to i32
%49 = load i8*, i8** %4
%50 = load i8*, i8** %3
%51 = getelementptr inbounds i8, i8* %50, i64 2
%52 = call i32 @snprintf(i8* %43, i32 %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %49, i8* %51)
%53 = call i8* @realpath(i8* %43, i32* null)
store i8* %53, i8** %7
%54 = load i8*, i8** %4
%55 = call i32 @free(i8* %54)
%56 = load i8*, i8** %7
store i8* %56, i8** %2
store i32 1, i32* %8
br label %57
57: ; preds = %47, %46
%58 = load i8*, i8** %5
br label %62
59: ; preds = %22, %16, %12
%60 = load i8*, i8** %3
%61 = call i8* @realpath(i8* %60, i32* null)
store i8* %61, i8** %2
br label %62
62: ; preds = %59, %57, %11
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
define i8* @sdb_realpath(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %34, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp sgt i32 %4, 1
br i1 %5, label %6, label %32
6: ; preds = %3
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 126
br i1 %8, label %9, label %32
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 47
br i1 %12, label %13, label %32
13: ; preds = %9
%14 = tail call i8* (...) @sdb_get_homedir()
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @strlen(i8* %14)
br label %18
18: ; preds = %13, %16
%19 = phi i32 [ %17, %16 ], [ 0, %13 ]
%20 = tail call i32 @strlen(i8* %0)
%21 = add nsw i32 %20, %19
%22 = zext i32 %21 to i64
%23 = tail call i8* @llvm.stacksave()
%24 = alloca i8, i64 %22
br i1 %15, label %30, label %25
25: ; preds = %18
%26 = getelementptr inbounds i8, i8* %0, i64 2
%27 = call i32 @snprintf(i8* %24, i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %26)
%28 = call i8* @realpath(i8* %24, i32* null)
%29 = call i32 @free(i8* %14)
br label %30
30: ; preds = %18, %25
%31 = phi i8* [ %28, %25 ], [ null, %18 ]
br label %34
32: ; preds = %9, %6, %3
%33 = tail call i8* @realpath(i8* %0, i32* null)
br label %34
34: ; preds = %1, %32, %30
%35 = phi i8* [ %31, %30 ], [ %33, %32 ], [ null, %1 ]
ret i8* %35
}