#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_storage {
  int dummy;
};
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  scalar_t__ sa_family;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int getsockname(int, struct sockaddr *, int *);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int local4remote(struct sockaddr *ra, struct sockaddr_storage *la) {
  int s, r;
  socklen_t llen;
  s = socket(ra->sa_family, SOCK_DGRAM, 0);
  if (s == -1) {
    return (-1);
  }
  if (connect(s, ra,
              (((ra)->sa_family == AF_INET) ? sizeof(struct sockaddr_in)
                                            : sizeof(struct sockaddr_in6))) ==
      -1) {
    close(s);
    return (-1);
  }
  llen = sizeof(*la);
  r = getsockname(s, ((struct sockaddr *)(la)), &llen);
  close(s);
  return (r);
}


x86 assembly (clang, -O0):
<local4remote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1180 <local4remote+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <local4remote+0xb0>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    0x2e53(%rip),%rax
mov    (%rax),%rdx
mov    $0x4,%eax
cmp    %rdx,%rcx
cmove  %rax,%rax
mov    %eax,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11c8 <local4remote+0x88>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <local4remote+0xb0>
movl   $0x4,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getsockname@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local4remote>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11a2 <local4remote+0x62>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     119b <local4remote+0x5b>
movl   $0x4,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <getsockname@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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