#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ endereco_servidor;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
scalar_t__ connect(int, struct sockaddr *, int);
int erro(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ timestamp();
int sock_cliente(char *nome_servidor, char *porta) {
  struct hostent *dns = gethostbyname(nome_servidor);
  if (!dns)
    return erro("servidor DNS inalcançável.");
  struct sockaddr_in endereco_servidor;
  bcopy(dns->h_addr, &(endereco_servidor.sin_addr), dns->h_length);
  endereco_servidor.sin_family = AF_INET;
  endereco_servidor.sin_port = htons(atoi(porta));
  int cliente_servidor = socket(AF_INET, SOCK_STREAM, 0);
  if (cliente_servidor < 0)
    return erro("socket().");
  timestamp();
  printf("[C] Tentando conectar com %s em %s\n", nome_servidor, porta);
  if (connect(cliente_servidor, (struct sockaddr *)&endereco_servidor,
              sizeof(endereco_servidor)) < 0)
    return erro("connect().");
  return cliente_servidor;
}


x86 assembly (clang, -O0):
<sock_cliente>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <sock_cliente+0x3c>
lea    0xe41(%rip),%rdi
call   1060 <erro@plt>
mov    %eax,-0x4(%rbp)
jmp    128f <sock_cliente+0xff>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    123f <sock_cliente+0xaf>
lea    0xdeb(%rip),%rdi
call   1060 <erro@plt>
mov    %eax,-0x4(%rbp)
jmp    128f <sock_cliente+0xff>
mov    $0x0,%al
call   1030 <timestamp@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1289 <sock_cliente+0xf9>
lea    0xdcf(%rip),%rdi
call   1060 <erro@plt>
mov    %eax,-0x4(%rbp)
jmp    128f <sock_cliente+0xff>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_cliente>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1226 <sock_cliente+0x96>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x8(%rsp),%rsi
call   1080 <bcopy@plt>
mov    0x2e2a(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     123a <sock_cliente+0xaa>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <timestamp@plt>
lea    0xe29(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    1248 <sock_cliente+0xb8>
lea    0xe27(%rip),%rdi
jmp    1241 <sock_cliente+0xb1>
lea    0xdd3(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <erro@plt>
lea    0xddc(%rip),%rdi
call   1060 <erro@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"servidor DNS inalcan\C3\A7\C3\A1vel.\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"socket().\00"
@.str.2 = [36 x i8] c"[C] Tentando conectar com %s em %s\0A\00"
@.str.3 = [11 x i8] c"connect().\00"
define i32 @sock_cliente(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %6
%11 = load %struct.hostent*, %struct.hostent** %6
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @erro(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %3
br label %50
15: ; preds = %2
%16 = load %struct.hostent*, %struct.hostent** %6
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%20 = load %struct.hostent*, %struct.hostent** %6
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @bcopy(i32 %18, i32* %19, i32 %22)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i8*, i8** %5
%27 = call i32 @atoi(i8* %26)
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %15
%36 = call i32 @erro(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 %36, i32* %3
br label %50
37: ; preds = %15
%38 = call i32 (...) @timestamp()
%39 = load i8*, i8** %4
%40 = load i8*, i8** %5
%41 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* %40)
%42 = load i32, i32* %8
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = call i32 @erro(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 %47, i32* %3
br label %50
48: ; preds = %37
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %46, %35, %13
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"servidor DNS inalcan\C3\A7\C3\A1vel.\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"socket().\00"
@.str.2 = [36 x i8] c"[C] Tentando conectar com %s em %s\0A\00"
@.str.3 = [11 x i8] c"connect().\00"
define i32 @sock_cliente(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @erro(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %36
8: ; preds = %2
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 1
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @bcopy(i32 %11, i32* %12, i32 %14)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @atoi(i8* %1)
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %8
%26 = call i32 (...) @timestamp()
%27 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %23, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25, %8
%32 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %25 ]
%33 = call i32 @erro(i8* %32)
br label %34
34: ; preds = %31, %25
%35 = phi i32 [ %23, %25 ], [ %33, %31 ]
br label %36
36: ; preds = %34, %6
%37 = phi i32 [ %35, %34 ], [ %7, %6 ]
ret i32 %37
}