// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x11f0
movl %eax, 0x3304(%rip)      # 0x5618
testl %eax, %eax
js 0x245d
movq %r15, %rdi
callq 0x10e0
movq %rax, 0x32f5(%rip)      # 0x5620
xorps %xmm0, %xmm0
movups %xmm0, 0x32bb(%rip)     # 0x55f0
movw $0x2, 0x32b2(%rip)      # 0x55f0
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movzwl 0x2dc2(%rip), %ecx      # 0x5110
rolw $0x8, %cx
movl %eax, 0x329c(%rip)      # 0x55f4
movw %cx, 0x3293(%rip)       # 0x55f2
movl $0x10, 0x3297(%rip)     # 0x5600
movl 0x32a9(%rip), %edi      # 0x5618
callq 0x1e98
testq %rax, %rax
je 0x246b
movq %rax, %r15
movq %r12, %rdi
callq 0x214a
movq %rax, %r12
movq %rax, %rdi
callq 0x1675
testq %rax, %rax
je 0x247f
movl 0x3276(%rip), %edi      # 0x5618
leaq 0x325b(%rip), %r13      # 0x5604
movq %rax, %rsi
movq %r13, %rdx
callq 0x2273
movl 0x10(%r12), %ecx
testb $0x8, %cl
jne 0x23c2
xorl %eax, %eax
jmp 0x23d8
leaq 0x18(%r12), %rdi
leaq 0xd19(%rip), %rsi       # 0x30e7
callq 0x1150
movl 0x10(%r12), %ecx
movl 0x323a(%rip), %edx      # 0x5618
testb $0x40, %cl
jne 0x240c
movl 0x3217(%rip), %ecx      # 0x5600
leaq 0x3214(%rip), %r8       # 0x5604
movq %rax, %rdi
movl %edx, %esi
movq %r8, %rdx
callq 0x1be8
movl %eax, %ebx
movl 0x3213(%rip), %edi      # 0x5618
callq 0x10b0
jmp 0x2430
addq $0x18, %r12
movl (%r15), %r8d
movl 0x31e7(%rip), %eax      # 0x5600
movl %r14d, %edi
movl %ebx, %esi
movq %r12, %rcx
movq %r15, %r9
pushq %rax
pushq %r13
callq 0x1ab8
popq %rcx
popq %rdx
movl %eax, %ebx
testl %ebx, %ebx
je 0x2443
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd41(%rip), %rdi       # 0x318b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1060
leaq 0xc7c(%rip), %rdi       # 0x30e0
callq 0x1160
jmp 0x2477
leaq 0xcec(%rip), %rdi       # 0x315e
callq 0x1060
pushq $0x1
popq %rdi
callq 0x11a0
leaq 0xcee(%rip), %rdi       # 0x3174
callq 0x1060
xorl %edi, %edi
callq 0x11a0