#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 uint;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char const *);
char const *strncpy(char *, char const *, int);
int file_path_dir(const char *src, char *dst, uint sz) {
  if (strlen(src) + 1 >= sz)
    return -1;
  memset(dst, 0, sz);
  int len = strlen(strncpy(dst, src, sz));
  if (dst[len - 1] != '/')
    dst[len] = '/';
  return 0;
}


x86 assembly (clang, -O0):
<file_path_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jl     1164 <file_path_dir+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <file_path_dir+0x8a>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11b3 <file_path_dir+0x83>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_path_dir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <strlen@plt>
add    $0x1,%eax
mov    $0xffffffff,%r14d
cmp    %ebp,%eax
jge    1184 <file_path_dir+0x54>
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %rax,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     1184 <file_path_dir+0x54>
movb   $0x2f,(%r15,%rax,1)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @file_path_dir(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = load i32, i32* %7
%13 = icmp sge i32 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %38
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = call i32 @memset(i8* %16, i32 0, i32 %17)
%19 = load i8*, i8** %6
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = call i8* @strncpy(i8* %19, i8* %20, i32 %21)
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %8
%24 = load i8*, i8** %6
%25 = load i32, i32* %8
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 47
br i1 %31, label %32, label %37
32: ; preds = %15
%33 = load i8*, i8** %6
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 47, i8* %36
br label %37
37: ; preds = %32, %15
store i32 0, i32* %4
br label %38
38: ; preds = %37, %14
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
define i32 @file_path_dir(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = icmp slt i32 %5, %2
br i1 %6, label %7, label %19
7: ; preds = %3
%8 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%9 = tail call i8* @strncpy(i8* %1, i8* %0, i32 %2)
%10 = tail call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = sext i32 %10 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 47, i8* %18
br label %19
19: ; preds = %7, %16, %3
%20 = phi i32 [ -1, %3 ], [ 0, %16 ], [ 0, %7 ]
ret i32 %20
}