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

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strchr(char *, char);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *cw_filename(const char *path, const char *name, const char *ext) {
  int name_len;
  int ext_len;
  int size = 0;
  int path_len;
  char c;
  char *result;
  char *slash = "";
  path_len = strlen(path);
  if (path_len > 0) {
    c = path[path_len - 1];
    if (!strchr("/", c)) {
      path_len++;
      slash = "/";
    }
  }
  name_len = strlen(name);
  if (ext != NULL) {
    if (ext[0] == '.') {
      ext++;
    }
    ext_len = strlen(ext);
  } else
    ext_len = -1;
  size = (path_len) + name_len + (ext_len + 1) + 1;
  result = malloc(size);
  if (!result)
    return NULL;
  strncpy(result, path, path_len);
  result[path_len] = 0;
  strcat(result, slash);
  strcat(result, name);
  if (ext) {
    strcat(result, ".");
    strcat(result, ext);
  }
  return result;
}


x86 assembly (clang, -O0):
<cw_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    11d1 <cw_filename+0x81>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x31(%rbp)
lea    0xe5a(%rip),%rdi
movsbl -0x31(%rbp),%esi
call   1050 <strchr@plt>
cmp    $0x0,%eax
jne    11cc <cw_filename+0x7c>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
lea    0xe38(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    11d1 <cw_filename+0x81>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1215 <cw_filename+0xc5>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1204 <cw_filename+0xb4>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
jmp    121c <cw_filename+0xcc>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x30(%rbp),%eax
add    -0x24(%rbp),%eax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1254 <cw_filename+0x104>
movq   $0x0,-0x8(%rbp)
jmp    12ba <cw_filename+0x16a>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcat@plt>
cmpq   $0x0,-0x20(%rbp)
je     12b2 <cw_filename+0x162>
mov    -0x40(%rbp),%rdi
lea    0xd62(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cw_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r13d
lea    0xe90(%rip),%rbx
test   %eax,%eax
jle    119a <cw_filename+0x4a>
lea    -0x1(%r13),%eax
movsbl (%r15,%rax,1),%esi
lea    0xe79(%rip),%r12
mov    %r12,%rdi
call   1050 <strchr@plt>
test   %eax,%eax
jne    119a <cw_filename+0x4a>
add    $0x1,%r13d
mov    %r12,%rbx
mov    %rbx,(%rsp)
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
test   %rbp,%rbp
je     11c7 <cw_filename+0x77>
xor    %ebx,%ebx
cmpb   $0x2e,0x0(%rbp)
sete   %bl
add    %rbp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
jmp    11cb <cw_filename+0x7b>
xor    %eax,%eax
xor    %ebx,%ebx
add    %r13d,%r12d
lea    (%rax,%r12,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     122e <cw_filename+0xde>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
movslq %r13d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rdi
mov    (%rsp),%rsi
call   1070 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
test   %rbx,%rbx
je     122e <cw_filename+0xde>
lea    0xde7(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcat@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c".\00"
define i8* @cw_filename(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %10
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %14
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i32, i32* %11
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* %12
%26 = load i8, i8* %12
%27 = call i32 @strchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8 signext %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %19
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
store i32 %31, i32* %11
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %14
br label %32
32: ; preds = %29, %19
br label %33
33: ; preds = %32, %3
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %8
%36 = load i8*, i8** %7
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %50
38: ; preds = %33
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 46
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i8*, i8** %7
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %7
br label %47
47: ; preds = %44, %38
%48 = load i8*, i8** %7
%49 = call i32 @strlen(i8* %48)
store i32 %49, i32* %9
br label %51
50: ; preds = %33
store i32 -1, i32* %9
br label %51
51: ; preds = %50, %47
%52 = load i32, i32* %11
%53 = load i32, i32* %8
%54 = add nsw i32 %52, %53
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
%57 = add nsw i32 %54, %56
%58 = add nsw i32 %57, 1
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = call i8* @malloc(i32 %59)
store i8* %60, i8** %13
%61 = load i8*, i8** %13
%62 = icmp ne i8* %61, null
br i1 %62, label %64, label %63
63: ; preds = %51
store i8* null, i8** %4
br label %89
64: ; preds = %51
%65 = load i8*, i8** %13
%66 = load i8*, i8** %5
%67 = load i32, i32* %11
%68 = call i32 @strncpy(i8* %65, i8* %66, i32 %67)
%69 = load i8*, i8** %13
%70 = load i32, i32* %11
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
%73 = load i8*, i8** %13
%74 = load i8*, i8** %14
%75 = call i32 @strcat(i8* %73, i8* %74)
%76 = load i8*, i8** %13
%77 = load i8*, i8** %6
%78 = call i32 @strcat(i8* %76, i8* %77)
%79 = load i8*, i8** %7
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %87
81: ; preds = %64
%82 = load i8*, i8** %13
%83 = call i32 @strcat(i8* %82, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%84 = load i8*, i8** %13
%85 = load i8*, i8** %7
%86 = call i32 @strcat(i8* %84, i8* %85)
br label %87
87: ; preds = %81, %64
%88 = load i8*, i8** %13
store i8* %88, i8** %4
br label %89
89: ; preds = %87, %63
%90 = load i8*, i8** %4
ret i8* %90
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c".\00"
define i8* @cw_filename(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %3
%7 = add nsw i32 %4, -1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = tail call i32 @strchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8 signext %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = add nuw nsw i32 %4, 1
br label %15
15: ; preds = %6, %13, %3
%16 = phi i32 [ %4, %6 ], [ %14, %13 ], [ %4, %3 ]
%17 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %3 ]
%18 = tail call i32 @strlen(i8* %1)
%19 = icmp eq i8* %2, null
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = load i8, i8* %2
%22 = icmp eq i8 %21, 46
%23 = zext i1 %22 to i64
%24 = getelementptr i8, i8* %2, i64 %23
%25 = tail call i32 @strlen(i8* %24)
%26 = add i32 %25, 1
br label %27
27: ; preds = %15, %20
%28 = phi i32 [ %26, %20 ], [ 0, %15 ]
%29 = phi i8* [ %24, %20 ], [ null, %15 ]
%30 = add i32 %16, 1
%31 = add i32 %30, %18
%32 = add i32 %31, %28
%33 = tail call i8* @malloc(i32 %32)
%34 = icmp eq i8* %33, null
br i1 %34, label %45, label %35
35: ; preds = %27
%36 = tail call i32 @strncpy(i8* %33, i8* %0, i32 %16)
%37 = sext i32 %16 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
store i8 0, i8* %38
%39 = tail call i32 @strcat(i8* %33, i8* %17)
%40 = tail call i32 @strcat(i8* %33, i8* %1)
%41 = icmp eq i8* %29, null
br i1 %41, label %45, label %42
42: ; preds = %35
%43 = tail call i32 @strcat(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%44 = tail call i32 @strcat(i8* %33, i8* %29)
br label %45
45: ; preds = %35, %42, %27
ret i8* %33
}