#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 TYPE_2__ {
  unsigned int s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int BMI_sockio_init_sock(struct sockaddr *, char const *, int);
int EINTR;
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int ntohs(int /*<<< orphan*/);
int sprintf(char *, char *, unsigned int, unsigned int, unsigned int,
            unsigned int);
int BMI_sockio_connect_sock(int sockd, const char *name, int service, char *ip,
                            int *port) {
  struct sockaddr saddr;
  int ret;
  if ((ret = BMI_sockio_init_sock(&saddr, name, service)) != 0)
    return (ret);
connect_sock_restart:
  if (connect(sockd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    if (errno == EINTR)
      goto connect_sock_restart;
    return (-errno);
  }
  unsigned int addr = ((struct sockaddr_in *)&saddr)->sin_addr.s_addr;
  int ssize = sprintf(ip, "%u.%u.%u.%u", (addr << 24) >> 24, (addr << 16) >> 24,
                      (addr << 8) >> 24, addr >> 24);
  int l = sizeof(saddr);
  getsockname(sockd, &saddr, &l);
  *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
  return (sockd);
}


x86 assembly (clang, -O0):
<BMI_sockio_connect_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%rdi
call   1030 <BMI_sockio_init_sock@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     1191 <BMI_sockio_connect_sock+0x41>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <BMI_sockio_connect_sock+0xfd>
jmp    1196 <BMI_sockio_connect_sock+0x46>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    11e1 <BMI_sockio_connect_sock+0x91>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
jne    11ce <BMI_sockio_connect_sock+0x7e>
jmp    1196 <BMI_sockio_connect_sock+0x46>
xor    %eax,%eax
mov    0x2e09(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <BMI_sockio_connect_sock+0xfd>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%edx
shl    $0x18,%edx
shr    $0x18,%edx
mov    -0x38(%rbp),%ecx
shl    $0x10,%ecx
shr    $0x18,%ecx
mov    -0x38(%rbp),%r8d
shl    $0x8,%r8d
shr    $0x18,%r8d
mov    -0x38(%rbp),%r9d
shr    $0x18,%r9d
lea    0xde8(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x4,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1050 <getsockname@plt>
mov    -0x30(%rbp),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMI_sockio_connect_sock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %edi,%ebp
mov    %rsp,%rdi
call   1030 <BMI_sockio_init_sock@plt>
test   %eax,%eax
jne    11ec <BMI_sockio_connect_sock+0x9c>
mov    %rsp,%r12
mov    0x2e67(%rip),%r13
mov    0x2e58(%rip),%rbx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    11a0 <BMI_sockio_connect_sock+0x50>
mov    0x0(%r13),%eax
cmp    (%rbx),%eax
je     1180 <BMI_sockio_connect_sock+0x30>
neg    %eax
jmp    11ec <BMI_sockio_connect_sock+0x9c>
mov    0x4(%rsp),%eax
movzbl %al,%edx
movzbl %ah,%ecx
mov    %eax,%esi
shr    $0x10,%esi
movzbl %sil,%r8d
shr    $0x18,%eax
lea    0xe43(%rip),%rsi
mov    %r15,%rdi
mov    %eax,%r9d
call   1060 <sprintf@plt>
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1050 <getsockname@plt>
mov    (%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @BMI_sockio_connect_sock(i32 %0, i8* %1, i32 %2, i8* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca %struct.sockaddr
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32* %4, i32** %11
%17 = load i8*, i8** %8
%18 = load i32, i32* %9
%19 = call i32 @BMI_sockio_init_sock(%struct.sockaddr* %12, i8* %17, i32 %18)
store i32 %19, i32* %13
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %5
%22 = load i32, i32* %13
store i32 %22, i32* %6
br label %62
23: ; preds = %5
br label %24
24: ; preds = %32, %23
%25 = load i32, i32* %7
%26 = call i64 @connect(i32 %25, %struct.sockaddr* %12, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = load i32, i32* @EINTR
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %24
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %6
br label %62
36: ; preds = %24
%37 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %37, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %14
%41 = load i8*, i8** %10
%42 = load i32, i32* %14
%43 = shl i32 %42, 24
%44 = lshr i32 %43, 24
%45 = load i32, i32* %14
%46 = shl i32 %45, 16
%47 = lshr i32 %46, 24
%48 = load i32, i32* %14
%49 = shl i32 %48, 8
%50 = lshr i32 %49, 24
%51 = load i32, i32* %14
%52 = lshr i32 %51, 24
%53 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %44, i32 %47, i32 %50, i32 %52)
store i32 %53, i32* %15
store i32 4, i32* %16
%54 = load i32, i32* %7
%55 = call i32 @getsockname(i32 %54, %struct.sockaddr* %12, i32* %16)
%56 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @ntohs(i32 %58)
%60 = load i32*, i32** %11
store i32 %59, i32* %60
%61 = load i32, i32* %7
store i32 %61, i32* %6
br label %62
62: ; preds = %36, %33, %21
%63 = load i32, i32* %6
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @BMI_sockio_connect_sock(i32 %0, i8* %1, i32 %2, i8* nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = alloca %struct.sockaddr
%7 = alloca i32
%8 = bitcast %struct.sockaddr* %6 to i8*
%9 = call i32 @BMI_sockio_init_sock(%struct.sockaddr* %6, i8* %1, i32 %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %5, %14
%12 = call i64 @connect(i32 %0, %struct.sockaddr* %6, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = load i32, i32* @EINTR
%17 = icmp eq i32 %15, %16
br i1 %17, label %11, label %18
18: ; preds = %14
%19 = sub nsw i32 0, %15
br label %34
20: ; preds = %11
%21 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %6, i64 1, i32 0
%22 = load i32, i32* %21
%23 = and i32 %22, 255
%24 = lshr i32 %22, 8
%25 = and i32 %24, 255
%26 = lshr i32 %22, 16
%27 = and i32 %26, 255
%28 = lshr i32 %22, 24
%29 = call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %25, i32 %27, i32 %28)
store i32 4, i32* %7
%30 = call i32 @getsockname(i32 %0, %struct.sockaddr* %6, i32* %7)
%31 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %6, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
store i32 %33, i32* %4
br label %34
34: ; preds = %5, %20, %18
%35 = phi i32 [ %19, %18 ], [ %0, %20 ], [ %9, %5 ]
ret i32 %35
}