#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 *pathdup_addsubdir(char *path, char *subdir) {
  int addslash = (path[strlen(path) - 1] != '/');
  char *newpath = NULL;
  int k = addslash ? 2 : 1;
  newpath = malloc(strlen(path) + strlen(subdir) + k);
  strcpy(newpath, path);
  if (addslash) {
    strcat(newpath, "/");
  }
  strcat(newpath, subdir);
  return newpath;
}


x86 assembly (clang, -O0):
<pathdup_addsubdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edx
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%edi
add    %eax,%edi
add    -0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
cmpl   $0x0,-0x14(%rbp)
je     11ea <pathdup_addsubdir+0xaa>
mov    -0x20(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathdup_addsubdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
cltq
mov    -0x1(%rax,%r15,1),%r12b
xor    %ebp,%ebp
cmp    $0x2f,%r12b
sete   %bpl
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %eax,%ebx
sub    %ebp,%ebx
add    $0x2,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
cmp    $0x2f,%r12b
je     11a7 <pathdup_addsubdir+0x67>
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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