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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_client(char *ip, u_short port) {
  int sockfd;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket error");
    exit(EXIT_FAILURE);
  }
  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  if (inet_pton(AF_INET, ip, &(sin.sin_addr)) <= 0) {
    perror("inet_pton error");
    exit(EXIT_FAILURE);
  }
  if (connect(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("connect error");
    exit(EXIT_FAILURE);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<socket_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11bf <socket_client+0x4f>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jg     1220 <socket_client+0xb0>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1255 <socket_client+0xe5>
lea    0xddb(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e60(%rip),%r12
mov    (%r12),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11ff <socket_client+0x8f>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <inet_pton@plt>
test   %rax,%rax
jle    1208 <socket_client+0x98>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1211 <socket_client+0xa1>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
jmp    1218 <socket_client+0xa8>
lea    0xdfe(%rip),%rdi
jmp    1218 <socket_client+0xa8>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1070 <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
@.str = [13 x i8] c"socket error\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"inet_pton error\00"
@.str.2 = [14 x i8] c"connect error\00"
define i32 @socket_client(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %4
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = load i8*, i8** %3
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%26 = call i64 @inet_pton(i32 %23, i8* %24, i32* %25)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %16
%33 = load i32, i32* %5
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %32
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"inet_pton error\00"
@.str.2 = [14 x i8] c"connect error\00"
define i32 @socket_client(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%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 = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, 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 %27
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %12
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %6, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
ret i32 %6
}