#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPORT_DAYTIME;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcp_connect(const char *host) {
  int connfd;
  struct sockaddr_in servaddr;
  connfd = socket(AF_INET, SOCK_STREAM, 0);
  if (connfd == -1)
    error(EXIT_FAILURE, errno, "socket");
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(IPPORT_DAYTIME);
  if (inet_pton(AF_INET, host, &servaddr.sin_addr) <= 0) {
    fprintf(stderr, "incorrection ip address format\n");
    exit(EXIT_FAILURE);
  }
  if (connect(connfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    error(EXIT_FAILURE, errno, "connect");
  return connfd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d0 <tcp_connect+0x50>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdx
call   1070 <error@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x0,%rax
jg     1240 <tcp_connect+0xc0>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1278 <tcp_connect+0xf8>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
lea    0xdb4(%rip),%rdx
call   1070 <error@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e53(%rip),%r12
mov    (%r12),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11ce <tcp_connect+0x4e>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdx
call   1070 <error@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <inet_pton@plt>
test   %rax,%rax
jle    1252 <tcp_connect+0xd2>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1244 <tcp_connect+0xc4>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdx
call   1070 <error@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [7 x i8] c"socket\00"
@IPPORT_DAYTIME = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"incorrection ip address format\0A\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @tcp_connect(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i8* %0, i8** %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @EXIT_FAILURE
%12 = load i32, i32* @errno
%13 = call i32 @error(i32 %11, i32 %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %10, %1
%15 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @IPPORT_DAYTIME
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %2
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%24 = call i64 @inet_pton(i32 %21, i8* %22, i32* %23)
%25 = icmp sle i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %14
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %14
%32 = load i32, i32* %3
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @connect(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = load i32, i32* @EXIT_FAILURE
%38 = load i32, i32* @errno
%39 = call i32 @error(i32 %37, i32 %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %36, %31
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [7 x i8] c"socket\00"
@IPPORT_DAYTIME = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"incorrection ip address format\0A\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @tcp_connect(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @EXIT_FAILURE
%10 = load i32, i32* @errno
%11 = tail call i32 @error(i32 %9, i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %8, %1
%13 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @IPPORT_DAYTIME
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%21 = call i64 @inet_pton(i32 %19, i8* %0, i32* %20)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %28
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %12
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @connect(i32 %6, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* @EXIT_FAILURE
%34 = load i32, i32* @errno
%35 = call i32 @error(i32 %33, i32 %34, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %32, %28
ret i32 %6
}