// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1210
movl %eax, 0x29f7(%rip)      # 0x5628
cmpl $0x0, 0x29f0(%rip)      # 0x5628
jge 0x2c50
leaq 0x50c(%rip), %rdi       # 0x314d
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movq -0x18(%rbp), %rdi
callq 0x10f0
movq %rax, 0x29d0(%rip)      # 0x5630
leaq 0x2999(%rip), %rdi      # 0x5600
xorl %esi, %esi
movl $0x10, %edx
callq 0x10a0
movw $0x2, 0x2984(%rip)      # 0x5600
movq 0x29ad(%rip), %rax      # 0x5630
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, 0x2972(%rip)      # 0x5604
movl 0x2488(%rip), %eax      # 0x5120
movzwl %ax, %edi
callq 0x1080
movw %ax, 0x295b(%rip)       # 0x5602
movl $0x10, 0x295f(%rip)     # 0x5610
movl 0x2971(%rip), %edi      # 0x5628
callq 0x24a0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2cdf
leaq 0x486(%rip), %rdi       # 0x3154
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x11c0
movq -0x10(%rbp), %rdi
callq 0x2970
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x18c0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2dcf
movl 0x291e(%rip), %edi      # 0x5628
movq -0x20(%rbp), %rsi
movl -0x4(%rbp), %ecx
movl -0x8(%rbp), %r8d
leaq 0x28f8(%rip), %rdx      # 0x5614
callq 0x2b50
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x2d56
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
leaq 0x41e(%rip), %rsi       # 0x316b
callq 0x1170
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x2da3
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %esi
movl 0x28b7(%rip), %edx      # 0x5628
movq -0x28(%rbp), %rcx
addq $0x18, %rcx
movq -0x40(%rbp), %rax
movl (%rax), %r8d
movq -0x40(%rbp), %r9
movl 0x2886(%rip), %eax      # 0x5610
leaq 0x2883(%rip), %r10      # 0x5614
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x1f10
movl %eax, -0x30(%rbp)
jmp 0x2dcd
movq -0x38(%rbp), %rdi
movl 0x287b(%rip), %esi      # 0x5628
movl 0x285d(%rip), %ecx      # 0x5610
leaq 0x285a(%rip), %rdx      # 0x5614
callq 0x20a0
movl %eax, -0x30(%rbp)
movl 0x2860(%rip), %edi      # 0x5628
callq 0x10c0
jmp 0x2de4
leaq 0x398(%rip), %rdi       # 0x316e
movb $0x0, %al
callq 0x1090
xorl %edi, %edi
callq 0x11c0
cmpl $0x0, -0x30(%rbp)
jne 0x2df8
leaq 0x2d2(%rip), %rdi       # 0x30c3
movb $0x0, %al
callq 0x1090
addq $0x50, %rsp
popq %rbp
retq
nop