#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
struct addrinfo *default_addrinfo(char *, char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ random_sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int _connect_to_addr(char *host, char *port) {
  int s, i;
  char buffer[200];
  struct addrinfo *res = default_addrinfo(host, port);
  s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (s < 0) {
    fprintf(stderr,
            "[Error] Couldn't open socket for address %s:%s!  (Err: %s)\n",
            host, port, strerror(errno));
    exit(1);
  }
  for (i = 0; i < 10; i++) {
    if (!connect(s, res->ai_addr, res->ai_addrlen))
      break;
    if (errno == EINTR) {
      i--;
      continue;
    }
    snprintf(buffer, 200,
             "[Warning] Connection attempt %d to %s:%s failed: ", i + 1, host,
             port);
    perror(buffer);
    random_sleep(10);
  }
  if (i == 10) {
    fprintf(stderr, "[Error] Failed to connect to %s:%s! (Err: %s;\n", host,
            port, strerror(errno));
    fprintf(stderr, " This error may mean that the connection was refused.)\n");
    exit(1);
  }
  freeaddrinfo(res);
  return s;
}


x86 assembly (clang, -O0):
<_connect_to_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <default_addrinfo@plt>
mov    %rax,-0xe8(%rbp)
mov    -0xe8(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0xe8(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0xe8(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125a <_connect_to_addr+0xba>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xfc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0xf8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xf0(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0xfc(%rbp),%edi
mov    -0xf8(%rbp),%rdx
mov    -0xf0(%rbp),%rcx
mov    %rax,%r8
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    1306 <_connect_to_addr+0x166>
mov    -0x14(%rbp),%edi
mov    -0xe8(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0xe8(%rbp),%rax
mov    (%rax),%edx
call   1090 <connect@plt>
cmp    $0x0,%eax
jne    1294 <_connect_to_addr+0xf4>
jmp    1306 <_connect_to_addr+0x166>
mov    0x2d3d(%rip),%rax
mov    (%rax),%rax
mov    0x2d2b(%rip),%rcx
cmp    (%rcx),%rax
jne    12bc <_connect_to_addr+0x11c>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    12f8 <_connect_to_addr+0x158>
lea    -0xe0(%rbp),%rdi
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%r8
mov    -0x10(%rbp),%r9
mov    $0xc8,%esi
lea    0xd5f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xe0(%rbp),%rdi
call   1060 <perror@plt>
mov    $0xa,%edi
call   1070 <random_sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1261 <_connect_to_addr+0xc1>
cmpl   $0xa,-0x18(%rbp)
jne    138a <_connect_to_addr+0x1ea>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x114(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x110(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x108(%rbp)
mov    0x2c9c(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x114(%rbp),%edi
mov    -0x110(%rbp),%rdx
mov    -0x108(%rbp),%rcx
mov    %rax,%r8
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xe8(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_connect_to_addr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    %rdi,(%rsp)
mov    %rsi,0x8(%rsp)
call   1030 <default_addrinfo@plt>
mov    %rax,%rbx
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     1277 <_connect_to_addr+0xd7>
mov    %eax,%ebp
xor    %r13d,%r13d
mov    0x2df4(%rip),%r12
mov    0x2de5(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1090 <connect@plt>
test   %eax,%eax
je     125b <_connect_to_addr+0xbb>
mov    (%r12),%rax
cmp    (%r14),%rax
jne    121b <_connect_to_addr+0x7b>
add    $0xffffffff,%r13d
mov    %r13d,%eax
lea    0x1(%rax),%r13d
cmp    $0x9,%eax
jl     11f0 <_connect_to_addr+0x50>
jmp    1255 <_connect_to_addr+0xb5>
lea    0x1(%r13),%ecx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc8,%esi
lea    0xe09(%rip),%rdx
mov    (%rsp),%r8
mov    0x8(%rsp),%r9
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1060 <perror@plt>
mov    $0xa,%edi
call   1070 <random_sleep@plt>
jmp    120d <_connect_to_addr+0x6d>
cmp    $0xa,%r13d
je     12b5 <_connect_to_addr+0x115>
mov    %rbx,%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d62(%rip),%rax
mov    (%rax),%ebx
mov    0x2d51(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xd6a(%rip),%rsi
mov    %ebx,%edi
mov    (%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d24(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    (%r12),%rdi
call   10a0 <strerror@plt>
lea    0xd9f(%rip),%rsi
mov    %ebx,%edi
mov    (%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [60 x i8] c"[Error] Couldn't open socket for address %s:%s! (Err: %s)\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [50 x i8] c"[Warning] Connection attempt %d to %s:%s failed: \00"
@.str.2 = [47 x i8] c"[Error] Failed to connect to %s:%s! (Err: %s;\0A\00"
@.str.3 = [56 x i8] c" This error may mean that the connection was refused.)\0A\00"
define i32 @_connect_to_addr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [200 x i8]
%8 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = load i8*, i8** %4
%11 = call %struct.addrinfo* @default_addrinfo(i8* %9, i8* %10)
store %struct.addrinfo* %11, %struct.addrinfo** %8
%12 = load %struct.addrinfo*, %struct.addrinfo** %8
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 4
%14 = load i32, i32* %13
%15 = load %struct.addrinfo*, %struct.addrinfo** %8
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 3
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %8
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %14, i32 %17, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = load i8*, i8** %4
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27, i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %2
store i32 0, i32* %6
br label %33
33: ; preds = %64, %32
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 10
br i1 %35, label %36, label %67
36: ; preds = %33
%37 = load i32, i32* %5
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @connect(i32 %37, i32 %40, i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %36
br label %67
47: ; preds = %36
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINTR
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %6
%53 = add nsw i32 %52, -1
store i32 %53, i32* %6
br label %64
54: ; preds = %47
%55 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
%58 = load i8*, i8** %3
%59 = load i8*, i8** %4
%60 = call i32 @snprintf(i8* %55, i32 200, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %57, i8* %58, i8* %59)
%61 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%62 = call i32 @perror(i8* %61)
%63 = call i32 @random_sleep(i32 10)
br label %64
64: ; preds = %54, %51
%65 = load i32, i32* %6
%66 = add nsw i32 %65, 1
store i32 %66, i32* %6
br label %33
67: ; preds = %46, %33
%68 = load i32, i32* %6
%69 = icmp eq i32 %68, 10
br i1 %69, label %70, label %80
70: ; preds = %67
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %3
%73 = load i8*, i8** %4
%74 = load i64, i64* @errno
%75 = call i8* @strerror(i64 %74)
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %72, i8* %73, i8* %75)
%77 = load i32, i32* @stderr
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %67
%81 = load %struct.addrinfo*, %struct.addrinfo** %8
%82 = call i32 @freeaddrinfo(%struct.addrinfo* %81)
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [60 x i8] c"[Error] Couldn't open socket for address %s:%s! (Err: %s)\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [50 x i8] c"[Warning] Connection attempt %d to %s:%s failed: \00"
@.str.2 = [47 x i8] c"[Error] Failed to connect to %s:%s! (Err: %s;\0A\00"
@.str.3 = [56 x i8] c" This error may mean that the connection was refused.)\0A\00"
define i32 @_connect_to_addr(i8* %0, i8* %1) {
%3 = alloca [200 x i8]
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%5 = tail call %struct.addrinfo* @default_addrinfo(i8* %0, i8* %1)
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
%11 = load i32, i32* %10
%12 = tail call i32 @socket(i32 %7, i32 %9, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
br label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i64, i64* @errno
%20 = tail call i8* @strerror(i64 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %14, %40
%24 = phi i32 [ 0, %14 ], [ %42, %40 ]
%25 = load i32, i32* %15
%26 = load i32, i32* %16
%27 = call i32 @connect(i32 %12, i32 %25, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %54, label %29
29: ; preds = %23
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = add nsw i32 %24, -1
br label %40
35: ; preds = %29
%36 = add nsw i32 %24, 1
%37 = call i32 @snprintf(i8* %4, i32 200, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %36, i8* %0, i8* %1)
%38 = call i32 @perror(i8* %4)
%39 = call i32 @random_sleep(i32 10)
br label %40
40: ; preds = %35, %33
%41 = phi i32 [ %34, %33 ], [ %24, %35 ]
%42 = add nsw i32 %41, 1
%43 = icmp slt i32 %41, 9
br i1 %43, label %23, label %44
44: ; preds = %40
%45 = icmp eq i32 %42, 10
br i1 %45, label %46, label %54
46: ; preds = %44
%47 = load i32, i32* @stderr
%48 = load i64, i64* @errno
%49 = call i8* @strerror(i64 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1, i8* %49)
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %23, %44
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %5)
ret i32 %12
}