// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %r9, -0x40(%rbp)
movq %r8, %r12
movl %ecx, -0x2c(%rbp)
movl %esi, %r13d
movl %edi, %ebx
movl 0x10(%rbp), %eax
movq %rax, -0x38(%rbp)
movl 0x10(%rdx), %r15d
testb $0x20, %r15b
jne 0x2408
movq %rdx, %r14
testb $0x8, %r15b
jne 0x2448
testb $0x40, %r15b
jne 0x2505
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 0x1867
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movl -0x2c(%rbp), %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x40(%rbp), %r8
movq -0x38(%rbp), %r9
callq 0x10b0
movl %eax, 0x3226(%rip)      # 0x5610
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
callq 0x1210
testl %eax, %eax
je 0x2595
leaq -0x4c(%rbp), %rdi
callq 0x1200
movl (%r12), %r8d
leaq 0xd02(%rip), %rcx       # 0x312b
movl %ebx, %edi
movl %r13d, %esi
movl -0x2c(%rbp), %edx
movq %r12, %r9
pushq -0x38(%rbp)
pushq -0x40(%rbp)
callq 0x2041
addq $0x10, %rsp
jmp 0x24f6
leaq 0x18(%r14), %rdi
leaq 0xce0(%rip), %rsi       # 0x3133
movq %rdi, -0x48(%rbp)
callq 0x1220
testq %rax, %rax
je 0x254d
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 0xd40(%rip), %rdi       # 0x31e3
callq 0x1060
movl $0xa, %edi
callq 0x11f0
leaq 0xd43(%rip), %rdi       # 0x31fc
callq 0x1060
movl $0x4, 0x10(%rbx)
movq %rbx, %rdi
callq 0x1867
movq %rax, %rbx
movq %rax, %rdi
callq 0x1070
movl -0x2c(%rbp), %edi
movq %rbx, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x40(%rbp), %r8
movq -0x38(%rbp), %r9
callq 0x10b0
movl %eax, 0x311a(%rip)      # 0x5610
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x18, %r14
leaq 0xc3d(%rip), %rsi       # 0x314d
movq %r14, %rdi
callq 0x1160
movq %rax, %rbx
leaq 0xc2e(%rip), %rdi       # 0x3150
movq %r14, %rsi
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1b47
movq -0x48(%rbp), %rax
testb $0x8, %r15b
je 0x2387
leaq 0xbe3(%rip), %rdi       # 0x3145
movq %rax, %r14
movq %rax, %rsi
xorl %eax, %eax
callq 0x1080
movl (%r12), %r8d
movl %ebx, %edi
movl %r13d, %esi
movl -0x2c(%rbp), %edx
movq %r14, %rcx
movq %r12, %r9
pushq -0x38(%rbp)
pushq -0x40(%rbp)
callq 0x2041
addq $0x38, %rsp
jmp 0x24fa
leaq 0xb89(%rip), %rdi       # 0x3125
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x11e0
movl $0x1, %edi
callq 0x11b0