// File: osam-cologne[P]stereogain/dpf/utils/lv2-ttl-generator/lv2_ttl_generator.c
// Function: main
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("usage: %s /path/to/plugin-DLL\n", argv[0]);
        return 1;
    }

#ifdef TTL_GENERATOR_WINDOWS
    const HMODULE handle = LoadLibraryA(argv[1]);
#else
    void* const handle = dlopen(argv[1], RTLD_LAZY);
#endif

    if (! handle)
    {
#ifdef TTL_GENERATOR_WINDOWS
        printf("Failed to open plugin DLL\n");
#else
        printf("Failed to open plugin DLL, error was:\n%s\n", dlerror());
#endif
        return 2;
    }

#ifdef TTL_GENERATOR_WINDOWS
    const TTL_Generator_Function ttlFn = (TTL_Generator_Function)GetProcAddress(handle, "lv2_generate_ttl");
#else
    const TTL_Generator_Function ttlFn = (TTL_Generator_Function)dlsym(handle, "lv2_generate_ttl");
#endif

    if (ttlFn != NULL)
    {
        char basename[strlen(argv[1])+1];

#ifdef TTL_GENERATOR_WINDOWS
        char* base2 = strrchr(argv[1], '\\');
#else
        char* base2 = strrchr(argv[1], '/');
#endif
        if (base2 != NULL)
        {
            strcpy(basename, base2+1);
            basename[strrchr(base2, '.')-base2-1] = '\0';
        }
        else if (argv[1][0] == '.' && argv[1][1] == '/')
        {
            strcpy(basename, argv[1]+2);
            basename[strrchr(basename, '.')-basename] = '\0';
        }
        else
        {
            strcpy(basename, argv[1]);
        }

        printf("Generate ttl data for '%s', basename: '%s'\n", argv[1], basename);

        ttlFn(basename);
    }
    else
        printf("Failed to find 'lv2_generate_ttl' function\n");

#ifdef TTL_GENERATOR_WINDOWS
    FreeLibrary(handle);
#else
    dlclose(handle);
#endif

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x11ed
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xe2a(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1384
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x1, %esi
callq 0x1080
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x122c
callq 0x1030
movq %rax, %rsi
leaq 0xe0a(%rip), %rdi       # 0x2023
movb $0x0, %al
callq 0x1060
movl $0x2, -0x4(%rbp)
jmp 0x1384
movq -0x18(%rbp), %rdi
leaq 0xe16(%rip), %rsi       # 0x204d
callq 0x1090
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x1366
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1050
movq %rax, %rdx
movq %rdx, %rax
incq %rax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
addq $0x10, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2f, %esi
callq 0x1070
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x12d5
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x1, %rsi
callq 0x1040
movq -0x38(%rbp), %rdi
movl $0x2e, %esi
callq 0x1070
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rdx
subq %rdx, %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x133c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x1329
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2f, %eax
jne 0x1329
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x2, %rsi
callq 0x1040
movq -0x40(%rbp), %rdi
movl $0x2e, %esi
callq 0x1070
movq %rax, %rcx
movq -0x40(%rbp), %rax
subq %rax, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x133a
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1040
jmp 0x133c
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xd0f(%rip), %rdi       # 0x205e
movb $0x0, %al
callq 0x1060
movq -0x40(%rbp), %rdi
callq *-0x20(%rbp)
movq -0x28(%rbp), %rax
movq %rax, %rsp
jmp 0x1374
leaq 0xd1d(%rip), %rdi       # 0x208a
movb $0x0, %al
callq 0x1060
movq -0x18(%rbp), %rdi
callq 0x10a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq