// File: /workspace/github_cpp_makefile_star2_O2/brunokeymolen[P]ssdp/src/ssdpnotifyalive.cpp
// Function: SSDPNotifyAlive::Process(sockaddr*, std::vector<SSDP_HTTP_HEADER*, std::allocator<SSDP_HTTP_HEADER*>>)
int SSDPNotifyAlive::Process(struct sockaddr* sender, std::vector<SSDP_HTTP_HEADER*> msgheaders){
    u8* usn;
    int usnlen;
    int ret;
    u8* val;
    int vallen;
    int cache;
    SSDPDBDevice *device;
    //SSDPDBService *service;
    ssdpuuid uuid;



    //Get the Device USN (Unique Service Name)
    ret=GetHeaderValueFromCollection(msgheaders, (u8*)"USN", 3, &usn, &usnlen);
    if(ret != 0 || usnlen<=0){
        ret = -1;
        return ret;
    }

    ret = ParseUSN(usn, usnlen, &uuid);
    if(ret < 0){
        return ret;//Unknown format
    }


    mDB->Lock();


    //Device or service update

    //Is the device already registered ?
    device = mDB->GetDevice(usn, usnlen);

    GetHeaderValueFromCollection(msgheaders, (u8*)"CACHE-CONTROL", 13, &val, &vallen);
    cache = cachecontroltoi(val, vallen);

    if(device == NULL && cache >= 0){ //cachecontrol is mandatory, if not formatted well we ignore all to avoid conflicts with the db cleanup
        device = mDB->InsertDevice(usn, usnlen);

        memcpy(&(device->ip), (u8*)sender->sa_data+2, 4);
        memcpy(&(device->port), sender->sa_data, 2);
        device->port = ntohs(device->port);

        //uuid
        device->uuid.assign((const char*)uuid.uuid, uuid.uuidlen);
        device->isroot = uuid.isrootdevice;
        device->isdevice = uuid.isdevice;
        device->isservice = uuid.isservice;
        device->type.assign((const char*)uuid.type, uuid.typelen);
        if(!device->isroot){
            device->urn.assign((const char*)uuid.fullurn, uuid.fullurnlen);
            device->version.assign((const char*)uuid.version, uuid.versionlen);
        }
        //host
        GetHeaderValueFromCollection(msgheaders, (u8*)"HOST", 4, &val, &vallen);
        device->host.assign((const char*)val, vallen);
        //Location
        GetHeaderValueFromCollection(msgheaders, (u8*)"LOCATION", 8, &val, &vallen);
        device->location.assign((const char*)val, vallen);


        //Workaround for MusicPal bug
        size_t wrongdev = device->urn.find("schemas-upnp-org:device:RenderingControl:1");
        if(wrongdev != std::string::npos){
            //replace schemas-upnp-org:device:RenderingControl:1 with schemas-upnp-org:service:RenderingControl:1
            char buf[] = "schemas-upnp-org:service:RenderingControl:1";
            device->urn.replace(wrongdev, sizeof(buf), buf);
        }
        //end workaround


        //Inform the observers
        if(uuid.isdevice || uuid.isrootdevice){
            mDB->DeviceUpdate(device);
        }else if(uuid.isservice){
            mDB->ServiceUpdate(device);
        }

    }

    //always update cache control
    if(device != NULL && cache >= 0){
        mDB->UpdateCacheControl(uuid.uuid, uuid.uuidlen, cache);
    }

    //printf("%s\n", device->uuid.c_str());

    mDB->Unlock();


    //printf("SSDPNotifyAlive\n");
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
movq %rdx, %rsi
callq 0x58e0
leaq 0x28dd(%rip), %rsi      # 0x84db
pushq $0x3
popq %rdx
leaq 0x18(%rsp), %rcx
leaq 0xc(%rsp), %r8
movq %r15, %rdi
callq 0x6cfc
movl %eax, %ebp
leaq 0xb8(%rsp), %rdi
callq 0x594c
testl %ebp, %ebp
setne %al
movl 0xc(%rsp), %esi
testl %esi, %esi
setle %cl
orb %al, %cl
je 0x5c3c
pushq $-0x1
popq %rbp
jmp 0x5eba
movq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x6d76
movl %eax, %ebp
testl %eax, %eax
js 0x5eba
movq 0x20(%rbx), %rdi
callq 0x4b86
movq 0x20(%rbx), %rdi
movq 0x18(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x4e0c
movq %rax, %r14
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x58e0
leaq 0x281e(%rip), %rsi      # 0x84ab
pushq $0xd
popq %rdx
leaq 0x10(%rsp), %rcx
leaq 0x4(%rsp), %r8
movq %r15, %rdi
callq 0x6cfc
leaq 0xa0(%rsp), %rdi
callq 0x594c
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x70fe
testq %r14, %r14
setne %dl
testl %eax, %eax
sets %cl
orb %dl, %cl
jne 0x5e94
movl %eax, 0x8(%rsp)
movq 0x20(%rbx), %rdi
movq 0x18(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x4b9a
movq %rax, %r14
movl 0x4(%r13), %eax
movl %eax, 0x138(%r14)
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movw %ax, 0x13c(%r14)
leaq 0x8(%r14), %rdi
movq 0x20(%rsp), %rsi
movl 0x28(%rsp), %edx
callq 0x3300
movb 0x4d(%rsp), %al
movb %al, 0x1(%r14)
movb 0x4c(%rsp), %al
movb %al, (%r14)
movb 0x4e(%rsp), %al
movb %al, 0x2(%r14)
leaq 0xb8(%r14), %rdi
movq 0x50(%rsp), %rsi
movl 0x58(%rsp), %edx
callq 0x3300
cmpb $0x0, 0x1(%r14)
jne 0x5d71
leaq 0x28(%r14), %rdi
movq 0x30(%rsp), %rsi
movl 0x38(%rsp), %edx
callq 0x3300
leaq 0xd8(%r14), %rdi
movq 0x60(%rsp), %rsi
movl 0x68(%rsp), %edx
callq 0x3300
leaq 0x88(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x58e0
leaq 0x271b(%rip), %rsi      # 0x84a6
pushq $0x4
popq %rdx
leaq 0x10(%rsp), %rcx
leaq 0x4(%rsp), %r8
movq %r15, %rdi
callq 0x6cfc
leaq 0x88(%rsp), %rdi
callq 0x594c
leaq 0xf8(%r14), %rdi
leaq 0x10(%rsp), %rax
movq (%rax), %rsi
leaq 0x4(%rsp), %r13
movslq (%r13), %rdx
callq 0x3300
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x58e0
leaq 0x26d8(%rip), %rsi      # 0x84b9
pushq $0x8
popq %rdx
movq %r15, %rdi
leaq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x6cfc
leaq 0x70(%rsp), %rdi
callq 0x594c
leaq 0x118(%r14), %rdi
movq 0x10(%rsp), %rsi
movslq 0x4(%rsp), %rdx
callq 0x3300
leaq 0x28(%r14), %r15
leaq 0x26c0(%rip), %rsi      # 0x84df
movq %r15, %rdi
xorl %edx, %edx
callq 0x3430
cmpq $-0x1, %rax
je 0x5e65
movups 0x26f6(%rip), %xmm0     # 0x852c
leaq 0xd0(%rsp), %rcx
movups %xmm0, 0x1c(%rcx)
movaps 0x26d7(%rip), %xmm0     # 0x8520
movaps %xmm0, 0x10(%rcx)
movaps 0x26bc(%rip), %xmm0     # 0x8510
movaps %xmm0, (%rcx)
pushq $0x2c
popq %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x3150
movb 0x4d(%rsp), %al
orb 0x4c(%rsp), %al
je 0x5e7d
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x4cfa
jmp 0x5e90
cmpb $0x0, 0x4e(%rsp)
je 0x5e90
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x4f30
movl 0x8(%rsp), %eax
testl %eax, %eax
js 0x5eb1
testq %r14, %r14
je 0x5eb1
movq 0x20(%rbx), %rdi
movq 0x20(%rsp), %rsi
movl 0x28(%rsp), %edx
movl %eax, %ecx
callq 0x4f6e
movq 0x20(%rbx), %rdi
callq 0x4b90
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5ed4
jmp 0x5ed4
jmp 0x5ed4
movq %rax, %rbx
movq %r15, %rdi
callq 0x594c
movq %rbx, %rdi
callq 0x33d0
nop