#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;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ flg;
typedef int /*<<< orphan*/ addrstr;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_BROADCAST;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int OpenUdpSocket(const char *host, unsigned short int port) {
  char addrstr[INET_ADDRSTRLEN];
  struct hostent *hent = NULL;
  struct sockaddr_in addr;
  int fd = -1, flg = 1;
  if (!port) {
    goto error;
  }
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    goto error;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons((int)port);
  if (!host) {
    setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &flg, sizeof(flg));
    addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  } else {
    if (!(hent = gethostbyname(host))) {
      goto error;
    }
    if (!inet_ntop(AF_INET, hent->h_addr, addrstr, sizeof(addrstr))) {
      goto error;
    }
    if (!inet_pton(AF_INET, addrstr, &addr.sin_addr)) {
      goto error;
    }
  }
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    goto error;
  }
  return (fd);
error:
  if (fd >= 0) {
    close(fd);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<OpenUdpSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x44(%rbp)
movl   $0x1,-0x48(%rbp)
cmpw   $0x0,-0x12(%rbp)
jne    1209 <OpenUdpSocket+0x69>
jmp    1343 <OpenUdpSocket+0x1a3>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jge    1233 <OpenUdpSocket+0x93>
jmp    1343 <OpenUdpSocket+0x1a3>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movzwl -0x12(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12a0 <OpenUdpSocket+0x100>
mov    -0x44(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x40(%rbp)
jmp    1311 <OpenUdpSocket+0x171>
mov    -0x10(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12bc <OpenUdpSocket+0x11c>
jmp    1343 <OpenUdpSocket+0x1a3>
mov    -0x58(%rbp),%rdx
mov    -0x60(%rbp),%rax
mov    0x2d15(%rip),%rcx
mov    (%rcx),%edi
mov    -0x30(%rbp),%rcx
mov    (%rcx),%esi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    12e8 <OpenUdpSocket+0x148>
jmp    1343 <OpenUdpSocket+0x1a3>
mov    -0x58(%rbp),%rsi
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
call   10a0 <inet_pton@plt>
cmp    $0x0,%eax
jne    130c <OpenUdpSocket+0x16c>
jmp    1343 <OpenUdpSocket+0x1a3>
jmp    1311 <OpenUdpSocket+0x171>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
je     1331 <OpenUdpSocket+0x191>
jmp    1343 <OpenUdpSocket+0x1a3>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1363 <OpenUdpSocket+0x1c3>
cmpl   $0x0,-0x44(%rbp)
jl     1355 <OpenUdpSocket+0x1b5>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenUdpSocket>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e00(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x1,-0x3c(%rbp)
mov    $0xffffffff,%eax
test   %esi,%esi
je     12d0 <OpenUdpSocket+0x130>
mov    %esi,%r14d
mov    %rdi,%r13
mov    0x2df2(%rip),%r15
mov    (%r15),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
test   %eax,%eax
js     12cb <OpenUdpSocket+0x12b>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,-0x30(%rbp)
movzwl %r14w,%edi
call   1050 <htons@plt>
mov    %eax,-0x34(%rbp)
test   %r13,%r13
je     1274 <OpenUdpSocket+0xd4>
mov    %r13,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
mov    0x2d9b(%rip),%r14
mov    -0x2c(%rbp),%r15d
je     12c3 <OpenUdpSocket+0x123>
mov    (%r14),%edi
mov    (%rax),%esi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
je     12c3 <OpenUdpSocket+0x123>
mov    (%r14),%edi
lea    -0x38(%rbp),%rdx
mov    %rbx,%rsi
call   10a0 <inet_pton@plt>
test   %eax,%eax
jne    12ad <OpenUdpSocket+0x10d>
jmp    12c3 <OpenUdpSocket+0x123>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    -0x2c(%rbp),%r15d
mov    %r15d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x38(%rbp)
lea    -0x38(%rbp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
je     12df <OpenUdpSocket+0x13f>
mov    %r15d,%edi
call   1080 <close@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%eax
jmp    12d0 <OpenUdpSocket+0x130>

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@INADDR_BROADCAST = global i32 0
define i32 @OpenUdpSocket(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.hostent*
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%13 = load i32, i32* @INET_ADDRSTRLEN
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
store %struct.hostent* null, %struct.hostent** %8
store i32 -1, i32* %10
store i32 1, i32* %11
%17 = load i16, i16* %5
%18 = icmp ne i16 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %2
br label %74
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
br label %74
26: ; preds = %20
%27 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i16, i16* %5
%31 = zext i16 %30 to i32
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i8*, i8** %4
%35 = icmp ne i8* %34, null
br i1 %35, label %45, label %36
36: ; preds = %26
%37 = load i32, i32* %10
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_BROADCAST
%40 = call i32 @setsockopt(i32 %37, i32 %38, i32 %39, i32* %11, i32 4)
%41 = load i32, i32* @INADDR_BROADCAST
%42 = call i32 @htonl(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
store i32 %42, i32* %44
br label %66
45: ; preds = %26
%46 = load i8*, i8** %4
%47 = call %struct.hostent* @gethostbyname(i8* %46)
store %struct.hostent* %47, %struct.hostent** %8
%48 = icmp ne %struct.hostent* %47, null
br i1 %48, label %50, label %49
49: ; preds = %45
br label %74
50: ; preds = %45
%51 = load i32, i32* @AF_INET
%52 = load %struct.hostent*, %struct.hostent** %8
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = trunc i64 %14 to i32
%56 = call i32 @inet_ntop(i32 %51, i32 %54, i8* %16, i32 %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %50
br label %74
59: ; preds = %50
%60 = load i32, i32* @AF_INET
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%62 = call i32 @inet_pton(i32 %60, i8* %16, %struct.TYPE_2__* %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %59
br label %74
65: ; preds = %59
br label %66
66: ; preds = %65, %36
%67 = load i32, i32* %10
%68 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%69 = call i64 @connect(i32 %67, %struct.sockaddr* %68, i32 12)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %66
br label %74
72: ; preds = %66
%73 = load i32, i32* %10
store i32 %73, i32* %3
store i32 1, i32* %12
br label %81
74: ; preds = %71, %64, %58, %49, %25, %19
%75 = load i32, i32* %10
%76 = icmp sge i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i32, i32* %10
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %77, %74
store i32 -1, i32* %3
store i32 1, i32* %12
br label %81
81: ; preds = %80, %72
%82 = load i8*, i8** %6
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@INADDR_BROADCAST = global i32 0
define i32 @OpenUdpSocket(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @INET_ADDRSTRLEN
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%10 = icmp eq i16 %1, 0
br i1 %10, label %51, label %11
11: ; preds = %2
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %51, label %16
16: ; preds = %11
%17 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = zext i16 %1 to i32
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = icmp eq i8* %0, null
br i1 %23, label %24, label %31
24: ; preds = %16
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_BROADCAST
%27 = call i32 @setsockopt(i32 %14, i32 %25, i32 %26, i32* %4, i32 4)
%28 = load i32, i32* @INADDR_BROADCAST
%29 = call i32 @htonl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %29, i32* %30
br label %45
31: ; preds = %16
%32 = call %struct.hostent* @gethostbyname(i8* %0)
%33 = icmp eq %struct.hostent* %32, null
br i1 %33, label %49, label %34
34: ; preds = %31
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @inet_ntop(i32 %35, i32 %37, i8* %7, i32 %5)
%39 = icmp eq i32 %38, 0
br i1 %39, label %49, label %40
40: ; preds = %34
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%43 = call i32 @inet_pton(i32 %41, i8* %7, %struct.TYPE_2__* %42)
%44 = icmp eq i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %40, %24
%46 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%47 = call i64 @connect(i32 %14, %struct.sockaddr* %46, i32 12)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %45, %40, %34, %31
%50 = call i32 @close(i32 %14)
br label %51
51: ; preds = %2, %11, %49, %45
%52 = phi i32 [ %14, %45 ], [ -1, %49 ], [ -1, %11 ], [ -1, %2 ]
ret i32 %52
}