#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 */
typedef int uint32_t;
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;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECONNREFUSED;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dieError(char *, char const *);
int /*<<< orphan*/ dieSysError(char *);
scalar_t__ errno;
struct hostent *gethostbyname(char const *);
int htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ logDebug(char *, char const *, int);
int /*<<< orphan*/ logNorm(char *, int, int);
int /*<<< orphan*/ logVerb(char *, int, int, int, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int ipConnectPeer(const char *host, int port, int retries) {
  struct sockaddr_in addr;
  int sd;
  logDebug("connect('%s:%d')\n", host, port);
  struct hostent *hostInfo = gethostbyname(host);
  if (!hostInfo) {
    dieError("gethostbyname('%s')\n", host);
    return -2;
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = ((struct in_addr *)hostInfo->h_addr)->s_addr;
  uint32_t ipAddr = htonl(addr.sin_addr.s_addr);
  logVerb("connecting to %u.%u.%u.%u:%d\n", (ipAddr >> 24) & 0xFF,
          (ipAddr >> 16) & 0xFF, (ipAddr >> 8) & 0xFF, ipAddr & 0xFF, port);
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    dieSysError("socket()\n");
    return sd;
  }
  for (int i = 0; i < retries; i++) {
    int res = connect(sd, (struct sockaddr *)&addr, sizeof(addr));
    if (res == 0)
      break;
    if (errno == ECONNREFUSED && retries > 0) {
      logNorm("connection failed, retrying %d of %d...\n", i, retries);
      usleep(500 * 1000);
      continue;
    }
    dieSysError("connect()\n");
    return res;
  }
  logVerb("connected to %u.%u.%u.%u:%d\n", (ipAddr >> 24) & 0xFF,
          (ipAddr >> 16) & 0xFF, (ipAddr >> 8) & 0xFF, ipAddr & 0xFF, port);
  return sd;
}


x86 assembly (clang, -O0):
<ipConnectPeer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe30(%rip),%rdi
call   10b0 <logDebug@plt>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1209 <ipConnectPeer+0x59>
mov    -0x10(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1080 <dieError@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1397 <ipConnectPeer+0x1e7>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
sar    $0x18,%esi
and    $0xff,%esi
mov    -0x3c(%rbp),%edx
sar    $0x10,%edx
and    $0xff,%edx
mov    -0x3c(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x3c(%rbp),%r8d
and    $0xff,%r8d
mov    -0x14(%rbp),%r9d
lea    0xdb6(%rip),%rdi
call   1070 <logVerb@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    12b2 <ipConnectPeer+0x102>
lea    0xda3(%rip),%rdi
call   1090 <dieSysError@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1397 <ipConnectPeer+0x1e7>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1352 <ipConnectPeer+0x1a2>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    12e8 <ipConnectPeer+0x138>
jmp    1352 <ipConnectPeer+0x1a2>
mov    0x2cd9(%rip),%rax
mov    (%rax),%rax
mov    0x2ce7(%rip),%rcx
cmp    (%rcx),%rax
jne    132d <ipConnectPeer+0x17d>
cmpl   $0x0,-0x18(%rbp)
jle    132d <ipConnectPeer+0x17d>
mov    -0x40(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xd36(%rip),%rdi
call   1050 <logNorm@plt>
mov    $0x7a120,%edi
call   10c0 <usleep@plt>
jmp    1344 <ipConnectPeer+0x194>
lea    0xd44(%rip),%rdi
call   1090 <dieSysError@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1397 <ipConnectPeer+0x1e7>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12b9 <ipConnectPeer+0x109>
mov    -0x3c(%rbp),%esi
sar    $0x18,%esi
and    $0xff,%esi
mov    -0x3c(%rbp),%edx
sar    $0x10,%edx
and    $0xff,%edx
mov    -0x3c(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x3c(%rbp),%r8d
and    $0xff,%r8d
mov    -0x14(%rbp),%r9d
lea    0xcf7(%rip),%rdi
call   1070 <logVerb@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipConnectPeer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r13d
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xe32(%rip),%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   10b0 <logDebug@plt>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     12ed <ipConnectPeer+0x13d>
mov    %rax,%rbp
mov    0x2df4(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x20(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%edi
mov    %edi,0x18(%rsp)
call   1040 <htonl@plt>
mov    %eax,%ecx
movzbl %ah,%ebx
movzbl %al,%r12d
mov    %eax,%esi
shr    $0x18,%esi
shr    $0x10,%ecx
movzbl %cl,%ebp
lea    0xdf6(%rip),%rdi
mov    %esi,0x10(%rsp)
mov    %ebp,%edx
mov    %ebx,0x14(%rsp)
mov    %ebx,%ecx
mov    %r12d,%r8d
mov    %r15d,%r9d
call   1070 <logVerb@plt>
mov    (%r14),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1304 <ipConnectPeer+0x154>
mov    %ebp,0x8(%rsp)
mov    %r12d,0xc(%rsp)
test   %r13d,%r13d
jle    12cb <ipConnectPeer+0x11b>
xor    %ebp,%ebp
mov    0x2d63(%rip),%r12
nopl   (%rax)
mov    %r14d,%edi
lea    0x18(%rsp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
je     12cb <ipConnectPeer+0x11b>
mov    %eax,%ebx
mov    0x2d29(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
jne    1312 <ipConnectPeer+0x162>
lea    0xda0(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
call   1050 <logNorm@plt>
mov    $0x7a120,%edi
call   10c0 <usleep@plt>
add    $0x1,%ebp
cmp    %ebp,%r13d
jne    1280 <ipConnectPeer+0xd0>
lea    0xdb1(%rip),%rdi
mov    0x10(%rsp),%esi
mov    0x8(%rsp),%edx
mov    0x14(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    %r15d,%r9d
call   1070 <logVerb@plt>
jmp    1321 <ipConnectPeer+0x171>
lea    0xd1e(%rip),%rdi
mov    %rbx,%rsi
call   1080 <dieError@plt>
mov    $0xfffffffe,%r14d
jmp    1321 <ipConnectPeer+0x171>
lea    0xd3a(%rip),%rdi
call   1090 <dieSysError@plt>
jmp    1321 <ipConnectPeer+0x171>
lea    0xd5f(%rip),%rdi
call   1090 <dieSysError@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"connect('%s:%d')\0A\00"
@.str.1 = [21 x i8] c"gethostbyname('%s')\0A\00"
@AF_INET = global i32 0
@.str.2 = [30 x i8] c"connecting to %u.%u.%u.%u:%d\0A\00"
@SOCK_STREAM = global i32 0
@.str.3 = [10 x i8] c"socket()\0A\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@.str.4 = [41 x i8] c"connection failed, retrying %d of %d...\0A\00"
@.str.5 = [11 x i8] c"connect()\0A\00"
@.str.6 = [29 x i8] c"connected to %u.%u.%u.%u:%d\0A\00"
define i32 @ipConnectPeer(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = call i32 @logDebug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
%17 = load i8*, i8** %5
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %10
%19 = load %struct.hostent*, %struct.hostent** %10
%20 = icmp ne %struct.hostent* %19, null
br i1 %20, label %24, label %21
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = call i32 @dieError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -2, i32* %4
br label %107
24: ; preds = %3
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %6
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %28, i32* %29
%30 = load %struct.hostent*, %struct.hostent** %10
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to %struct.in_addr*
%34 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @htonl(i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = ashr i32 %42, 24
%44 = and i32 %43, 255
%45 = load i32, i32* %11
%46 = ashr i32 %45, 16
%47 = and i32 %46, 255
%48 = load i32, i32* %11
%49 = ashr i32 %48, 8
%50 = and i32 %49, 255
%51 = load i32, i32* %11
%52 = and i32 %51, 255
%53 = load i32, i32* %6
%54 = call i32 @logVerb(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %47, i32 %50, i32 %52, i32 %53)
%55 = load i32, i32* @AF_INET
%56 = load i32, i32* @SOCK_STREAM
%57 = call i32 @socket(i32 %55, i32 %56, i32 0)
store i32 %57, i32* %9
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %24
%60 = call i32 @dieSysError(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%61 = load i32, i32* %9
store i32 %61, i32* %4
br label %107
62: ; preds = %24
store i32 0, i32* %12
br label %63
63: ; preds = %89, %62
%64 = load i32, i32* %12
%65 = load i32, i32* %7
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %92
67: ; preds = %63
%68 = load i32, i32* %9
%69 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%70 = call i32 @connect(i32 %68, %struct.sockaddr* %69, i32 12)
store i32 %70, i32* %13
%71 = load i32, i32* %13
%72 = icmp eq i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %67
br label %92
74: ; preds = %67
%75 = load i64, i64* @errno
%76 = load i64, i64* @ECONNREFUSED
%77 = icmp eq i64 %75, %76
br i1 %77, label %78, label %86
78: ; preds = %74
%79 = load i32, i32* %7
%80 = icmp sgt i32 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %78
%82 = load i32, i32* %12
%83 = load i32, i32* %7
%84 = call i32 @logNorm(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %82, i32 %83)
%85 = call i32 @usleep(i32 500000)
br label %89
86: ; preds = %78, %74
%87 = call i32 @dieSysError(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%88 = load i32, i32* %13
store i32 %88, i32* %4
br label %107
89: ; preds = %81
%90 = load i32, i32* %12
%91 = add nsw i32 %90, 1
store i32 %91, i32* %12
br label %63
92: ; preds = %73, %63
%93 = load i32, i32* %11
%94 = ashr i32 %93, 24
%95 = and i32 %94, 255
%96 = load i32, i32* %11
%97 = ashr i32 %96, 16
%98 = and i32 %97, 255
%99 = load i32, i32* %11
%100 = ashr i32 %99, 8
%101 = and i32 %100, 255
%102 = load i32, i32* %11
%103 = and i32 %102, 255
%104 = load i32, i32* %6
%105 = call i32 @logVerb(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %95, i32 %98, i32 %101, i32 %103, i32 %104)
%106 = load i32, i32* %9
store i32 %106, i32* %4
br label %107
107: ; preds = %92, %86, %59, %21
%108 = load i32, i32* %4
ret i32 %108
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"connect('%s:%d')\0A\00"
@.str.1 = [21 x i8] c"gethostbyname('%s')\0A\00"
@AF_INET = global i32 0
@.str.2 = [30 x i8] c"connecting to %u.%u.%u.%u:%d\0A\00"
@SOCK_STREAM = global i32 0
@.str.3 = [10 x i8] c"socket()\0A\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@.str.4 = [41 x i8] c"connection failed, retrying %d of %d...\0A\00"
@.str.5 = [11 x i8] c"connect()\0A\00"
@.str.6 = [29 x i8] c"connected to %u.%u.%u.%u:%d\0A\00"
define i32 @ipConnectPeer(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call i32 @logDebug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @dieError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %57
11: ; preds = %3
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to %struct.in_addr*
%19 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %18, i64 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htonl(i32 %20)
%23 = lshr i32 %22, 24
%24 = lshr i32 %22, 16
%25 = and i32 %24, 255
%26 = lshr i32 %22, 8
%27 = and i32 %26, 255
%28 = and i32 %22, 255
%29 = tail call i32 @logVerb(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %25, i32 %27, i32 %28, i32 %1)
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = tail call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %11
%35 = icmp sgt i32 %2, 0
br i1 %35, label %36, label %55
36: ; preds = %34
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %40
38: ; preds = %11
%39 = tail call i32 @dieSysError(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %57
40: ; preds = %36, %48
%41 = phi i32 [ 0, %36 ], [ %51, %48 ]
%42 = call i32 @connect(i32 %32, %struct.sockaddr* %37, i32 12)
%43 = icmp eq i32 %42, 0
br i1 %43, label %55, label %44
44: ; preds = %40
%45 = load i64, i64* @errno
%46 = load i64, i64* @ECONNREFUSED
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = call i32 @logNorm(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %41, i32 %2)
%50 = call i32 @usleep(i32 500000)
%51 = add nuw nsw i32 %41, 1
%52 = icmp eq i32 %51, %2
br i1 %52, label %55, label %40
53: ; preds = %44
%54 = call i32 @dieSysError(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %57
55: ; preds = %48, %40, %34
%56 = call i32 @logVerb(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %23, i32 %25, i32 %27, i32 %28, i32 %1)
br label %57
57: ; preds = %53, %38, %55, %9
%58 = phi i32 [ -2, %9 ], [ %32, %38 ], [ %32, %55 ], [ %42, %53 ]
ret i32 %58
}