#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ conn_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int client_connect(const char *server, uint16_t server_port) {
  struct sockaddr_in conn_addr;
  struct hostent *he;
  int connfd;
  int ret;
  connfd = socket(AF_INET, SOCK_STREAM, 0);
  do {
    if (connfd < 0) {
      fprintf(stderr, "Failed to %s\n", "open client socket");
      goto err;
    }
  } while (0);
  he = gethostbyname(server);
  do {
    if ((he == NULL || he->h_addr_list == NULL)) {
      fprintf(stderr, "Failed to %s\n", "found a host");
      goto err_conn;
    }
  } while (0);
  conn_addr.sin_family = he->h_addrtype;
  conn_addr.sin_port = htons(server_port);
  memcpy(&conn_addr.sin_addr, he->h_addr_list[0], he->h_length);
  memset(conn_addr.sin_zero, 0, sizeof(conn_addr.sin_zero));
  ret = connect(connfd, (struct sockaddr *)&conn_addr, sizeof(conn_addr));
  do {
    if (ret < 0) {
      fprintf(stderr, "Failed to %s\n", "connect client");
      goto err_conn;
    }
  } while (0);
  return connfd;
err_conn:
  close(connfd);
err:
  return -1;
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11d6 <client_connect+0x56>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
lea    0xe42(%rip),%rdx
call   1070 <fprintf@plt>
jmp    12c0 <client_connect+0x140>
jmp    11db <client_connect+0x5b>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1202 <client_connect+0x82>
mov    -0x30(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1223 <client_connect+0xa3>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
lea    0xe08(%rip),%rdx
call   1070 <fprintf@plt>
jmp    12b8 <client_connect+0x138>
jmp    1228 <client_connect+0xa8>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12a8 <client_connect+0x128>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
lea    0xd90(%rip),%rdx
call   1070 <fprintf@plt>
jmp    12b8 <client_connect+0x138>
jmp    12ad <client_connect+0x12d>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c7 <client_connect+0x147>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     124e <client_connect+0xce>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1229 <client_connect+0xa9>
mov    %rax,%rbp
cmpq   $0x0,0x8(%rax)
je     1229 <client_connect+0xa9>
mov    0x10(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    0x8(%rbp),%rax
mov    (%rax),%esi
mov    0x0(%rbp),%edx
call   1080 <memcpy@plt>
mov    (%rsp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <connect@plt>
test   %eax,%eax
jns    126f <client_connect+0xef>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
lea    0xe07(%rip),%rdx
jmp    1240 <client_connect+0xc0>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
lea    0xde1(%rip),%rdx
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    126a <client_connect+0xea>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
lea    0xda9(%rip),%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"Failed to %s\0A\00"
@.str.1 = [19 x i8] c"open client socket\00"
@.str.2 = [13 x i8] c"found a host\00"
@.str.3 = [15 x i8] c"connect client\00"
define i32 @client_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
br label %13
13: ; preds = %2
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %71
19: ; preds = %13
br label %20
20: ; preds = %19
%21 = load i8*, i8** %4
%22 = call %struct.hostent* @gethostbyname(i8* %21)
store %struct.hostent* %22, %struct.hostent** %7
br label %23
23: ; preds = %20
%24 = load %struct.hostent*, %struct.hostent** %7
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = load %struct.hostent*, %struct.hostent** %7
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 1
%29 = load i32*, i32** %28
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %26, %23
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %68
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = load %struct.hostent*, %struct.hostent** %7
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 3
store i32 %38, i32* %39
%40 = load i32, i32* %5
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%44 = load %struct.hostent*, %struct.hostent** %7
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 1
%46 = load i32*, i32** %45
%47 = getelementptr inbounds i32, i32* %46, i64 0
%48 = load i32, i32* %47
%49 = load %struct.hostent*, %struct.hostent** %7
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @memcpy(i32* %43, i32 %48, i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @memset(i32 %54, i32 0, i32 4)
%56 = load i32, i32* %8
%57 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%58 = call i32 @connect(i32 %56, %struct.sockaddr* %57, i32 16)
store i32 %58, i32* %9
br label %59
59: ; preds = %35
%60 = load i32, i32* %9
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %68
65: ; preds = %59
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
store i32 %67, i32* %3
br label %72
68: ; preds = %62, %31
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %16
store i32 -1, i32* %3
br label %72
72: ; preds = %71, %66
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"Failed to %s\0A\00"
@.str.1 = [19 x i8] c"open client socket\00"
@.str.2 = [13 x i8] c"found a host\00"
@.str.3 = [15 x i8] c"connect client\00"
define i32 @client_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%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 %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %45
12: ; preds = %2
%13 = tail call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 1
%17 = load i32*, i32** %16
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15, %12
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %43
22: ; preds = %15
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 2
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %24, i32* %25
%26 = tail call i32 @htons(i32 %1)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%29 = load i32*, i32** %16
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @memcpy(i32* %28, i32 %30, i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memset(i32 %35, i32 0, i32 4)
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i32 @connect(i32 %7, %struct.sockaddr* %37, i32 16)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %22
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %40, %19
%44 = call i32 @close(i32 %7)
br label %45
45: ; preds = %9, %43, %22
%46 = phi i32 [ %7, %22 ], [ -1, %43 ], [ -1, %9 ]
ret i32 %46
}