#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*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *dupdir(char *dir) {
  int n;
  char *p;
  n = strlen(dir);
  if (dir[n - 1] != '/') {
    p = malloc(n + 2);
    strcpy(p, dir);
    strcat(p, "/");
  } else {
    p = malloc(n + 1);
    strcpy(p, dir);
  }
  return p;
}


x86 assembly (clang, -O0):
<dupdir>:
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    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11a3 <dupdir+0x63>
mov    -0xc(%rbp),%edi
add    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1060 <strcat@plt>
jmp    11bf <dupdir+0x7f>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupdir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
movslq %eax,%rcx
cmpb   $0x2f,-0x1(%rcx,%r14,1)
jne    1171 <dupdir+0x31>
add    $0x1,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1198 <dupdir+0x58>
add    $0x2,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @dupdir(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 eq i8 %6, 47
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = add nsw i32 %2, 2
%10 = tail call i8* @malloc(i32 %9)
%11 = tail call i32 @strcpy(i8* %10, i8* %0)
%12 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %17
13: ; preds = %1
%14 = add nsw i32 %2, 1
%15 = tail call i8* @malloc(i32 %14)
%16 = tail call i32 @strcpy(i8* %15, i8* %0)
br label %17
17: ; preds = %13, %8
%18 = phi i8* [ %10, %8 ], [ %15, %13 ]
ret i8* %18
}