#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 *buildConfig_homeDirectory();
int /*<<< orphan*/ errors_printMessageAndExit(char *, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int const);
int /*<<< orphan*/ normalizeAbsFile(char *, char *, int);
int /*<<< orphan*/ normalizeRelFile(char *, char *, int);
int strlen(char const *);
char *filename_normalize(char *destination, const char *filename,
                         int filenameLen) {
  if (filename == NULL) {
    errors_printMessageAndExit(
        "\x1B[7mNull pointer exception (%s:%d)\x1B[27m",
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/gerardvisser/skylge/refs/heads/master/tools/"
        "filename.c",
        40);
  }
  char *name;
  if (filename[0] == '~') {
    const char *homeDir = buildConfig_homeDirectory();
    const int homeDirLen = strlen(homeDir);
    name = malloc(filenameLen + homeDirLen);
    memcpy(name, homeDir, homeDirLen);
    memcpy(name + homeDirLen, filename + 1, filenameLen);
    filenameLen += homeDirLen - 1;
  } else {
    name = (char *)filename;
  }
  if (destination == NULL) {
    destination = malloc(filenameLen + 2);
  }
  if (name[0] == '/') {
    normalizeAbsFile(destination, name, filenameLen);
  } else {
    normalizeRelFile(destination, name, filenameLen);
  }
  if (name != filename) {
    free(name);
  }
  return destination;
}


x86 assembly (clang, -O0):
<filename_normalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b6 <filename_normalize+0x36>
lea    0xe5b(%rip),%rdi
lea    0xe7c(%rip),%rsi
mov    $0x28,%edx
call   10a0 <errors_printMessageAndExit@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1228 <filename_normalize+0xa8>
mov    $0x0,%al
call   1070 <buildConfig_homeDirectory@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
add    -0x2c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1230 <filename_normalize+0xb0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    124a <filename_normalize+0xca>
mov    -0x14(%rbp),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    126f <filename_normalize+0xef>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <normalizeAbsFile@plt>
jmp    127f <filename_normalize+0xff>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <normalizeRelFile@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1296 <filename_normalize+0x116>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filename_normalize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
test   %rsi,%rsi
je     11f1 <filename_normalize+0x71>
cmpb   $0x7e,(%r12)
jne    1210 <filename_normalize+0x90>
xor    %eax,%eax
call   1070 <buildConfig_homeDirectory@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    (%r15,%rbp,1),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
movslq %ebp,%rbp
mov    %rbx,%rdi
add    %rbp,%rdi
lea    0x1(%r12),%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
add    %ebp,%r15d
add    $0xffffffff,%r15d
test   %r14,%r14
jne    1218 <filename_normalize+0x98>
jmp    1232 <filename_normalize+0xb2>
lea    0xe08(%rip),%rdi
lea    0xe29(%rip),%rsi
mov    $0x28,%edx
call   10a0 <errors_printMessageAndExit@plt>
cmpb   $0x7e,(%r12)
je     11a0 <filename_normalize+0x20>
mov    %r12,%rbx
test   %r14,%r14
je     1232 <filename_normalize+0xb2>
cmpb   $0x2f,(%rbx)
jne    1243 <filename_normalize+0xc3>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <normalizeAbsFile@plt>
cmp    %r12,%rbx
jne    1256 <filename_normalize+0xd6>
jmp    125e <filename_normalize+0xde>
lea    0x2(%r15),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
cmpb   $0x2f,(%rbx)
je     121d <filename_normalize+0x9d>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1090 <normalizeRelFile@plt>
cmp    %r12,%rbx
je     125e <filename_normalize+0xde>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"\1B[7mNull pointer exception (%s:%d)\1B[27m\00"
@.str.1 = [145 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/gerardvisser/skylge/refs/heads/master/tools/filename.c\00"
define i8* @filename_normalize(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @errors_printMessageAndExit(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 40)
br label %14
14: ; preds = %12, %3
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 126
br i1 %19, label %20, label %44
20: ; preds = %14
%21 = call i8* (...) @buildConfig_homeDirectory()
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %9
%24 = load i32, i32* %6
%25 = load i32, i32* %9
%26 = add nsw i32 %24, %25
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = call i32 @memcpy(i8* %28, i8* %29, i32 %30)
%32 = load i8*, i8** %7
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i32, i32* %6
%39 = call i32 @memcpy(i8* %35, i8* %37, i32 %38)
%40 = load i32, i32* %9
%41 = sub nsw i32 %40, 1
%42 = load i32, i32* %6
%43 = add nsw i32 %42, %41
store i32 %43, i32* %6
br label %46
44: ; preds = %14
%45 = load i8*, i8** %5
store i8* %45, i8** %7
br label %46
46: ; preds = %44, %20
%47 = load i8*, i8** %4
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 2
%52 = call i8* @malloc(i32 %51)
store i8* %52, i8** %4
br label %53
53: ; preds = %49, %46
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i64 0
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 47
br i1 %58, label %59, label %64
59: ; preds = %53
%60 = load i8*, i8** %4
%61 = load i8*, i8** %7
%62 = load i32, i32* %6
%63 = call i32 @normalizeAbsFile(i8* %60, i8* %61, i32 %62)
br label %69
64: ; preds = %53
%65 = load i8*, i8** %4
%66 = load i8*, i8** %7
%67 = load i32, i32* %6
%68 = call i32 @normalizeRelFile(i8* %65, i8* %66, i32 %67)
br label %69
69: ; preds = %64, %59
%70 = load i8*, i8** %7
%71 = load i8*, i8** %5
%72 = icmp ne i8* %70, %71
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i8*, i8** %7
%75 = call i32 @free(i8* %74)
br label %76
76: ; preds = %73, %69
%77 = load i8*, i8** %4
ret i8* %77
}

LLVM IR (-O2):
@.str = [40 x i8] c"\1B[7mNull pointer exception (%s:%d)\1B[27m\00"
@.str.1 = [145 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/gerardvisser/skylge/refs/heads/master/tools/filename.c\00"
define i8* @filename_normalize(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @errors_printMessageAndExit(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 40)
br label %7
7: ; preds = %5, %3
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 126
br i1 %9, label %10, label %21
10: ; preds = %7
%11 = tail call i8* (...) @buildConfig_homeDirectory()
%12 = tail call i32 @strlen(i8* %11)
%13 = add i32 %12, %2
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 @memcpy(i8* %14, i8* %11, i32 %12)
%16 = sext i32 %12 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = getelementptr inbounds i8, i8* %1, i64 1
%19 = tail call i32 @memcpy(i8* %17, i8* %18, i32 %2)
%20 = add i32 %13, -1
br label %21
21: ; preds = %7, %10
%22 = phi i8* [ %14, %10 ], [ %1, %7 ]
%23 = phi i32 [ %20, %10 ], [ %2, %7 ]
%24 = icmp eq i8* %0, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = add nsw i32 %23, 2
%27 = tail call i8* @malloc(i32 %26)
br label %28
28: ; preds = %25, %21
%29 = phi i8* [ %27, %25 ], [ %0, %21 ]
%30 = load i8, i8* %22
%31 = icmp eq i8 %30, 47
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @normalizeAbsFile(i8* %29, i8* %22, i32 %23)
br label %36
34: ; preds = %28
%35 = tail call i32 @normalizeRelFile(i8* %29, i8* %22, i32 %23)
br label %36
36: ; preds = %34, %32
%37 = icmp eq i8* %22, %1
br i1 %37, label %40, label %38
38: ; preds = %36
%39 = tail call i32 @free(i8* %22)
br label %40
40: ; preds = %38, %36
ret i8* %29
}