#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ terminate(char *);
char *fix_dir_path(char *dir_path) {
  int length = strlen(dir_path);
  int new_length = length;
  if (dir_path[length - 1] != '/') {
    new_length += 1;
  }
  char *new_dir_path = (char *)malloc(new_length + 1);
  if (new_dir_path == NULL) {
    terminate("Cannot allocate memory for new path");
  }
  strcpy(new_dir_path, dir_path);
  if (new_length > length) {
    strcat(new_dir_path, "/");
  }
  return new_dir_path;
}


x86 assembly (clang, -O0):
<fix_dir_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1191 <fix_dir_path+0x41>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <fix_dir_path+0x67>
lea    0xe4e(%rip),%rdi
call   1060 <terminate@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11e0 <fix_dir_path+0x90>
mov    -0x18(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_dir_path>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
cltq
mov    -0x1(%rax,%r14,1),%bpl
xor    %ecx,%ecx
cmp    $0x2f,%bpl
setne  %cl
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118b <fix_dir_path+0x3b>
lea    0xe7a(%rip),%rdi
call   1060 <terminate@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
cmp    $0x2f,%bpl
je     11ab <fix_dir_path+0x5b>
lea    0xe81(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Cannot allocate memory for new path\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @fix_dir_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
store i32 %8, i32* %4
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 47
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
br label %20
20: ; preds = %17, %1
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @terminate(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
%30 = load i8*, i8** %5
%31 = load i8*, i8** %2
%32 = call i32 @strcpy(i8* %30, i8* %31)
%33 = load i32, i32* %4
%34 = load i32, i32* %3
%35 = icmp sgt i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i8*, i8** %5
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %36, %29
%40 = load i8*, i8** %5
ret i8* %40
}

LLVM IR (-O2):
@.str = [36 x i8] c"Cannot allocate memory for new path\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @fix_dir_path(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp ne i8 %6, 47
%8 = zext i1 %7 to i32
%9 = add i32 %2, 1
%10 = add i32 %9, %8
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 @terminate(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = tail call i32 @strcpy(i8* %12, i8* %0)
br i1 %7, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @strcat(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %16
ret i8* %12
}