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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECONNREFUSED;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr const *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int connect_to_master(char *address, int port) {
  int sock_master;
  int ret;
  sock_master = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in addr_master;
  addr_master.sin_family = AF_INET;
  addr_master.sin_port = htons(port);
  inet_aton(address, &addr_master.sin_addr);
  while ((ret = connect(sock_master, (const struct sockaddr *)&addr_master,
                        sizeof(addr_master))) == -1) {
    usleep(100 * 1000);
    if (errno == ECONNREFUSED) {
      return -1;
    }
  }
  return sock_master;
}


x86 assembly (clang, -O0):
<connect_to_master>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <inet_aton@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11f1 <connect_to_master+0xa1>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <connect_to_master+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <connect_to_master+0xa7>
jmp    119f <connect_to_master+0x4f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_master>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r15
mov    0x2e82(%rip),%r14
mov    (%r14),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r14
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <inet_aton@plt>
mov    0x2e24(%rip),%r15
mov    0x2e35(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11dc <connect_to_master+0x8c>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
jne    11b0 <connect_to_master+0x60>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@ECONNREFUSED = global i64 0
define i32 @connect_to_master(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* %5
%15 = call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%19 = call i32 @inet_aton(i8* %17, i32* %18)
br label %20
20: ; preds = %31, %2
%21 = load i32, i32* %6
%22 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%23 = call i32 @connect(i32 %21, %struct.sockaddr* %22, i32 12)
store i32 %23, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = call i32 @usleep(i32 100000)
%27 = load i64, i64* @errno
%28 = load i64, i64* @ECONNREFUSED
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %3
br label %34
31: ; preds = %25
br label %20
32: ; preds = %20
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %30
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@ECONNREFUSED = global i64 0
define i32 @connect_to_master(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%13 = call i32 @inet_aton(i8* %0, i32* %12)
%14 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %15
15: ; preds = %18, %2
%16 = call i32 @connect(i32 %6, %struct.sockaddr* %14, i32 12)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = call i32 @usleep(i32 100000)
%20 = load i64, i64* @errno
%21 = load i64, i64* @ECONNREFUSED
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %15
23: ; preds = %15, %18
%24 = phi i32 [ -1, %18 ], [ %6, %15 ]
ret i32 %24
}