#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ connection;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/, int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int establish_connection(char *host, int porta) {
  struct hostent *h;
  struct sockaddr_in connection;
  int new_socket;
  h = gethostbyname(host);
  new_socket = socket(AF_INET, SOCK_STREAM, 0);
  connection.sin_family = AF_INET;
  connection.sin_port = htons(porta);
  memcpy(&connection.sin_addr.s_addr, h->h_addr, 4);
  bzero(connection.sin_zero, 8);
  while ((connect(new_socket, (struct sockaddr *)&connection,
                  sizeof(connection))) != 0)
    ;
  return new_socket;
}


x86 assembly (clang, -O0):
<establish_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%edi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     11f4 <establish_connection+0x94>
jmp    11d4 <establish_connection+0x74>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
call   1040 <gethostbyname@plt>
mov    %rax,%r14
mov    0x2e6d(%rip),%r15
mov    (%r15),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rdi
mov    (%r14),%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%edi
mov    $0x8,%esi
call   1060 <bzero@plt>
lea    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
test   %rax,%rax
jne    11d0 <establish_connection+0x70>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @establish_connection(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.hostent*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* %4
%16 = call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load %struct.hostent*, %struct.hostent** %5
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @memcpy(i32* %19, i32 %22, i32 4)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @bzero(i32 %25, i32 8)
br label %27
27: ; preds = %32, %2
%28 = load i32, i32* %7
%29 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 16)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
br label %27
33: ; preds = %27
%34 = load i32, i32* %7
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @establish_connection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @memcpy(i32* %13, i32 %15, i32 4)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @bzero(i32 %18, i32 8)
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %21
21: ; preds = %21, %2
%22 = call i64 @connect(i32 %8, %struct.sockaddr* %20, i32 16)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %21
24: ; preds = %21
ret i32 %8
}