#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ perror(char *);
void connect_udp(int sock, const struct sockaddr_in *peer,
                 struct sockaddr_in *src) {
  socklen_t addrlen = sizeof *src;
  if (connect(sock, (struct sockaddr *)peer, sizeof *peer) < 0) {
    perror("connect");
    exit(EXIT_FAILURE);
  }
  if (getsockname(sock, (struct sockaddr *)src, &addrlen) < 0) {
    perror("getsockname");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<connect_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    118f <connect_udp+0x4f>
lea    0xe84(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <getsockname@plt>
cmp    $0x0,%rax
jge    11c3 <connect_udp+0x83>
lea    0xe58(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_udp>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %edi,%ebp
movl   $0x4,0x4(%rsp)
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     117a <connect_udp+0x3a>
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <getsockname@plt>
test   %rax,%rax
js     1183 <connect_udp+0x43>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7f(%rip),%rdi
jmp    118a <connect_udp+0x4a>
lea    0xe7e(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"connect\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"getsockname\00"
define void @connect_udp(i32 %0, %struct.sockaddr_in* %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %6
store i32 4, i32* %7
%8 = load i32, i32* %4
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%10 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%11 = call i64 @connect(i32 %8, %struct.sockaddr* %10, i32 4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %3
%18 = load i32, i32* %4
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%20 = bitcast %struct.sockaddr_in* %19 to %struct.sockaddr*
%21 = call i64 @getsockname(i32 %18, %struct.sockaddr* %20, i32* %7)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %17
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"connect\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"getsockname\00"
define void @connect_udp(i32 %0, %struct.sockaddr_in* %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%6 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%7 = tail call i64 @connect(i32 %0, %struct.sockaddr* %6, i32 4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %3
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i64 @getsockname(i32 %0, %struct.sockaddr* %14, i32* %4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
ret void
}