#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
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*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ logcat(char *, char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int connect_server(char *destIp, int destPort, int udp, int timeout) {
  int result;
  struct sockaddr_in address;
  int s_socket;
  struct timeval tv;
  int ret;
  if (udp)
    s_socket = socket(AF_INET, SOCK_DGRAM, 0);
  else
    s_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (timeout <= 0)
    timeout = 30;
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  ret = setsockopt(s_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
                   sizeof(struct timeval));
  if (ret < 0) {
    logcat("setsockopt: %s\n", strerror(errno));
    return -1;
  }
  bzero(&address, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = inet_addr(destIp);
  address.sin_port = htons(destPort);
  result = connect(s_socket, (struct sockaddr *)&address, sizeof(address));
  if (result == -1) {
    return -1;
  }
  logcat("##### Connect to server: %s:%d, socket = %d #####\n", destIp,
         destPort, s_socket);
  return s_socket;
}


x86 assembly (clang, -O0):
<connect_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11c0 <connect_server+0x40>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
jmp    11dc <connect_server+0x5c>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11ed <connect_server+0x6d>
movl   $0x1e,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1257 <connect_server+0xd7>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <connect_server+0x150>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12b2 <connect_server+0x132>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <connect_server+0x150>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x34(%rbp),%ecx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e3d(%rip),%r12
mov    (%r12),%edi
test   %edx,%edx
jne    11ac <connect_server+0x2c>
mov    0x2e16(%rip),%rax
jmp    11b3 <connect_server+0x33>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %ebx,%ebx
mov    $0x1e,%eax
cmovg  %ebx,%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1258 <connect_server+0xd8>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1277 <connect_server+0xf7>
lea    0xdc9(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <logcat@plt>
jmp    127c <connect_server+0xfc>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xd90(%rip),%rdi
xor    %eax,%eax
call   1050 <logcat@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i32 0
@.str.1 = [51 x i8] c"##### Connect to server: %s:%d, socket = %d #####\0A\00"
define i32 @connect_server(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%15 = load i32, i32* %8
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %12
br label %25
21: ; preds = %4
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %12
br label %25
25: ; preds = %21, %17
%26 = load i32, i32* %9
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 30, i32* %9
br label %29
29: ; preds = %28, %25
%30 = load i32, i32* %9
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %32
%33 = load i32, i32* %12
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_SNDTIMEO
%36 = bitcast %struct.timeval* %13 to i8*
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i8* %36, i32 16)
store i32 %37, i32* %14
%38 = load i32, i32* %14
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %29
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
%45 = call i32 (i8*, i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %44)
store i32 -1, i32* %5
br label %69
46: ; preds = %29
%47 = call i32 @bzero(%struct.sockaddr_in* %11, i32 12)
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %48, i32* %49
%50 = load i8*, i8** %6
%51 = call i32 @inet_addr(i8* %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 0
store i32 %51, i32* %53
%54 = load i32, i32* %7
%55 = call i32 @htons(i32 %54)
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %55, i32* %56
%57 = load i32, i32* %12
%58 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%59 = call i32 @connect(i32 %57, %struct.sockaddr* %58, i32 12)
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %63
62: ; preds = %46
store i32 -1, i32* %5
br label %69
63: ; preds = %46
%64 = load i8*, i8** %6
%65 = load i32, i32* %7
%66 = load i32, i32* %12
%67 = call i32 (i8*, i8*, ...) @logcat(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %64, i32 %65, i32 %66)
%68 = load i32, i32* %12
store i32 %68, i32* %5
br label %69
69: ; preds = %63, %62, %40
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i32 0
@.str.1 = [51 x i8] c"##### Connect to server: %s:%d, socket = %d #####\0A\00"
define i32 @connect_server(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.timeval
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = icmp eq i32 %2, 0
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @SOCK_DGRAM
%13 = select i1 %9, i32 %11, i32 %12
%14 = tail call i32 @socket(i32 %10, i32 %13, i32 0)
%15 = icmp slt i32 %3, 1
%16 = select i1 %15, i32 30, i32 %3
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_SNDTIMEO
%21 = call i32 @setsockopt(i32 %14, i32 %19, i32 %20, i8* %8, i32 16)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %4
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = sext i32 %25 to i64
%27 = inttoptr i64 %26 to i8*
%28 = call i32 (i8*, i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %42
29: ; preds = %4
%30 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @inet_addr(i8* %0)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %33, i32* %34
%35 = call i32 @htons(i32 %1)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%38 = call i32 @connect(i32 %14, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %42, label %40
40: ; preds = %29
%41 = call i32 (i8*, i8*, ...) @logcat(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i32 %14)
br label %42
42: ; preds = %29, %40, %23
%43 = phi i32 [ -1, %23 ], [ %14, %40 ], [ -1, %29 ]
ret i32 %43
}