#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ peeraddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
#define HOST_NOT_FOUND 131
#define NO_ADDRESS 130
#define NO_RECOVERY 129
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
#define TRY_AGAIN 128
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int h_errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_connection(char *peername, int peerport) {
  struct sockaddr_in peeraddr;
  int SocketDescriptor;
  struct hostent *HostEntry;
  HostEntry = gethostbyname(peername);
  if (HostEntry == NULL) {
    switch (h_errno) {
    case HOST_NOT_FOUND:
      printf("Host not found.\n");
      return -1;
      break;
    case NO_ADDRESS:
      printf("No IP found for valid hostname.\n");
      return -1;
      break;
    case NO_RECOVERY:
      printf("Name service error.\n");
      return -1;
      break;
    case TRY_AGAIN:
      printf("Temporary error in name resolution.\n");
      return -1;
      break;
    default:
      break;
    }
  }
  memset(&peeraddr, 0, sizeof(peeraddr));
  peeraddr.sin_family = AF_INET;
  peeraddr.sin_port = htons(peerport);
  peeraddr.sin_addr = *(struct in_addr *)HostEntry->h_addr;
  SocketDescriptor = socket(PF_INET, SOCK_STREAM, 0);
  if (SocketDescriptor == -1) {
    printf("Error creating socket\n");
    return -1;
  }
  if (connect(SocketDescriptor, (struct sockaddr *)&peeraddr,
              sizeof(peeraddr)) == -1) {
    perror("Error connecting");
    return -1;
  }
  return SocketDescriptor;
}


x86 assembly (clang, -O0):
<get_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1230 <get_connection+0xc0>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     1226 <get_connection+0xb6>
mov    -0x38(%rbp),%rax
lea    0xe43(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <get_connection+0x169>
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <get_connection+0x169>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <get_connection+0x169>
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <get_connection+0x169>
jmp    122b <get_connection+0xbb>
jmp    1230 <get_connection+0xc0>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12a1 <get_connection+0x131>
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <get_connection+0x169>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d3 <get_connection+0x163>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <get_connection+0x169>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_connection>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
call   1060 <gethostbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fb <get_connection+0x8b>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1229 <get_connection+0xb9>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1255 <get_connection+0xe5>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
jmp    1250 <get_connection+0xe0>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x3,%eax
ja     1185 <get_connection+0x15>
lea    0xde9(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe10(%rip),%rdi
jmp    124b <get_connection+0xdb>
lea    0xdf1(%rip),%rdi
jmp    124b <get_connection+0xdb>
lea    0xe22(%rip),%rdi
jmp    124b <get_connection+0xdb>
lea    0xe2d(%rip),%rdi
jmp    124b <get_connection+0xdb>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@h_errno = global i32 0
@.str = [17 x i8] c"Host not found.\0A\00"
@.str.1 = [33 x i8] c"No IP found for valid hostname.\0A\00"
@.str.2 = [21 x i8] c"Name service error.\0A\00"
@.str.3 = [37 x i8] c"Temporary error in name resolution.\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [23 x i8] c"Error creating socket\0A\00"
@.str.5 = [17 x i8] c"Error connecting\00"
define i32 @get_connection(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca %struct.hostent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %8
%11 = load %struct.hostent*, %struct.hostent** %8
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %25
13: ; preds = %2
%14 = load i32, i32* @h_errno
switch i32 %14, label %23 [
i32 131, label %15
i32 130, label %17
i32 129, label %19
i32 128, label %21
]
15: ; preds = %13
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
19: ; preds = %13
%20 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
21: ; preds = %13
%22 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
23: ; preds = %13
br label %24
24: ; preds = %23
br label %25
25: ; preds = %24, %2
%26 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* %5
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%33 = load %struct.hostent*, %struct.hostent** %8
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to %struct.in_addr*
%37 = bitcast %struct.in_addr* %32 to i8*
%38 = bitcast %struct.in_addr* %36 to i8*
%39 = load i32, i32* @PF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %25
%45 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
46: ; preds = %25
%47 = load i32, i32* %7
%48 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
53: ; preds = %46
%54 = load i32, i32* %7
store i32 %54, i32* %3
br label %55
55: ; preds = %53, %51, %44, %21, %19, %17, %15
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@h_errno = global i32 0
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.5 = [17 x i8] c"Error connecting\00"
@str = [22 x i8] c"Error creating socket\00"
@str.6 = [36 x i8] c"Temporary error in name resolution.\00"
@str.7 = [20 x i8] c"Name service error.\00"
@str.8 = [32 x i8] c"No IP found for valid hostname.\00"
@str.9 = [16 x i8] c"Host not found.\00"
define i32 @get_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 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = load i32, i32* @h_errno
switch i32 %8, label %17 [
i32 131, label %9
i32 130, label %11
i32 129, label %13
i32 128, label %15
]
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
br label %40
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
br label %40
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %40
15: ; preds = %7
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.6, i64 0, i64 0))
br label %40
17: ; preds = %7, %2
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %1)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to i32*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%27 = load i32, i32* %25
store i32 %27, i32* %26
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %40
34: ; preds = %17
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @connect(i32 %30, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %34, %38, %32, %15, %13, %11, %9
%41 = phi i32 [ -1, %32 ], [ -1, %38 ], [ -1, %15 ], [ -1, %13 ], [ -1, %11 ], [ -1, %9 ], [ %30, %34 ]
ret i32 %41
}