#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 {
  void *sin_port;
  TYPE_1__ sin_addr;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ data_addr;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEPORT;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
void *htons(int const);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int const);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to_client(char *client, struct sockaddr_in *client_addr, int port,
                      struct hostent *server, fd_set *wfds) {
  int sockfd;
  int portno;
  const int _PORT_U = 3059;
  const int _PORT_LD = 20;
  struct sockaddr_in data_addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    perror("ERROR opening socket");
  int optval = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)) < 0)
    perror("setsockopt()");
  data_addr.sin_family = AF_INET;
  data_addr.sin_addr.s_addr = INADDR_ANY;
  data_addr.sin_port = htons(_PORT_LD);
  while (bind(sockfd, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) {
    sleep(1);
  }
  server = gethostbyname(client);
  if (server == NULL) {
    fprintf(stderr, "DNS\n");
    exit(0);
  }
  bzero((char *)client_addr, sizeof(*client_addr));
  client_addr->sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&(client_addr->sin_addr.s_addr),
        server->h_length);
  client_addr->sin_port = htons(_PORT_U);
  if ((portno = connect(sockfd, (struct sockaddr *)client_addr,
                        sizeof(*client_addr))) < 0) {
    fprintf(stderr, "Error connecting.\n");
    return -1;
  }
  printf("Connected to %s on port %d\n", client, _PORT_LD);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0xbf3,-0x3c(%rbp)
