#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);
int connect(int, struct sockaddr const *, int /*<<< orphan*/);
int errno;
int getsockname(int, struct sockaddr *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_source_address(const struct sockaddr *dst, socklen_t dst_len,
                       struct sockaddr *src, socklen_t *src_len) {
  int s, rc, save;
  s = socket(dst->sa_family, SOCK_DGRAM, 0);
  if (s < 0)
    goto fail;
  rc = connect(s, dst, dst_len);
  if (rc < 0)
    goto fail;
  rc = getsockname(s, src, src_len);
  if (rc < 0)
    goto fail;
  close(s);
  return rc;
fail:
  save = errno;
  close(s);
  errno = save;
  return -1;
}


x86 assembly (clang, -O0):
<get_source_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    117f <get_source_address+0x3f>
jmp    11d5 <get_source_address+0x95>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <connect@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11a0 <get_source_address+0x60>
jmp    11d5 <get_source_address+0x95>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <getsockname@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11c2 <get_source_address+0x82>
jmp    11d5 <get_source_address+0x95>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <get_source_address+0xbc>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x34(%rbp),%ecx
mov    0x2de5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_source_address>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbp
mov    (%rdi),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1199 <get_source_address+0x59>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1050 <connect@plt>
test   %eax,%eax
js     1199 <get_source_address+0x59>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <getsockname@plt>
test   %eax,%eax
js     1199 <get_source_address+0x59>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11b2 <get_source_address+0x72>
mov    0x2e38(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@errno = global i32 0
define i32 @get_source_address(%struct.sockaddr* %0, i32 %1, %struct.sockaddr* %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca %struct.sockaddr*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %6
store i32 %1, i32* %7
store %struct.sockaddr* %2, %struct.sockaddr** %8
store i32* %3, i32** %9
%13 = load %struct.sockaddr*, %struct.sockaddr** %6
%14 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
br label %41
21: ; preds = %4
%22 = load i32, i32* %10
%23 = load %struct.sockaddr*, %struct.sockaddr** %6
%24 = load i32, i32* %7
%25 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
br label %41
29: ; preds = %21
%30 = load i32, i32* %10
%31 = load %struct.sockaddr*, %struct.sockaddr** %8
%32 = load i32*, i32** %9
%33 = call i32 @getsockname(i32 %30, %struct.sockaddr* %31, i32* %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
br label %41
37: ; preds = %29
%38 = load i32, i32* %10
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %11
store i32 %40, i32* %5
br label %46
41: ; preds = %36, %28, %20
%42 = load i32, i32* @errno
store i32 %42, i32* %12
%43 = load i32, i32* %10
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %12
store i32 %45, i32* @errno
store i32 -1, i32* %5
br label %46
46: ; preds = %41, %37
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@errno = global i32 0
define i32 @get_source_address(%struct.sockaddr* %0, i32 %1, %struct.sockaddr* %2, i32* %3) {
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %4
%11 = tail call i32 @connect(i32 %8, %struct.sockaddr* %0, i32 %1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = tail call i32 @getsockname(i32 %8, %struct.sockaddr* %2, i32* %3)
%15 = icmp slt i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @close(i32 %8)
br label %21
18: ; preds = %13, %10, %4
%19 = load i32, i32* @errno
%20 = tail call i32 @close(i32 %8)
store i32 %19, i32* @errno
br label %21
21: ; preds = %18, %16
%22 = phi i32 [ -1, %18 ], [ %14, %16 ]
ret i32 %22
}