// File: /workspace/github_c_makefile_star2_O3/ulisses[P]ftp-client-server/servidorc2.c
// Function: trataPedido
void trataPedido(int te, int tp, Datagrama *novo, int newSock, ConState *con, struct sockaddr_in *from, int fromlen){
	char *buffer;
	if((novo->flags & FL_DIR) /*&& strstr(novo->data, "dir.cmd")*/) { //corresponde ao comando DIR
		int status, pid = fork();
		if(pid == 0){//executar comando ls
			execl("./dir", NULL,NULL);
			exit(1);
		}
		wait(&status);
		transfereFich( te, tp, newSock, "dir.cmd", con->clientBuffer, con, from, fromlen);
		return ;
	}
	if((novo->flags & FL_GET) && strstr(novo->data, "wait")){
		Datagrama *res = initDatagrama();
		strcpy(res->data, "fim de wait\n");
		printf("A preparar para domir...\n");
		sleep(10);
		printf("Acordou\n");
		res->flags = FL_FIN;
		buffer = muxDatagrama(res);
		n = sendto(newSock,buffer,strlen(buffer), 0, (struct sockaddr *)from, fromlen);
		return ;
	}
	if((novo->flags & FL_GET)) { 
		printf("get %s\n", novo->data);
		transfereFich(te, tp, newSock, novo->data, con->clientBuffer, con, from, fromlen);
		return ;
	}
	if((novo->flags & FL_PUT)) {
		FILE *f = NULL;
		f = fopen(novo->data, "w+");
		printf("put %s\n", novo->data);
		recebeFicheiro(f, newSock, from, fromlen);
		return ;
	}
	else {
		Datagrama *dados;
		dados = initDatagrama();
		dados->flags = (FL_ERRO | FL_FIN) ;
		buffer = muxDatagrama(dados);
		n = sendto(newSock,buffer,strlen(buffer), 0, (struct sockaddr *)from, fromlen);
		free(dados);
		free(buffer);
		return ;
	}
	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, -0x38(%rbp)
movq %r8, %r12
movl %ecx, %r15d
movl %esi, %ebx
movl %edi, %r14d
movl 0x10(%rbp), %eax
movq %rax, -0x30(%rbp)
movl 0x10(%rdx), %eax
testb $0x20, %al
jne 0x23c7
movq %rdx, %r13
testb $0x8, %al
jne 0x2407
testb $0x40, %al
jne 0x24c3
movl $0xe0, %edi
callq 0x1110
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movw $0x63, 0x18(%rax)
movl $0x14, 0x10(%rax)
movq %rax, %rdi
callq 0x1891
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x10b0
movl %eax, 0x3267(%rip)      # 0x5610
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
callq 0x1210
testl %eax, %eax
je 0x2542
leaq -0x3c(%rbp), %rdi
callq 0x1200
movl (%r12), %r8d
leaq 0xd43(%rip), %rcx       # 0x312b
movl %r14d, %edi
movl %ebx, %esi
movl %r15d, %edx
movq %r12, %r9
pushq -0x30(%rbp)
pushq -0x38(%rbp)
callq 0x2052
addq $0x10, %rsp
jmp 0x24b4
addq $0x18, %r13
leaq 0xd21(%rip), %rsi       # 0x3133
movq %r13, %rdi
callq 0x1220
testq %rax, %rax
je 0x250b
movl $0xe0, %edi
callq 0x1110
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movabsq $0x77206564206d6966, %rax # imm = 0x77206564206D6966
movq %rax, 0x18(%rbx)
movabsq $0xa746961772065, %rax  # imm = 0xA746961772065
movq %rax, 0x1d(%rbx)
leaq 0xd82(%rip), %rdi       # 0x31e3
callq 0x1060
movl $0xa, %edi
callq 0x11f0
leaq 0xd85(%rip), %rdi       # 0x31fc
callq 0x1060
movl $0x4, 0x10(%rbx)
movq %rbx, %rdi
callq 0x1891
movq %rax, %rbx
movq %rax, %rdi
callq 0x1070
movl %r15d, %edi
movq %rbx, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x10b0
movl %eax, 0x315c(%rip)      # 0x5610
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x18, %r13
leaq 0xc7f(%rip), %rsi       # 0x314d
movq %r13, %rdi
callq 0x1160
movq %rax, %rbx
leaq 0xc70(%rip), %rdi       # 0x3150
movq %r13, %rsi
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
movl %r15d, %esi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1b7b
leaq 0xc33(%rip), %rdi       # 0x3145
movq %r13, %rsi
xorl %eax, %eax
callq 0x1080
movl (%r12), %r8d
movl %r14d, %edi
movl %ebx, %esi
movl %r15d, %edx
movq %r13, %rcx
movq %r12, %r9
pushq -0x30(%rbp)
pushq -0x38(%rbp)
callq 0x2052
addq $0x28, %rsp
jmp 0x24b8
leaq 0xbdc(%rip), %rdi       # 0x3125
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x11e0
movl $0x1, %edi
callq 0x11b0