#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_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  scalar_t__ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bcopy(char *, char *, size_t);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
bool connect_to(int *sockfd, int portno, struct sockaddr_in *serveraddr,
                struct hostent *server, const char *hostname) {
  *sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  server = gethostbyname(hostname);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host as %s\n", hostname);
  }
  bzero((char *)serveraddr, sizeof((*serveraddr)));
  (*serveraddr).sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&(*serveraddr).sin_addr.s_addr,
        (size_t)server->h_length);
  (*serveraddr).sin_port = htons(portno);
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jae    11c3 <connect_to+0x53>
lea    0xe42(%rip),%rdi
call   1070 <error@plt>
mov    -0x30(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f4 <connect_to+0x84>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xe26(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
call   1060 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r15
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e6d(%rip),%r12
mov    (%r12),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,0x0(%rbp)
mov    %r15,%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11ba <connect_to+0x5a>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
mov    0x0(%rbp),%rdx
mov    0x8(%rbp),%rsi
lea    0x4(%rbx),%rdi
call   1060 <memmove@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i64 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"ERROR, no such host as %s\0A\00"
define i32 @connect_to(i32* %0, i32 %1, %struct.sockaddr_in* %2, %struct.hostent* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.hostent*
%11 = alloca i8*
store i32* %0, i32** %7
store i32 %1, i32* %8
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %9
store %struct.hostent* %3, %struct.hostent** %10
store i8* %4, i8** %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = load i32*, i32** %7
store i32 %14, i32* %15
%16 = load i32*, i32** %7
%17 = icmp ult i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %5
%21 = load i8*, i8** %11
%22 = call %struct.hostent* @gethostbyname(i8* %21)
store %struct.hostent* %22, %struct.hostent** %10
%23 = load %struct.hostent*, %struct.hostent** %10
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %11
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %20
%30 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%31 = bitcast %struct.sockaddr_in* %30 to i8*
%32 = call i32 @bzero(i8* %31, i32 12)
%33 = load i32, i32* @AF_INET
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 2
store i32 %33, i32* %35
%36 = load %struct.hostent*, %struct.hostent** %10
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i8*
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 1
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
%43 = bitcast i32* %42 to i8*
%44 = load %struct.hostent*, %struct.hostent** %10
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = call i32 @bcopy(i8* %39, i8* %43, i64 %46)
%48 = load i32, i32* %8
%49 = call i32 @htons(i32 %48)
%50 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i32, i32* %6
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i64 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"ERROR, no such host as %s\0A\00"
define i32 @connect_to(i32* nocapture writeonly %0, i32 %1, %struct.sockaddr_in* nocapture writeonly %2, %struct.hostent* nocapture readnone %3, i8* %4) {
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %0
%9 = tail call %struct.hostent* @gethostbyname(i8* %4)
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %14
14: ; preds = %11, %5
%15 = bitcast %struct.sockaddr_in* %2 to i8*
%16 = tail call i32 @bzero(i8* %15, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to i8*
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%23 = bitcast i32* %22 to i8*
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%25 = load i64, i64* %24
tail call void @llvm.memmove.p0i8.p0i8.i64(i8* align 1 %23, i8* align 1 %21, i64 %25, i1 false)
%26 = tail call i32 @htons(i32 %1)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %26, i32* %27
ret i32 undef
}