#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
extern int udp_connect(const char *host, unsigned port) {
  int err, sd, optval;
  struct sockaddr_in sa;
  sd = socket(AF_INET, SOCK_DGRAM, 0);
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  inet_pton(AF_INET, host, &sa.sin_addr);
  err = connect(sd, (struct sockaddr *)&sa, sizeof(sa));
  return err ? err : sd;
}


x86 assembly (clang, -O0):
<udp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <inet_pton@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11e1 <udp_connect+0x91>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11e7 <udp_connect+0x97>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e7f(%rip),%r12
mov    (%r12),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1050 <inet_pton@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %eax,%eax
cmove  %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @udp_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @htons(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%15 = call i32 @inet_pton(i32 %13, i8* %0, i32* %14)
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i32 @connect(i32 %7, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i32 %7, i32 %17
ret i32 %19
}