#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
int /*<<< orphan*/ log_error(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int udpsocket_open(int /*<<< orphan*/, int /*<<< orphan*/, unsigned short);
int udpsocket_connect(in_addr_t raddr, unsigned short port) {
  int sock = udpsocket_open(INADDR_ANY, raddr, port);
  struct sockaddr_in sin;
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = raddr;
  sin.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    log_error("could not connect udp-socket to ip `%s' (%s)",
              inet_ntoa(sin.sin_addr), strerror(errno));
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<udpsocket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %edi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
movzwl -0xa(%rbp),%edx
call   1060 <udpsocket_open@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
movzwl -0xa(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    11f9 <udpsocket_connect+0x99>
mov    -0x20(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe18(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <udpsocket_connect+0x9f>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udpsocket_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <udpsocket_open@plt>
mov    %eax,%r14d
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    11df <udpsocket_connect+0x7f>
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%ebx
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <log_error@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@.str = [45 x i8] c"could not connect udp-socket to ip `%s' (%s)\00"
@errno = global i32 0
define i32 @udpsocket_connect(i32 %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i32 %0, i32* %4
store i16 %1, i16* %5
%8 = load i32, i32* @INADDR_ANY
%9 = load i32, i32* %4
%10 = load i16, i16* %5
%11 = call i32 @udpsocket_open(i32 %8, i32 %9, i16 zeroext %10)
store i32 %11, i32* %6
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i16, i16* %5
%18 = call i32 @htons(i16 zeroext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %6
%21 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%22 = call i64 @connect(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %2
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @inet_ntoa(i32 %27)
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @log_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %30)
store i32 -1, i32* %3
br label %34
32: ; preds = %2
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %24
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@.str = [45 x i8] c"could not connect udp-socket to ip `%s' (%s)\00"
@errno = global i32 0
define i32 @udpsocket_connect(i32 %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @INADDR_ANY
%5 = tail call i32 @udpsocket_open(i32 %4, i32 %0, i16 zeroext %1)
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %0, i32* %9
%10 = tail call i32 @htons(i16 zeroext %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%13 = call i64 @connect(i32 %5, %struct.sockaddr* %12, i32 12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* %9
%17 = call i32 @inet_ntoa(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @log_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %19)
br label %21
21: ; preds = %2, %15
%22 = phi i32 [ -1, %15 ], [ %5, %2 ]
ret i32 %22
}