#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 */
typedef int /*<<< orphan*/ uint16_t;
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;
};
typedef int /*<<< orphan*/ si;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void *tcp_client_init(const char *host, uint16_t port) {
  int *fd = (int *)calloc(1, sizeof(int));
  struct sockaddr_in si;
  *fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == *fd) {
    printf("socket: %s\n", strerror(errno));
    return NULL;
  }
  si.sin_family = AF_INET;
  si.sin_addr.s_addr = inet_addr(host);
  si.sin_port = htons(port);
  if (-1 == connect(*fd, (struct sockaddr *)&si, sizeof(si))) {
    printf("connect: %s\n", strerror(errno));
    close(*fd);
    return NULL;
  }
  return (void *)fd;
}


x86 assembly (clang, -O0):
<tcp_client_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rcx
mov    $0xffffffff,%eax
cmp    (%rcx),%eax
jne    1205 <tcp_client_init+0x85>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1288 <tcp_client_init+0x108>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1280 <tcp_client_init+0x100>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xda9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1288 <tcp_client_init+0x108>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_client_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     1225 <tcp_client_init+0xa5>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1244 <tcp_client_init+0xc4>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf8(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
jmp    1242 <tcp_client_init+0xc2>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc6(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"connect: %s\0A\00"
define i8* @tcp_client_init(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i64 @calloc(i32 1, i32 4)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %6
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
%14 = load i32*, i32** %6
store i32 %13, i32* %14
%15 = load i32*, i32** %6
%16 = load i32, i32* %15
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %20)
store i8* null, i8** %3
br label %47
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i8*, i8** %4
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %5
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%35 = call i32 @connect(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 -1, %35
br i1 %36, label %37, label %44
37: ; preds = %22
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32*, i32** %6
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
store i8* null, i8** %3
br label %47
44: ; preds = %22
%45 = load i32*, i32** %6
%46 = bitcast i32* %45 to i8*
store i8* %46, i8** %3
br label %47
47: ; preds = %44, %37, %18
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"connect: %s\0A\00"
define i8* @tcp_client_init(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = tail call i64 @calloc(i32 1, i32 4)
%5 = inttoptr i64 %4 to i32*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = tail call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %35
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 %1)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %5
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %16
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
br label %35
33: ; preds = %16
%34 = inttoptr i64 %4 to i8*
br label %35
35: ; preds = %33, %27, %12
%36 = phi i8* [ null, %12 ], [ null, %27 ], [ %34, %33 ]
ret i8* %36
}