// File: /workspace/github_c_makefile_star2_O3/ulisses[P]ftp-client-server/clientec2.c
// Function: cliente
void cliente(int te, int tp, char *pedido, char *ip) {
        char *buffer;
        Datagrama *enviar = NULL;
        int fin, erro = 0;
	FILE *f = NULL;
	ConState *con;
        sock = socket(PF_INET, SOCK_DGRAM, 0); //Abrir o socket com NONBLOCKING
        //fcntl1 = fcntl(sock, F_SETFL, O_NONBLOCK); if (fcntl1 < 0) {perror("fcntl"); exit(1);}

        if (sock<0){
                perror("socket");
                exit(1);
                }

        //server = gethostbyname("192.168.2.4");
	server = gethostbyname(ip);
        memset(&serv_addr, '\0', (size_t)sizeof(serv_addr));

        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr = *((struct in_addr *)server->h_addr);
        serv_addr.sin_port = htons(portno);

        length = sizeof(struct sockaddr_in);
        
        con = conecta(sock);
        if(!con){
        	printf("Conexão mal sucedida\n");
        	exit(1);
        }
	enviar = trataPedido(pedido);
	buffer = muxDatagrama(enviar);
	if(buffer){
		enviaPedido(sock, buffer,&from, te, tp);
		erro = fin = 0;
		if(enviar->flags & FL_GET){
			f = fopen(enviar->data, "a+");
		}
		if(enviar->flags & FL_PUT){
			erro = transfereFich(te, tp, sock, enviar->data, con->clientBuffer, con, &from, length);
		}
		else {
			erro = recebeFicheiro(f, sock, &from, length);
			close(sock);
		}
	}
	else {
		printf("Comando nao encontrado\n");
		exit(0);
	}
   	if(!erro) printf("Ocorreu um erro\n");
               
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r15
movq %rdx, %r12
movl %esi, %ebx
movl %edi, %r14d
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
movl %eax, 0x3040(%rip)      # 0x5618
testl %eax, %eax
js 0x2723
movq %r15, %rdi
callq 0x10f0
movq %rax, 0x3031(%rip)      # 0x5620
xorps %xmm0, %xmm0
movups %xmm0, 0x2ff7(%rip)     # 0x55f0
movw $0x2, 0x2fee(%rip)      # 0x55f0
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movzwl 0x2b06(%rip), %ecx      # 0x5118
rolw $0x8, %cx
movl %eax, 0x2fd8(%rip)      # 0x55f4
movw %cx, 0x2fcf(%rip)       # 0x55f2
movl $0x10, 0x2fd3(%rip)     # 0x5600
movl 0x2fe5(%rip), %edi      # 0x5618
callq 0x214f
testq %rax, %rax
je 0x2728
movq %rax, %r15
movq %r12, %rdi
callq 0x23b6
movq %rax, %r12
movq %rax, %rdi
callq 0x18f1
testq %rax, %rax
je 0x273e
movl 0x2fb2(%rip), %edi      # 0x5618
leaq 0x2f97(%rip), %r13      # 0x5604
movq %rax, %rsi
movq %r13, %rdx
callq 0x253e
movl 0x10(%r12), %ecx
testb $0x8, %cl
jne 0x2686
xorl %eax, %eax
jmp 0x269c
leaq 0x18(%r12), %rdi
leaq 0xa71(%rip), %rsi       # 0x3103
callq 0x1160
movl 0x10(%r12), %ecx
movl 0x2f76(%rip), %edx      # 0x5618
testb $0x40, %cl
jne 0x26d0
movl 0x2f53(%rip), %ecx      # 0x5600
leaq 0x2f50(%rip), %r8       # 0x5604
movq %rax, %rdi
movl %edx, %esi
movq %r8, %rdx
callq 0x1e63
movl %eax, %ebx
movl 0x2f4f(%rip), %edi      # 0x5618
callq 0x10c0
jmp 0x26f6
addq $0x18, %r12
movl (%r15), %r8d
movl 0x2f23(%rip), %eax      # 0x5600
movl %r14d, %edi
movl %ebx, %esi
movq %r12, %rcx
movq %r15, %r9
pushq %rax
pushq %r13
callq 0x1d85
addq $0x10, %rsp
movl %eax, %ebx
testl %ebx, %ebx
je 0x2709
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa97(%rip), %rdi       # 0x31a7
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1060
callq 0x13dc
leaq 0xa4b(%rip), %rdi       # 0x317a
callq 0x1060
movl $0x1, %edi
callq 0x11b0
leaq 0xa4b(%rip), %rdi       # 0x3190
callq 0x1060
xorl %edi, %edi
callq 0x11b0