#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 {
  int /*<<< orphan*/ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sockutil_get_udp_connected(struct sockaddr *addr, socklen_t addrlen) {
  int ret = socket(addr->sa_family, SOCK_DGRAM, 0);
  if (ret == -1) {
    goto bail;
  }
  if (connect(ret, addr, addrlen)) {
    goto bail;
  }
  return ret;
bail:
  if (ret != -1) {
    close(ret);
  }
  return -1;
}


x86 assembly (clang, -O0):
<sockutil_get_udp_connected>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1167 <sockutil_get_udp_connected+0x37>
jmp    1190 <sockutil_get_udp_connected+0x60>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
je     1185 <sockutil_get_udp_connected+0x55>
jmp    1190 <sockutil_get_udp_connected+0x60>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a9 <sockutil_get_udp_connected+0x79>
cmpl   $0xffffffff,-0x18(%rbp)
je     11a2 <sockutil_get_udp_connected+0x72>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockutil_get_udp_connected>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    (%rdi),%edi
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     117a <sockutil_get_udp_connected+0x4a>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1177 <sockutil_get_udp_connected+0x47>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    117a <sockutil_get_udp_connected+0x4a>
mov    %ebx,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
define i32 @sockutil_get_udp_connected(%struct.sockaddr* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i32 %1, i32* %5
%7 = load %struct.sockaddr*, %struct.sockaddr** %4
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
br label %24
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load %struct.sockaddr*, %struct.sockaddr** %4
%18 = load i32, i32* %5
%19 = call i64 @connect(i32 %16, %struct.sockaddr* %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %24
22: ; preds = %15
%23 = load i32, i32* %6
store i32 %23, i32* %3
br label %31
24: ; preds = %21, %14
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %24
store i32 -1, i32* %3
br label %31
31: ; preds = %30, %22
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
define i32 @sockutil_get_udp_connected(%struct.sockaddr* %0, i32 %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = tail call i64 @connect(i32 %6, %struct.sockaddr* %0, i32 %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @close(i32 %6)
br label %13
13: ; preds = %2, %11, %8
%14 = phi i32 [ %6, %8 ], [ -1, %2 ], [ -1, %11 ]
ret i32 %14
}