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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECONNREFUSED;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close_socket(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int cli_conn_server(int fd, char *ip, unsigned short port) {
  struct sockaddr_in s;
  bzero(&s, sizeof(struct sockaddr));
  s.sin_family = AF_INET;
  s.sin_port = htons(port);
  s.sin_addr.s_addr = inet_addr(ip);
  printf("Conecting Server...ip:%s port:%d\n", ip, port);
  if ((connect(fd, (struct sockaddr *)&s, sizeof(struct sockaddr))) == -1) {
    perror("Connecting Server error !");
    if (errno == ECONNREFUSED) {
      printf("try again\n");
    }
    close_socket(fd);
    return -1;
  }
  printf("Connect Server ok\n");
  return fd;
}


x86 assembly (clang, -O0):
<cli_conn_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1080 <bzero@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
movzwl -0x12(%rbp),%edx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1230 <cli_conn_server+0xc0>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
jne    121c <cli_conn_server+0xac>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close_socket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <cli_conn_server+0xd4>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_conn_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
call   1090 <bzero@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0xe31(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     120a <cli_conn_server+0x8a>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%rax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
jne    1238 <cli_conn_server+0xb8>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1070 <close_socket@plt>
mov    $0xffffffff,%r15d
jmp    11fc <cli_conn_server+0x7c>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [34 x i8] c"Conecting Server...ip:%s port:%d\0A\00"
@.str.1 = [26 x i8] c"Connecting Server error !\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@.str.2 = [11 x i8] c"try again\0A\00"
@.str.3 = [19 x i8] c"Connect Server ok\0A\00"
define i32 @cli_conn_server(i32 %0, i8* %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i16
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i8* %1, i8** %6
store i16 %2, i16* %7
%9 = call i32 @bzero(%struct.sockaddr_in* %8, i32 4)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i16, i16* %7
%13 = call i32 @htons(i16 zeroext %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i8*, i8** %6
%16 = call i32 @inet_addr(i8* %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i8*, i8** %6
%20 = load i16, i16* %7
%21 = zext i16 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %21)
%23 = load i32, i32* %5
%24 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%25 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %37
27: ; preds = %3
%28 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%29 = load i64, i64* @errno
%30 = load i64, i64* @ECONNREFUSED
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i32, i32* %5
%36 = call i32 @close_socket(i32 %35)
store i32 -1, i32* %4
br label %40
37: ; preds = %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* %5
store i32 %39, i32* %4
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [34 x i8] c"Conecting Server...ip:%s port:%d\0A\00"
@.str.1 = [26 x i8] c"Connecting Server error !\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@str = [18 x i8] c"Connect Server ok\00"
@str.4 = [10 x i8] c"try again\00"
define i32 @cli_conn_server(i32 %0, i8* %1, i16 zeroext %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @bzero(%struct.sockaddr_in* %4, i32 4)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i16 zeroext %2)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %9, i32* %10
%11 = tail call i32 @inet_addr(i8* %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %11, i32* %12
%13 = zext i16 %2 to i32
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %13)
%15 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%16 = call i32 @connect(i32 %0, %struct.sockaddr* %15, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %27
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%20 = load i64, i64* @errno
%21 = load i64, i64* @ECONNREFUSED
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = call i32 @close_socket(i32 %0)
br label %29
27: ; preds = %3
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = phi i32 [ -1, %25 ], [ %0, %27 ]
ret i32 %30
}