movl   $0x14,-0x40(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    122c <connect_to_client+0x5c>
lea    0xdd9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x5c(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
lea    -0x5c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    126d <connect_to_client+0x9d>
lea    0xdad(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    $0x14,%edi
call   1040 <htons@plt>
mov    %rax,-0x58(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12bf <connect_to_client+0xef>
mov    $0x1,%edi
call   10e0 <sleep@plt>
jmp    1295 <connect_to_client+0xc5>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12f3 <connect_to_client+0x123>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0xd3b(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x18,%esi
call   10b0 <bzero@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
add    $0x8,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    $0xbf3,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x18,%edx
call   10d0 <connect@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    1380 <connect_to_client+0x1b0>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    0xcb8(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139b <connect_to_client+0x1cb>
mov    -0x10(%rbp),%rsi
lea    0xcaf(%rip),%rdi
mov    $0x14,%edx
call   1050 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2df7(%rip),%r12
mov    (%r12),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1335 <connect_to_client+0x165>
movl   $0x1,0x4(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1346 <connect_to_client+0x176>
mov    (%r12),%rax
mov    %rax,0x18(%rsp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x14,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    129e <connect_to_client+0xce>
lea    0x8(%rsp),%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1280 <connect_to_client+0xb0>
mov    %r14,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1357 <connect_to_client+0x187>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x18,%esi
call   10b0 <bzero@plt>
mov    (%r12),%rax
mov    %rax,0x10(%r15)
mov    0x8(%rbx),%rsi
lea    0x8(%r15),%rdi
mov    (%rbx),%edx
call   1090 <memmove@plt>
mov    $0xbf3,%edi
call   1040 <htons@plt>
mov    %rax,(%r15)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x18,%edx
call   10d0 <connect@plt>
test   %eax,%eax
js     130c <connect_to_client+0x13c>
lea    0xd3d(%rip),%rdi
mov    %r14,%rsi
mov    $0x14,%edx
call   1050 <printf@plt>
jmp    1326 <connect_to_client+0x156>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc4(%rip),%rdi
call   10a0 <perror@plt>
jmp    1207 <connect_to_client+0x37>
lea    0xcc8(%rip),%rdi
call   10a0 <perror@plt>
jmp    123c <connect_to_client+0x6c>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
lea    0xcbb(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@.str.1 = [13 x i8] c"setsockopt()\00"
@INADDR_ANY = global i32 0
@stderr = global i32 0
@.str.2 = [5 x i8] c"DNS\0A\00"
@.str.3 = [19 x i8] c"Error connecting.\0A\00"
@.str.4 = [28 x i8] c"Connected to %s on port %d\0A\00"
define i32 @connect_to_client(i8* %0, %struct.sockaddr_in* %1, i32 %2, %struct.hostent* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.sockaddr_in
%17 = alloca i32
store i8* %0, i8** %7
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %8
store i32 %2, i32* %9
store %struct.hostent* %3, %struct.hostent** %10
store i32* %4, i32** %11
store i32 3059, i32* %14
store i32 20, i32* %15
%18 = load i8*, i8** @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i8* %18, i32 %19, i32 0)
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %5
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %5
store i32 1, i32* %17
%26 = load i32, i32* %12
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEPORT
%29 = call i64 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %17, i32 4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
%34 = load i8*, i8** @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 2
store i8* %34, i8** %35
%36 = load i32, i32* @INADDR_ANY
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 1
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = call i8* @htons(i32 20)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 0
store i8* %39, i8** %40
br label %41
41: ; preds = %46, %33
%42 = load i32, i32* %12
%43 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%44 = call i64 @bind(i32 %42, %struct.sockaddr* %43, i32 24)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @sleep(i32 1)
br label %41
48: ; preds = %41
%49 = load i8*, i8** %7
%50 = call %struct.hostent* @gethostbyname(i8* %49)
store %struct.hostent* %50, %struct.hostent** %10
%51 = load %struct.hostent*, %struct.hostent** %10
%52 = icmp eq %struct.hostent* %51, null
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %48
%58 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%59 = bitcast %struct.sockaddr_in* %58 to i8*
%60 = call i32 @bzero(i8* %59, i32 24)
%61 = load i8*, i8** @AF_INET
%62 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 2
store i8* %61, i8** %63
%64 = load %struct.hostent*, %struct.hostent** %10
%65 = getelementptr inbounds %struct.hostent, %struct.hostent* %64, i32 0, i32 1
%66 = load i64, i64* %65
%67 = inttoptr i64 %66 to i8*
%68 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%69 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %68, i32 0, i32 1
%70 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %69, i32 0, i32 0
%71 = bitcast i32* %70 to i8*
%72 = load %struct.hostent*, %struct.hostent** %10
%73 = getelementptr inbounds %struct.hostent, %struct.hostent* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i32 @bcopy(i8* %67, i8* %71, i32 %74)
%76 = call i8* @htons(i32 3059)
%77 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %77, i32 0, i32 0
store i8* %76, i8** %78
%79 = load i32, i32* %12
%80 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%81 = bitcast %struct.sockaddr_in* %80 to %struct.sockaddr*
%82 = call i32 @connect(i32 %79, %struct.sockaddr* %81, i32 24)
store i32 %82, i32* %13
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %87
84: ; preds = %57
%85 = load i32, i32* @stderr
%86 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %91
87: ; preds = %57
%88 = load i8*, i8** %7
%89 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %88, i32 20)
%90 = load i32, i32* %12
store i32 %90, i32* %6
br label %91
91: ; preds = %87, %84
%92 = load i32, i32* %6
ret i32 %92
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@.str.1 = [13 x i8] c"setsockopt()\00"
@INADDR_ANY = global i32 0
@stderr = global i32 0
@.str.2 = [5 x i8] c"DNS\0A\00"
@.str.3 = [19 x i8] c"Error connecting.\0A\00"
@.str.4 = [28 x i8] c"Connected to %s on port %d\0A\00"
define i32 @connect_to_client(i8* %0, %struct.sockaddr_in* %1, i32 %2, %struct.hostent* nocapture readnone %3, i32* nocapture readnone %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = load i8*, i8** @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i8* %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %5
%16 = bitcast i32* %7 to i8*
store i32 1, i32* %7
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEPORT
%19 = call i64 @setsockopt(i32 %11, i32 %17, i32 %18, i32* %7, i32 4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i8*, i8** @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i8* %24, i8** %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = call i8* @htons(i32 20)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i8* %28, i8** %29
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @bind(i32 %11, %struct.sockaddr* %30, i32 24)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %23, %33
%34 = call i32 @sleep(i32 1)
%35 = call i64 @bind(i32 %11, %struct.sockaddr* %30, i32 24)
%36 = icmp slt i64 %35, 0
br i1 %36, label %33, label %37
37: ; preds = %33, %23
%38 = call %struct.hostent* @gethostbyname(i8* %0)
%39 = icmp eq %struct.hostent* %38, null
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %37
%45 = bitcast %struct.sockaddr_in* %1 to i8*
%46 = call i32 @bzero(i8* %45, i32 24)
%47 = load i8*, i8** @AF_INET
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i8* %47, i8** %48
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i64 0, i32 1
%50 = load i64, i64* %49
%51 = inttoptr i64 %50 to i8*
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%53 = bitcast i32* %52 to i8*
%54 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i64 0, i32 0
%55 = load i32, i32* %54
%56 = call i8* @htons(i32 3059)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i8* %56, i8** %57
%58 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%59 = call i32 @connect(i32 %11, %struct.sockaddr* %58, i32 24)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %44
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %66
64: ; preds = %44
%65 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 20)
br label %66
66: ; preds = %64, %61
%67 = phi i32 [ -1, %61 ], [ %11, %64 ]
ret i32 %67
}