#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 *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
int strlen(char const *);
char *jfs_realpath(const char *path) {
  char *jfs_realpath;
  int path_len;
  printf("Called jfs_realpath, path:%s\n", path);
  path_len = strlen(path) + 37 + 2;
  jfs_realpath = malloc(sizeof(*jfs_realpath) * path_len);
  if (!jfs_realpath) {
    printf("Failed to allocate memory for jfs_realpath.\n");
  } else {
    if (path[0] == '/') {
      snprintf(jfs_realpath, path_len, "%s%s",
               "/home/joinfs/git/joinFS/demo/.queries", path);
    } else if (path == NULL) {
      snprintf(jfs_realpath, path_len, "%s/",
               "/home/joinfs/git/joinFS/demo/.queries");
    } else {
      snprintf(jfs_realpath, path_len, "%s/%s",
               "/home/joinfs/git/joinFS/demo/.queries", path);
    }
    printf("Computed realpath:%s\n", jfs_realpath);
  }
  return jfs_realpath;
}


x86 assembly (clang, -O0):
<jfs_realpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x25,%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <jfs_realpath+0x61>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1239 <jfs_realpath+0xf9>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11d6 <jfs_realpath+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%r8
lea    0xe88(%rip),%rdx
lea    0xe86(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1227 <jfs_realpath+0xe7>
cmpq   $0x0,-0x8(%rbp)
jne    1202 <jfs_realpath+0xc2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xe87(%rip),%rdx
lea    0xe5a(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1222 <jfs_realpath+0xe2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%r8
lea    0xe66(%rip),%rdx
lea    0xe35(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1227 <jfs_realpath+0xe7>
mov    -0x10(%rbp),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jfs_realpath>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0x27,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c3 <jfs_realpath+0x73>
cmpb   $0x2f,(%r14)
lea    0xe8f(%rip),%rax
lea    0xeb3(%rip),%rdx
cmove  %rax,%rdx
lea    0xe82(%rip),%rcx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0xe98(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11cf <jfs_realpath+0x7f>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Called jfs_realpath, path:%s\0A\00"
@.str.1 = [45 x i8] c"Failed to allocate memory for jfs_realpath.\0A\00"
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [38 x i8] c"/home/joinfs/git/joinFS/demo/.queries\00"
@.str.4 = [4 x i8] c"%s/\00"
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [22 x i8] c"Computed realpath:%s\0A\00"
define i8* @jfs_realpath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 37
%10 = add nsw i32 %9, 2
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = mul i64 1, %12
%14 = trunc i64 %13 to i32
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp ne i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %47
20: ; preds = %1
%21 = load i8*, i8** %2
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = load i32, i32* %4
%29 = load i8*, i8** %2
%30 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %27, i32 %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %44
31: ; preds = %20
%32 = load i8*, i8** %2
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i8*, i8** %3
%36 = load i32, i32* %4
%37 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %35, i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %43
38: ; preds = %31
%39 = load i8*, i8** %3
%40 = load i32, i32* %4
%41 = load i8*, i8** %2
%42 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %39, i32 %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %38, %34
br label %44
44: ; preds = %43, %26
%45 = load i8*, i8** %3
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %44, %18
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@.str = [30 x i8] c"Called jfs_realpath, path:%s\0A\00"
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [38 x i8] c"/home/joinfs/git/joinFS/demo/.queries\00"
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [22 x i8] c"Computed realpath:%s\0A\00"
@str = [44 x i8] c"Failed to allocate memory for jfs_realpath.\00"
define i8* @jfs_realpath(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 39
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %15
9: ; preds = %1
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 47
%12 = select i1 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0)
%13 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 %4, i8* %12, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %5)
br label %15
15: ; preds = %9, %7
ret i8* %5
}