// File: /workspace/github_c_makefile_star2_O1/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, 0x2ffa(%rip)      # 0x5618
testl %eax, %eax
js 0x2765
movq %r15, %rdi
callq 0x10f0
movq %rax, 0x2feb(%rip)      # 0x5620
xorps %xmm0, %xmm0
movups %xmm0, 0x2fb1(%rip)     # 0x55f0
movw $0x2, 0x2fa8(%rip)      # 0x55f0
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movzwl 0x2ac0(%rip), %ecx      # 0x5118
rolw $0x8, %cx
movl %eax, 0x2f92(%rip)      # 0x55f4
movw %cx, 0x2f89(%rip)       # 0x55f2
movl $0x10, 0x2f8d(%rip)     # 0x5600
movl 0x2f9f(%rip), %edi      # 0x5618
callq 0x2138
testq %rax, %rax
je 0x276a
movq %rax, %r15
movq %r12, %rdi
callq 0x23fc
movq %rax, %r12
movq %rax, %rdi
callq 0x18c7
testq %rax, %rax
je 0x2780
movl 0x2f6c(%rip), %edi      # 0x5618
leaq 0x2f51(%rip), %r13      # 0x5604
movq %rax, %rsi
movq %r13, %rdx
callq 0x2584
testb $0x8, 0x10(%r12)
jne 0x26ca
xorl %eax, %eax
jmp 0x26db
leaq 0x18(%r12), %rdi
leaq 0xa2d(%rip), %rsi       # 0x3103
callq 0x1160
testb $0x40, 0x10(%r12)
movl 0x2f31(%rip), %edx      # 0x5618
jne 0x2712
movl 0x2f11(%rip), %ecx      # 0x5600
leaq 0x2f0e(%rip), %r8       # 0x5604
movq %rax, %rdi
movl %edx, %esi
movq %r8, %rdx
callq 0x1e3f
movl %eax, %ebx
movl 0x2f0d(%rip), %edi      # 0x5618
callq 0x10c0
jmp 0x2738
addq $0x18, %r12
movl (%r15), %r8d
movl 0x2ee1(%rip), %eax      # 0x5600
movl %r14d, %edi
movl %ebx, %esi
movq %r12, %rcx
movq %r15, %r9
pushq %rax
pushq %r13
callq 0x1d67
addq $0x10, %rsp
movl %eax, %ebx
testl %ebx, %ebx
je 0x274b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa55(%rip), %rdi       # 0x31a7
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1060
callq 0x13dd
leaq 0xa09(%rip), %rdi       # 0x317a
callq 0x1060
movl $0x1, %edi
callq 0x11b0
leaq 0xa09(%rip), %rdi       # 0x3190
callq 0x1060
xorl %edi, %edi
callq 0x11b0