#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*/ tv;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servername;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOL_TCP;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ init_sockaddr(struct sockaddr_in *, char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_port(const char *hostname, int port, int block, int mode) {
  int sock;
  int count = 0;
  struct sockaddr_in servername;
  do {
    sock = socket(PF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
      perror("socket (scheduler)");
      return sock;
    }
    if (1) {
      int one = 1;
      setsockopt(sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
    }
    if (1) {
      struct timeval tv;
      tv.tv_sec = 10;
      tv.tv_usec = 0;
      setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv));
    }
    init_sockaddr(&servername, hostname, port);
    if (!block) {
      fcntl(sock, F_SETFD, O_NONBLOCK);
    }
    if (connect(sock, (struct sockaddr *)&servername, sizeof(servername)) < 0) {
      perror("connect");
      close(sock);
      if (!block) {
        return -1;
      }
      sleep(4);
      count++;
    } else {
      return sock;
    }
  } while (count < 10);
  return -1;
}


x86 assembly (clang, -O0):
<connect_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11d9 <connect_port+0x59>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ed <connect_port+0x16d>
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
movl   $0xa,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x10,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%rdi
call   1060 <init_sockaddr@plt>
cmpl   $0x0,-0x18(%rbp)
jne    126f <connect_port+0xef>
mov    -0x20(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    12cc <connect_port+0x14c>
lea    0xd82(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12b4 <connect_port+0x134>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <connect_port+0x16d>
mov    $0x4,%edi
call   1090 <sleep@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12d7 <connect_port+0x157>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ed <connect_port+0x16d>
jmp    12dc <connect_port+0x15c>
cmpl   $0xa,-0x24(%rbp)
jl     119c <connect_port+0x1c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,0x4(%rsp)
mov    %rdi,0x8(%rsp)
mov    $0xa,%r12d
mov    0x2e01(%rip),%rax
mov    0x2e02(%rip),%rcx
lea    0x20(%rsp),%rbx
mov    (%rax),%edi
mov    %rcx,%r14
mov    (%rcx),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12c0 <connect_port+0x140>
movl   $0x1,0x10(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%r8d
mov    %ebp,%edi
lea    0x10(%rsp),%r13
mov    %r13,%rcx
xor    %eax,%eax
call   1030 <setsockopt@plt>
movl   $0xa,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    $0x10,%r8d
mov    %ebp,%edi
mov    %r13,%rcx
xor    %eax,%eax
call   1030 <setsockopt@plt>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    0x4(%rsp),%edx
call   1060 <init_sockaddr@plt>
test   %r15d,%r15d
jne    1260 <connect_port+0xe0>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    12af <connect_port+0x12f>
lea    0xd98(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
test   %r15d,%r15d
je     12af <connect_port+0x12f>
mov    $0x4,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%r12d
mov    0x2d02(%rip),%rax
mov    %r14,%rcx
jne    11b3 <connect_port+0x33>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd39(%rip),%rdi
call   1070 <perror@plt>
jmp    12af <connect_port+0x12f>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"socket (scheduler)\00"
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@F_SETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @connect_port(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 i32
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca %struct.timeval
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
br label %15
15: ; preds = %65, %4
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* %10
store i32 %23, i32* %5
br label %69
24: ; preds = %15
store i32 1, i32* %13
%25 = load i32, i32* %10
%26 = load i32, i32* @SOL_TCP
%27 = load i32, i32* @TCP_NODELAY
%28 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %25, i32 %26, i32 %27, i32* %13, i64 4)
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 10, i32* %29
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %30
%31 = load i32, i32* %10
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_RCVTIMEO
%34 = bitcast %struct.timeval* %14 to i8*
%35 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %31, i32 %32, i32 %33, i8* %34, i64 16)
%36 = load i8*, i8** %6
%37 = load i32, i32* %7
%38 = call i32 @init_sockaddr(%struct.sockaddr_in* %12, i8* %36, i32 %37)
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %24
%42 = load i32, i32* %10
%43 = load i32, i32* @F_SETFD
%44 = load i32, i32* @O_NONBLOCK
%45 = call i32 @fcntl(i32 %42, i32 %43, i32 %44)
br label %46
46: ; preds = %41, %24
%47 = load i32, i32* %10
%48 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%49 = call i64 @connect(i32 %47, %struct.sockaddr* %48, i32 4)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %62
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* %10
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %8
%56 = icmp ne i32 %55, 0
br i1 %56, label %58, label %57
57: ; preds = %51
store i32 -1, i32* %5
br label %69
58: ; preds = %51
%59 = call i32 @sleep(i32 4)
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 1
store i32 %61, i32* %11
br label %64
62: ; preds = %46
%63 = load i32, i32* %10
store i32 %63, i32* %5
br label %69
64: ; preds = %58
br label %65
65: ; preds = %64
%66 = load i32, i32* %11
%67 = icmp slt i32 %66, 10
br i1 %67, label %15, label %68
68: ; preds = %65
store i32 -1, i32* %5
br label %69
69: ; preds = %68, %62, %57, %21
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"socket (scheduler)\00"
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@F_SETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @connect_port(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
%13 = icmp eq i32 %2, 0
%14 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %15
15: ; preds = %41, %4
%16 = phi i32 [ 0, %4 ], [ %43, %41 ]
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %45
23: ; preds = %15
store i32 1, i32* %6
%24 = load i32, i32* @SOL_TCP
%25 = load i32, i32* @TCP_NODELAY
%26 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %19, i32 %24, i32 %25, i32* %6, i64 4)
store i32 10, i32* %11
store i64 0, i64* %12
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_RCVTIMEO
%29 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %19, i32 %27, i32 %28, %struct.timeval* %7, i64 16)
%30 = call i32 @init_sockaddr(%struct.sockaddr_in* %5, i8* %0, i32 %1)
br i1 %13, label %31, label %35
31: ; preds = %23
%32 = load i32, i32* @F_SETFD
%33 = load i32, i32* @O_NONBLOCK
%34 = call i32 @fcntl(i32 %19, i32 %32, i32 %33)
br label %35
35: ; preds = %31, %23
%36 = call i64 @connect(i32 %19, %struct.sockaddr* %14, i32 4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @close(i32 %19)
br i1 %13, label %45, label %41
41: ; preds = %38
%42 = call i32 @sleep(i32 4)
%43 = add nuw nsw i32 %16, 1
%44 = icmp eq i32 %43, 10
br i1 %44, label %45, label %15
45: ; preds = %41, %35, %38, %21
%46 = phi i32 [ %19, %21 ], [ -1, %41 ], [ %19, %35 ], [ -1, %38 ]
ret i32 %46
}