#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*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to_host(char *host, struct sockaddr_in *serv_addr,
                    struct hostent *server, fd_set *rfds) {
  int maxfd, sockfd;
  const int TELNET = 21;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  maxfd = sockfd + 1;
  FD_ZERO(rfds);
  FD_SET(sockfd, rfds);
  server = gethostbyname(host);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  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(TELNET);
  if (connect(sockfd, (struct sockaddr *)serv_addr, sizeof(*serv_addr)) < 0) {
    fprintf(stderr, "Error connecting.\n");
    return -1;
  }
  printf("Connected to %s\n", host);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x15,-0x34(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1211 <connect_to_host+0x51>
lea    0xdf4(%rip),%rdi
call   1090 <error@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1263 <connect_to_host+0xa3>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    $0x15,%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12ea <connect_to_host+0x12a>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <connect_to_host+0x140>
mov    -0x10(%rbp),%rsi
lea    0xd48(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e10(%rip),%r12
mov    (%r12),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11ff <connect_to_host+0x3f>
lea    0xe06(%rip),%rdi
call   1090 <error@plt>
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r14,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     12a3 <connect_to_host+0xe3>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
mov    0x8(%rbp),%rsi
lea    0x4(%rbx),%rdi
mov    0x0(%rbp),%edx
call   1080 <memmove@plt>
mov    $0x15,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     127c <connect_to_host+0xbc>
lea    0xdcb(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    1297 <connect_to_host+0xd7>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.2 = [19 x i8] c"Error connecting.\0A\00"
@.str.3 = [17 x i8] c"Connected to %s\0A\00"
define i32 @connect_to_host(i8* %0, %struct.sockaddr_in* %1, %struct.hostent* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.hostent*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %7
store %struct.hostent* %2, %struct.hostent** %8
store i32* %3, i32** %9
store i32 21, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %4
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 1
store i32 %22, i32* %10
%23 = load i32*, i32** %9
%24 = call i32 @FD_ZERO(i32* %23)
%25 = load i32, i32* %11
%26 = load i32*, i32** %9
%27 = call i32 @FD_SET(i32 %25, i32* %26)
%28 = load i8*, i8** %6
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %8
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %36
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %20
%37 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%38 = bitcast %struct.sockaddr_in* %37 to i8*
%39 = call i32 @bzero(i8* %38, i32 12)
%40 = load i32, i32* @AF_INET
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 2
store i32 %40, i32* %42
%43 = load %struct.hostent*, %struct.hostent** %8
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 1
%45 = load i64, i64* %44
%46 = inttoptr i64 %45 to i8*
%47 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %47, i32 0, i32 1
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
%50 = bitcast i32* %49 to i8*
%51 = load %struct.hostent*, %struct.hostent** %8
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @bcopy(i8* %46, i8* %50, i32 %53)
%55 = call i32 @htons(i32 21)
%56 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i32 0, i32 0
store i32 %55, i32* %57
%58 = load i32, i32* %11
%59 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%60 = bitcast %struct.sockaddr_in* %59 to %struct.sockaddr*
%61 = call i64 @connect(i32 %58, %struct.sockaddr* %60, i32 12)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %36
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %70
66: ; preds = %36
%67 = load i8*, i8** %6
%68 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %67)
%69 = load i32, i32* %11
store i32 %69, i32* %5
br label %70
70: ; preds = %66, %63
%71 = load i32, i32* %5
ret i32 %71
}

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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.2 = [19 x i8] c"Error connecting.\0A\00"
@.str.3 = [17 x i8] c"Connected to %s\0A\00"
define i32 @connect_to_host(i8* %0, %struct.sockaddr_in* %1, %struct.hostent* nocapture readnone %2, i32* %3) {
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = tail call i32 @FD_ZERO(i32* %3)
%13 = tail call i32 @FD_SET(i32 %7, i32* %3)
%14 = tail call %struct.hostent* @gethostbyname(i8* %0)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %11
%21 = bitcast %struct.sockaddr_in* %1 to i8*
%22 = tail call i32 @bzero(i8* %21, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to i8*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%29 = bitcast i32* %28 to i8*
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%31 = load i32, i32* %30
tail call void @llvm.memmove.p0i8.p0i8.i32(i8* align 1 %29, i8* align 1 %27, i32 %31, i1 false)
%32 = tail call i32 @htons(i32 21)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%35 = tail call i64 @connect(i32 %7, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %20
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %42
40: ; preds = %20
%41 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %42
42: ; preds = %40, %37
%43 = phi i32 [ -1, %37 ], [ %7, %40 ]
ret i32 %43
}