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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_client_socket(char *ip, short port) {
  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = inet_addr(ip);
  server_addr.sin_port = htons(port);
  int s_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (0 > s_fd) {
    perror("socket error!\n");
    return -1;
  }
  if (-1 ==
      connect(s_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))) {
    perror("connect error");
    return -2;
  }
  return s_fd;
}


x86 assembly (clang, -O0):
<create_client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
movswl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jle    11c6 <create_client_socket+0x76>
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <create_client_socket+0xb4>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11fe <create_client_socket+0xae>
lea    0xe22(%rip),%rdi
call   1050 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1204 <create_client_socket+0xb4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_client_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    0x2e88(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
call   1040 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11b4 <create_client_socket+0x64>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11c7 <create_client_socket+0x77>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11aa <create_client_socket+0x5a>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffe,%ebx
jmp    11aa <create_client_socket+0x5a>

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
@.str = [15 x i8] c"socket error!\0A\00"
@.str.1 = [14 x i8] c"connect error\00"
define i32 @create_client_socket(i8* %0, i16 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i8*, i8** %4
%11 = call i32 @inet_addr(i8* %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i16, i16* %5
%15 = call i32 @htons(i16 signext %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sgt i32 0, %20
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %33
24: ; preds = %2
%25 = load i32, i32* %7
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 -1, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %33
31: ; preds = %24
%32 = load i32, i32* %7
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %29, %22
%34 = load i32, i32* %3
ret i32 %34
}

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
@.str = [15 x i8] c"socket error!\0A\00"
@.str.1 = [14 x i8] c"connect error\00"
define i32 @create_client_socket(i8* %0, i16 signext %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @inet_addr(i8* %0)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %7, i32* %8
%9 = tail call i32 @htons(i16 signext %1)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %23
17: ; preds = %2
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = call i32 @connect(i32 %13, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %17, %21, %15
%24 = phi i32 [ -1, %15 ], [ -2, %21 ], [ %13, %17 ]
ret i32 %24
}