// File: /workspace/github_cpp_makefile_star2_O2/brunokeymolen[P]ssdp/src/ssdptools.cpp
// Function: ParseUSN(unsigned char*, unsigned int, ssdpuuid*)
int ParseUSN(u8* uuidraw, u32 len, ssdpuuid *uuid){
    int ret = 0;
    int colon1 = 0;
    int colon2 = 0;
    int tel;
    int skip=0;

//    int lenleft;

    //Sanity
    if(uuid == NULL || len == 0){
        return -1;
    }
    if(memcmp(uuidraw, "uuid", 4) != 0){
        return -2;
    }

    memset(uuid, 0, sizeof(ssdpuuid));

//    lenleft = len;

    //uuid
    colon1 = getchar(uuidraw, len, ':', 1);
    colon2 = getchar(uuidraw, len, ':', 2);
    if(colon1<0 || colon1+1>=(int)len){ ret = -1;goto EXIT;}
    if(colon2<0){
        uuid->uuid=uuidraw;//+colon1+1;
        uuid->uuidlen=len;//-colon1-1;
        //uuid->uuidlen=len-colon1-1;
        goto EXIT;
    }else{
        uuid->uuid=uuidraw;//+colon1+1;
        uuid->uuidlen=colon2;
//        uuid->uuidlen=colon2-colon1-1;
    }

    //Sanity, there must be a double colon
    //Find the first '::' sequence and compute the number of semicolons to it
    //store it in the skip variable, which is used for offsetting subsequent searches
    //this is needed for some devices that have a semicolon in the name
    colon1 = getchar(uuidraw, len, ':', 2);
    colon2 = -1;
    while (colon1 < (int)len && colon2 == -1) {
        colon2 = getchar(uuidraw, len, ':', 2+skip+1);
        if (colon2-colon1 == 1) {
            uuid->uuidlen=colon1;
            colon1 = colon2;
        } else {
            colon1 = colon2;
            colon2 = -1;
            skip++;
        }
    }
    if(colon2 == -1){
        ret = -2;
        goto EXIT;
    }


    //upnp, isroot 
    colon1 = getchar(uuidraw, len, ':', 3+skip);
    if((len-colon1)>=15 && memcmp(uuidraw+colon1+1, "upnp:rootdevice", 15)==0 ){
        uuid->isrootdevice = 1;
        uuid->isdevice = 1;
        uuid->type = uuidraw+colon1+1;
        uuid->typelen = 15;
        goto EXIT;
    }

    //Sanity, there must be 4 colons after
    for(tel=4+skip;tel<=7+skip;tel++){
        colon1 = getchar(uuidraw, len, ':', tel);
        if(colon1 < 0){
            ret = -3;
            goto EXIT;
        }
    }


    //urn
    colon1 = getchar(uuidraw, len, ':', 3+skip);
    colon2 = getchar(uuidraw, len, ':', 4+skip);
    if((colon2-colon1)>=3 && memcmp(uuidraw+colon1+1, "urn", 3)!=0 ){
        ret = -4;
        goto EXIT;
    }
    uuid->fullurn = uuidraw+colon1+1;
    uuid->fullurnlen = len-colon1-1;

    colon1 = getchar(uuidraw, len, ':', 4+skip);
    colon2 = getchar(uuidraw, len, ':', 5+skip);
    uuid->urn=uuidraw+colon1+1;
    uuid->urnlen=colon2-colon1-1;



    //Device or service
    colon1 = getchar(uuidraw, len, ':', 5+skip);
    colon2 = getchar(uuidraw, len, ':', 6+skip);
    if( (colon2-colon1)>=6 && memcmp(uuidraw+colon1+1, "device", 6)==0){
        //device
        uuid->isdevice = 1;
    }else if((colon2-colon1)>=7 && memcmp(uuidraw+colon1+1, "service", 7)==0){
        //service
        uuid->isservice = 1;
    }else{
        ret = -5;
        goto EXIT;
    }


    //servicetype / devicetype
    colon1 = getchar(uuidraw, len, ':', 6+skip);
    colon2 = getchar(uuidraw, len, ':', 7+skip);
    uuid->type = uuidraw+colon1+1;
    uuid->typelen=colon2-colon1-1;

    //Workaround for MusicPal bug
    if(uuid->typelen==16 && memcmp(uuid->type, "RenderingControl", 16) == 0){
        uuid->isdevice = 0;
        uuid->isrootdevice = 0;
        uuid->isservice = 1;
    }

    //version
    uuid->version = uuidraw+colon2+1;
    uuid->versionlen=len-colon2-1;

EXIT:

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdx, %rdx
sete %al
testl %esi, %esi
sete %cl
orb %al, %cl
pushq $-0x1
popq %r13
jne 0x6f11
cmpl $0x64697575, (%rdi)     # imm = 0x64697575
pushq $-0x2
popq %rax
je 0x6dae
movl %eax, %r13d
jmp 0x6f11
movq %rdx, %rbx
movl %esi, %ebp
pxor %xmm0, %xmm0
movdqu %xmm0, 0x40(%rdx)
movdqu %xmm0, 0x30(%rdx)
movdqu %xmm0, 0x20(%rdx)
movdqu %xmm0, 0x10(%rdx)
movdqu %xmm0, (%rdx)
pushq $0x3a
popq %r15
pushq $0x1
popq %rcx
movq %rdi, %r14
movl %r15d, %edx
callq 0x3816
movl %eax, %r12d
pushq $0x2
popq %rcx
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x3816
testl %r12d, %r12d
setns %cl
incl %r12d
cmpl %ebp, %r12d
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x6f11
movq %r14, (%rbx)
testl %eax, %eax
js 0x6f0b
movl %eax, 0x8(%rbx)
pushq $0x3a
popq %r12
pushq $0x2
popq %rcx
movq %r14, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x3816
xorl %ecx, %ecx
pushq $-0x1
popq %r14
movq %rcx, 0x8(%rsp)
leal 0x3(%rcx), %r15d
movl %eax, %r13d
movl %r14d, %eax
cmpl %ebp, %r13d
jge 0x6e80
cmpl $-0x1, %eax
jne 0x6e80
movq (%rsp), %rdi
movl %ebp, %esi
movl %r12d, %edx
movl %r15d, %ecx
callq 0x3816
movl %eax, %ecx
subl %r13d, %ecx
cmpl $0x1, %ecx
jne 0x6e77
movl %r13d, 0x8(%rbx)
movl %eax, %r13d
jmp 0x6e49
movq 0x8(%rsp), %rcx
incl %ecx
jmp 0x6e3a
pushq $-0x2
popq %r13
cmpl $-0x1, %eax
movq (%rsp), %r14
je 0x6f11
pushq $0x3a
popq %rdx
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %ecx
callq 0x3816
movl %ebp, %ecx
subl %eax, %ecx
cmpl $0xf, %ecx
jb 0x6ecd
cltq
leaq (%r14,%rax), %r12
incq %r12
leaq 0x177b(%rip), %rsi      # 0x8635
pushq $0xf
popq %rdx
movq %r12, %rdi
callq 0x3290
testl %eax, %eax
je 0x705e
movq 0x8(%rsp), %rax
leal 0x4(%rax), %r12d
addl $0x7, %eax
movl %eax, 0x10(%rsp)
pushq $0x3a
popq %r14
pushq $-0x3
popq %r13
movl %r12d, 0x14(%rsp)
cmpl 0x10(%rsp), %r12d
ja 0x6f23
movq (%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x3816
incl %r12d
testl %eax, %eax
jns 0x6eea
jmp 0x6f11
movl %ebp, 0x8(%rbx)
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3a
popq %r12
movq (%rsp), %r14
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
movl %r15d, %ecx
callq 0x3816
movl %eax, %r15d
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
movl 0x14(%rsp), %r13d
movl %r13d, %ecx
callq 0x3816
subl %r15d, %eax
movslq %r15d, %rcx
leaq (%r14,%rcx), %r12
incq %r12
cmpl $0x3, %eax
jl 0x6f85
leaq 0x16d9(%rip), %rsi      # 0x8645
pushq $0x3
popq %rdx
movq %r12, %rdi
callq 0x3290
movq (%rsp), %r14
testl %eax, %eax
je 0x6f85
pushq $-0x4
popq %r13
jmp 0x6f11
movq %r12, 0x10(%rbx)
notl %r15d
addl %ebp, %r15d
movl %r15d, 0x18(%rbx)
pushq $0x3a
popq %rdx
movq %r14, %rdi
movl %ebp, %esi
movl %r13d, %ecx
callq 0x3816
movl %eax, %r12d
movq 0x8(%rsp), %r15
leal 0x5(%r15), %r13d
movq %r14, %rdi
movl %ebp, %esi
pushq $0x3a
popq %rdx
movl %r13d, %ecx
callq 0x3816
movslq %r12d, %rcx
leaq (%r14,%rcx), %rdx
incq %rdx
movq %rdx, 0x20(%rbx)
notl %ecx
addl %eax, %ecx
movl %ecx, 0x28(%rbx)
movq %r14, %rdi
movl %ebp, %esi
pushq $0x3a
popq %rdx
movl %r13d, %ecx
callq 0x3816
movl %eax, %r12d
addl $0x6, %r15d
movq %r14, %rdi
movl %ebp, %esi
pushq $0x3a
popq %rdx
movq %r15, 0x8(%rsp)
movl %r15d, %ecx
callq 0x3816
movl %eax, %r15d
subl %r12d, %r15d
pushq $-0x5
popq %r13
cmpl $0x6, %r15d
jl 0x6f11
movslq %r12d, %rax
leaq (%r14,%rax), %r12
incq %r12
leaq 0x1619(%rip), %rsi      # 0x863e
pushq $0x6
popq %rdx
movq %r12, %rdi
callq 0x3290
testl %eax, %eax
je 0x7074
cmpl $0x6, %r15d
je 0x6f11
leaq 0x1604(%rip), %rsi      # 0x8649
pushq $0x7
popq %rdx
movq %r12, %rdi
callq 0x3290
testl %eax, %eax
jne 0x6f11
movb $0x1, 0x2e(%rbx)
jmp 0x7078
movw $0x101, 0x2c(%rbx)      # imm = 0x101
movq %r12, 0x30(%rbx)
movl $0xf, 0x38(%rbx)
jmp 0x6f0e
movb $0x1, 0x2c(%rbx)
pushq $0x3a
popq %r15
movq (%rsp), %r14
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movq 0x8(%rsp), %rcx
callq 0x3816
movl %eax, %r12d
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movl 0x10(%rsp), %ecx
callq 0x3816
movslq %r12d, %rdx
leaq (%r14,%rdx), %rcx
incq %rcx
movq %rcx, 0x30(%rbx)
notl %edx
addl %eax, %edx
movl %edx, 0x38(%rbx)
cmpl $0x10, %edx
jne 0x70e1
movdqu (%rcx), %xmm0
pcmpeqb 0x1554(%rip), %xmm0     # 0x8620
pmovmskb %xmm0, %ecx
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0x70e1
andw $0x0, 0x2c(%rbx)
movb $0x1, 0x2e(%rbx)
movslq %eax, %rcx
movq (%rsp), %rdx
addq %rdx, %rcx
incq %rcx
movq %rcx, 0x40(%rbx)
notl %eax
addl %ebp, %eax
movl %eax, 0x48(%rbx)
jmp 0x6f0e