#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__ {
  long s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sw_memset_zero(struct sockaddr_in *, int);
int OpenConnection(const char *hostname, int port) {
  int sd;
  struct hostent *host;
  struct sockaddr_in addr;
  if ((host = gethostbyname(hostname)) == NULL) {
    printf("Eroor: %s\n", hostname);
    perror(hostname);
    abort();
  }
  sd = socket(PF_INET, SOCK_STREAM, 0);
  sw_memset_zero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = *(long *)(host->h_addr);
  if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    close(sd);
    perror(hostname);
    abort();
  }
  return sd;
}


x86 assembly (clang, -O0):
<OpenConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d6 <OpenConnection+0x46>
mov    -0x8(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x10,%esi
call   1070 <sw_memset_zero@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
je     1258 <OpenConnection+0xc8>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenConnection>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1220 <OpenConnection+0x90>
mov    %rax,%rbx
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x10,%esi
call   1070 <sw_memset_zero@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x10,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jne    1231 <OpenConnection+0xa1>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
jmp    1238 <OpenConnection+0xa8>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"Eroor: %s\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @OpenConnection(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
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** %6
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %3
%15 = call i32 @perror(i8* %14)
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %2
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %5
%21 = call i32 @sw_memset_zero(%struct.sockaddr_in* %7, i32 16)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %4
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %25, i32* %26
%27 = load %struct.hostent*, %struct.hostent** %6
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to i64*
%31 = load i64, i64* %30
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i64 %31, i64* %33
%34 = load i32, i32* %5
%35 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 16)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %17
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %3
%42 = call i32 @perror(i8* %41)
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %17
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"Eroor: %s\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @OpenConnection(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 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @perror(i8* %0)
%10 = tail call i32 (...) @abort()
unreachable
11: ; preds = %2
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = call i32 @sw_memset_zero(%struct.sockaddr_in* %3, i32 16)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 %1)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i64*
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 %23, i64* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @connect(i32 %14, %struct.sockaddr* %25, i32 16)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %11
%29 = call i32 @close(i32 %14)
%30 = call i32 @perror(i8* %0)
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %11
ret i32 %14
}