#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 */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char *,
                            char const *);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
char *uf2_basename(char const *, int /*<<< orphan*/);
void uf2_map(char *dst, size_t dstlen, const char *dstdir, const char *filename,
             int use_paths) {
  const char *basename = uf2_basename(filename, 0);
  char *foldername;
  if (use_paths) {
    foldername = strdup(filename);
    char *slash = strchr(foldername, '/');
    if (slash != NULL)
      *slash = '\0';
    if (strcmp(foldername, "..") == 0)
      strcpy(foldername, ".");
  } else {
    foldername = strdup(".");
  }
  snprintf(dst, dstlen, "%s/%s/%s", dstdir, foldername, basename);
  free(foldername);
}


x86 assembly (clang, -O0):
<uf2_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1070 <uf2_basename@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1205 <uf2_map+0x95>
mov    -0x20(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11d6 <uf2_map+0x66>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1200 <uf2_map+0x90>
mov    -0x38(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1215 <uf2_map+0xa5>
lea    0xdf5(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    -0x30(%rbp),%r9
lea    0xdd3(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uf2_map>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rcx,%rdi
xor    %esi,%esi
call   1070 <uf2_basename@plt>
mov    %rax,%r13
test   %ebp,%ebp
je     11e0 <uf2_map+0x70>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11bb <uf2_map+0x4b>
movb   $0x0,(%rax)
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11ef <uf2_map+0x7f>
lea    0xe2b(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    11ef <uf2_map+0x7f>
lea    0xe1a(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
lea    0xe0d(%rip),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
mov    %rbx,%r8
mov    %r13,%r9
call   1060 <snprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [9 x i8] c"%s/%s/%s\00"
define void @uf2_map(i8* %0, i64 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%14 = load i8*, i8** %9
%15 = call i8* @uf2_basename(i8* %14, i32 0)
store i8* %15, i8** %11
%16 = load i32, i32* %10
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %5
%19 = load i8*, i8** %9
%20 = call i8* @strdup(i8* %19)
store i8* %20, i8** %12
%21 = load i8*, i8** %12
%22 = call i8* @strchr(i8* %21, i8 signext 47)
store i8* %22, i8** %13
%23 = load i8*, i8** %13
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = load i8*, i8** %13
store i8 0, i8* %26
br label %27
27: ; preds = %25, %18
%28 = load i8*, i8** %12
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i8*, i8** %12
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %27
br label %37
35: ; preds = %5
%36 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %36, i8** %12
br label %37
37: ; preds = %35, %34
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = load i8*, i8** %8
%41 = load i8*, i8** %12
%42 = load i8*, i8** %11
%43 = call i32 @snprintf(i8* %38, i64 %39, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %41, i8* %42)
%44 = load i8*, i8** %12
%45 = call i32 @free(i8* %44)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [9 x i8] c"%s/%s/%s\00"
define void @uf2_map(i8* %0, i64 %1, i8* %2, i8* %3, i32 %4) {
%6 = tail call i8* @uf2_basename(i8* %3, i32 0)
%7 = icmp eq i32 %4, 0
br i1 %7, label %18, label %8
8: ; preds = %5
%9 = tail call i8* @strdup(i8* %3)
%10 = tail call i8* @strchr(i8* dereferenceable(1) %9, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %8
store i8 0, i8* %10
br label %13
13: ; preds = %12, %8
%14 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = tail call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %20
18: ; preds = %5
%19 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %13, %16, %18
%21 = phi i8* [ %19, %18 ], [ %9, %16 ], [ %9, %13 ]
%22 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %21, i8* %6)
%23 = tail call i32 @free(i8* %21)
ret void
}