// File: /workspace/github_c_makefile_star2_O2/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 0x21c3
movq %rdx, %r13
testb $0x8, %al
jne 0x2201
testb $0x40, %al
jne 0x22a8
callq 0x15ea
movq %rax, %rbx
movl $0x14, 0x10(%rax)
movq %rax, %rdi
callq 0x16b5
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 0x10a0
movl %eax, 0x346b(%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 0x1200
testl %eax, %eax
je 0x2315
leaq -0x3c(%rbp), %rdi
callq 0x11f0
movl (%r12), %r8d
leaq 0xf2b(%rip), %rcx       # 0x310f
movl %r14d, %edi
movl %ebx, %esi
movl %r15d, %edx
movq %r12, %r9
pushq -0x30(%rbp)
pushq -0x38(%rbp)
callq 0x1de9
popq %rcx
popq %rdx
jmp 0x2299
addq $0x18, %r13
leaq 0xf0b(%rip), %rsi       # 0x3117
movq %r13, %rdi
callq 0x1210
testq %rax, %rax
je 0x22f0
callq 0x15ea
movq %rax, %rbx
movabsq $0x77206564206d6966, %rax # imm = 0x77206564206D6966
movq %rax, 0x18(%rbx)
movabsq $0xa746961772065, %rax  # imm = 0xA746961772065
movq %rax, 0x1d(%rbx)
leaq 0xf7f(%rip), %rdi       # 0x31c7
callq 0x1060
pushq $0xa
popq %rdi
callq 0x11e0
leaq 0xf84(%rip), %rdi       # 0x31e0
callq 0x1060
movl $0x4, 0x10(%rbx)
movq %rbx, %rdi
callq 0x16b5
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 0x10a0
movl %eax, 0x3377(%rip)      # 0x5610
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x18, %r13
leaq 0xe7e(%rip), %rsi       # 0x3131
movq %r13, %rdi
callq 0x1150
movq %rax, %rbx
leaq 0xe6f(%rip), %rdi       # 0x3134
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 0x194a
leaq 0xe32(%rip), %rdi       # 0x3129
movq %r13, %rsi
xorl %eax, %eax
callq 0x1080
movl (%r12), %r8d
movl %r14d, %edi
movl %ebx, %esi
movl %r15d, %edx
movq %r13, %rcx
jmp 0x21ec
leaq 0xded(%rip), %rdi       # 0x3109
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x11a0