#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 {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bind(int, struct sockaddr const *, int);
struct hostent *gethostbyname(char *);
int htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connectUdp(int *sock, struct sockaddr_in *server, char *host, int port) {
  *sock = socket(AF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in myaddr;
  myaddr.sin_addr.s_addr = INADDR_ANY;
  myaddr.sin_port = 27629;
  bind(*sock, (const struct sockaddr *)&myaddr, sizeof(struct sockaddr));
  if (*sock == 0) {
    return -1;
  }
  server->sin_family = AF_INET;
  struct hostent *hp;
  hp = gethostbyname(host);
  if (hp == 0)
    return -2;
  bcopy((char *)hp->h_addr, (char *)&server->sin_addr, hp->h_length);
  server->sin_port = htons(port);
  return 0;
}


x86 assembly (clang, -O0):
<connectUdp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x6bed,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11c8 <connectUdp+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <connectUdp+0xde>
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11fc <connectUdp+0xac>
movl   $0xfffffffe,-0x4(%rbp)
jmp    122e <connectUdp+0xde>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectUdp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2e79(%rip),%r12
mov    (%r12),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,0x0(%rbp)
mov    0x2e53(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rsp)
movl   $0x6bed,(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <bind@plt>
cmpl   $0x0,0x0(%rbp)
je     11e0 <connectUdp+0x90>
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
mov    %r15,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11e7 <connectUdp+0x97>
mov    0x8(%rax),%rsi
lea    0x4(%rbx),%rdi
mov    (%rax),%edx
call   1060 <memmove@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    11ec <connectUdp+0x9c>
mov    $0xffffffff,%eax
jmp    11ec <connectUdp+0x9c>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @connectUdp(i32* %0, %struct.sockaddr_in* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.sockaddr_in*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
store i32* %0, i32** %6
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = load i32*, i32** %6
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 27629, i32* %19
%20 = load i32*, i32** %6
%21 = load i32, i32* %20
%22 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 4)
%24 = load i32*, i32** %6
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %4
store i32 -1, i32* %5
br label %53
28: ; preds = %4
%29 = load i32, i32* @AF_INET
%30 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i32 0, i32 2
store i32 %29, i32* %31
%32 = load i8*, i8** %8
%33 = call %struct.hostent* @gethostbyname(i8* %32)
store %struct.hostent* %33, %struct.hostent** %11
%34 = load %struct.hostent*, %struct.hostent** %11
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 -2, i32* %5
br label %53
37: ; preds = %28
%38 = load %struct.hostent*, %struct.hostent** %11
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i8*
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 1
%44 = bitcast %struct.TYPE_2__* %43 to i8*
%45 = load %struct.hostent*, %struct.hostent** %11
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @bcopy(i8* %41, i8* %44, i32 %47)
%49 = load i32, i32* %9
%50 = call i32 @htons(i32 %49)
%51 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 0
store i32 %50, i32* %52
store i32 0, i32* %5
br label %53
53: ; preds = %37, %36, %27
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i32, i64 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @connectUdp(i32* nocapture %0, %struct.sockaddr_in* nocapture writeonly %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = load i32, i32* @INADDR_ANY
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 27629, i32* %12
%13 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%14 = call i32 @bind(i32 %8, %struct.sockaddr* %13, i32 4)
%15 = load i32, i32* %0
%16 = icmp eq i32 %15, 0
br i1 %16, label %32, label %17
17: ; preds = %4
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = call %struct.hostent* @gethostbyname(i8* %2)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %32, label %22
22: ; preds = %17
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to i8*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%27 = bitcast %struct.TYPE_2__* %26 to i8*
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @htons(i32 %3)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %30, i32* %31
br label %32
32: ; preds = %22, %17, %4
%33 = phi i32 [ -1, %4 ], [ 0, %22 ], [ -2, %17 ]
ret i32 %33
}