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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ assert(int);
int connect(int const, struct sockaddr const *, int const);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int const);
int socket_connect_tcp(const int fd, const char *ip, const int port) {
  assert(fd != -1 && ip != NULL && port > 0);
  struct sockaddr_in server_addr;
  const socklen_t addr_len = sizeof(server_addr);
  memset(&server_addr, 0, addr_len);
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  inet_pton(AF_INET, ip, &server_addr.sin_addr.s_addr);
  memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
  return connect(fd, (const struct sockaddr *)&server_addr, addr_len);
}


x86 assembly (clang, -O0):
<socket_connect_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x4(%rbp)
mov    %al,-0x35(%rbp)
je     118b <socket_connect_tcp+0x3b>
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x35(%rbp)
je     118b <socket_connect_tcp+0x3b>
cmpl   $0x0,-0x14(%rbp)
setg   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x18,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
add    $0x8,%rdx
call   1060 <inet_pton@plt>
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1070 <connect@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect_tcp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cmp    $0xffffffff,%edi
setne  %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
test   %edx,%edx
setg   %al
and    %cl,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e4c(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r12),%edi
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1060 <inet_pton@plt>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   1070 <connect@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.sockaddr_in*, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @socket_connect_tcp(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 0
br label %17
17: ; preds = %14, %11, %3
%18 = phi i1 [ false, %11 ], [ false, %3 ], [ %16, %14 ]
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
store i32 24, i32* %8
%21 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 24)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 3
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i8*, i8** %5
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = call i32 @inet_pton(i32 %27, i8* %28, i32* %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %32
%34 = call i32 @memset(%struct.sockaddr_in* %33, i32 0, i32 8)
%35 = load i32, i32* %4
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 24)
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.sockaddr_in*, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @socket_connect_tcp(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = icmp ne i32 %0, -1
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
%8 = icmp sgt i32 %2, 0
%9 = and i1 %7, %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = bitcast %struct.sockaddr_in* %4 to i8*
%13 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 24)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %14, i32* %15
%16 = call i32 @htons(i32 %2)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%20 = call i32 @inet_pton(i32 %18, i8* %1, i32* %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %21
%23 = call i32 @memset(%struct.sockaddr_in* %22, i32 0, i32 8)
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @connect(i32 %0, %struct.sockaddr* %24, i32 24)
ret i32 %25
}