#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 sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
void connect_socket(int sockfd, char *hname, int portno) {
  struct sockaddr_in serv_addr;
  struct hostent *server;
  server = gethostbyname(hname);
  if (server == NULL)
    error("ERROR , no such host\n");
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portno);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR connecting");
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <connect_socket+0x36>
lea    0xe6f(%rip),%rdi
call   1060 <error@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    11fd <connect_socket+0x9d>
lea    0xe1e(%rip),%rdi
call   1060 <error@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118b <connect_socket+0x2b>
lea    0xe7a(%rip),%rdi
call   1060 <error@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbx),%rsi
lea    0xc(%rsp),%rdi
mov    (%rbx),%edx
call   1050 <memmove@plt>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    11e6 <connect_socket+0x86>
lea    0xe35(%rip),%rdi
call   1060 <error@plt>
add    $0x18,%rsp
pop    %rbx
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 { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"ERROR , no such host\0A\00"
@AF_INET = global i32 0
@.str.1 = [17 x i8] c"ERROR connecting\00"
define void @connect_socket(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%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 %15
13: ; preds = %3
%14 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %3
%16 = bitcast %struct.sockaddr_in* %7 to i8*
%17 = call i32 @bzero(i8* %16, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %18, i32* %19
%20 = load %struct.hostent*, %struct.hostent** %8
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = bitcast i32* %25 to i8*
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @bcopy(i8* %23, i8* %26, i32 %29)
%31 = load i32, i32* %6
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %15
%39 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %15
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"ERROR , no such host\0A\00"
@AF_INET = global i32 0
@.str.1 = [17 x i8] c"ERROR connecting\00"
define void @connect_socket(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call %struct.hostent* @gethostbyname(i8* %1)
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %3
%11 = call i32 @bzero(i8* %5, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 1
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i8*
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%18 = bitcast i32* %17 to i8*
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 @htons(i32 %2)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i64 @connect(i32 %0, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %10
%27 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %10
ret void
}