#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;
};
struct linger {
  int l_onoff;
  int l_linger;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_LINGER;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct linger *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(const char *ip, int port) {
  int sockfd;
  struct sockaddr_in servaddr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port);
  if (inet_pton(AF_INET, ip, &servaddr.sin_addr) < 0)
    printf("inet_pton error\n");
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    printf("connect %s:%d error\n", ip, port);
    return -1;
  }
  struct linger l;
  l.l_onoff = 1;
  l.l_linger = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (struct linger *)&l,
             sizeof(struct linger));
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jge    11f0 <connect_to_server+0x80>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    122b <connect_to_server+0xbb>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <connect_to_server+0xf3>
movl   $0x1,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e47(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <inet_pton@plt>
test   %rax,%rax
jns    11f4 <connect_to_server+0x74>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     123f <connect_to_server+0xbf>
movabs $0x100000001,%rax
mov    %rax,0x18(%rsp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
jmp    1258 <connect_to_server+0xd8>
lea    0xdba(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.linger = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"connect %s:%d error\0A\00"
@SOL_SOCKET = global i32 0
@SO_LINGER = global i32 0
@str = [16 x i8] c"inet_pton error\00"
define i32 @connect_to_server(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.linger
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 %1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%16 = call i64 @inet_pton(i32 %14, i8* %0, i32* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %8, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1)
br label %33
26: ; preds = %20
%27 = bitcast %struct.linger* %4 to i8*
%28 = getelementptr inbounds %struct.linger, %struct.linger* %4, i64 0, i32 0
store i32 1, i32* %28
%29 = getelementptr inbounds %struct.linger, %struct.linger* %4, i64 0, i32 1
store i32 1, i32* %29
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_LINGER
%32 = call i32 @setsockopt(i32 %8, i32 %30, i32 %31, %struct.linger* %4, i32 8)
br label %33
33: ; preds = %26, %24
%34 = phi i32 [ -1, %24 ], [ %8, %26 ]
ret i32 %34
}