#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
#define EACCES 138
#define EADDRINUSE 137
#define EAFNOSUPPORT 136
#define EBADF 135
#define EINVAL 134
#define EMFILE 133
#define ENFILE 132
#define ENOBUFS 131
#define ENOMEM 130
#define ENOTSOCK 129
#define EPROTONOSUPPORT 128
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ assert(int);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_init_socket(short port, const char *if_addr) {
  int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (s >= 0) {
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    if (if_addr) {
      assert(sizeof(addr.sin_addr.s_addr) == 4);
      inet_pton(AF_INET, if_addr, &addr.sin_addr.s_addr);
    } else
      addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr))) {
      close(s);
      switch (errno) {
      case EACCES:
      case EADDRINUSE:
      case EBADF:
      case EINVAL:
      case ENOTSOCK:
      default:;
      }
    }
  } else {
    switch (errno) {
    case EACCES:
    case EAFNOSUPPORT:
    case EINVAL:
    case EMFILE:
    case ENFILE:
    case ENOBUFS:
    case ENOMEM:
    case EPROTONOSUPPORT:
    default:;
    }
  }
  return s;
}


x86 assembly (clang, -O0):
<udp_init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1273 <udp_init_socket+0x113>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movswl -0x2(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e8 <udp_init_socket+0x88>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <inet_pton@plt>
jmp    11f4 <udp_init_socket+0x94>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     126e <udp_init_socket+0x10e>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x81,%eax
je     125f <udp_init_socket+0xff>
jmp    1233 <udp_init_socket+0xd3>
mov    -0x24(%rbp),%eax
add    $0xffffff7a,%eax
sub    $0x2,%eax
jb     125f <udp_init_socket+0xff>
jmp    1249 <udp_init_socket+0xe9>
mov    -0x24(%rbp),%eax
add    $0xffffff77,%eax
sub    $0x1,%eax
ja     1264 <udp_init_socket+0x104>
jmp    125f <udp_init_socket+0xff>
jmp    1264 <udp_init_socket+0x104>
jmp    1269 <udp_init_socket+0x109>
jmp    126e <udp_init_socket+0x10e>
jmp    12da <udp_init_socket+0x17a>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x80,%eax
je     12cb <udp_init_socket+0x16b>
jmp    128f <udp_init_socket+0x12f>
mov    -0x28(%rbp),%eax
add    $0xffffff7e,%eax
sub    $0x5,%eax
jb     12cb <udp_init_socket+0x16b>
jmp    12a5 <udp_init_socket+0x145>
mov    -0x28(%rbp),%eax
sub    $0x88,%eax
je     12cb <udp_init_socket+0x16b>
jmp    12b8 <udp_init_socket+0x158>
mov    -0x28(%rbp),%eax
sub    $0x8a,%eax
jne    12d0 <udp_init_socket+0x170>
jmp    12cb <udp_init_socket+0x16b>
jmp    12d0 <udp_init_socket+0x170>
jmp    12d5 <udp_init_socket+0x175>
jmp    12da <udp_init_socket+0x17a>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_init_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e72(%rip),%r15
mov    (%r15),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11f4 <udp_init_socket+0x94>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movswl %bp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
test   %r14,%r14
je     11ca <udp_init_socket+0x6a>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rdx
mov    %r14,%rsi
call   1060 <inet_pton@plt>
jmp    11d7 <udp_init_socket+0x77>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     11f4 <udp_init_socket+0x94>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_ANY = global i32 0
@errno = global i32 0
define i32 @udp_init_socket(i16 signext %0, i8* %1) {
%3 = alloca i16
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i16 %0, i16* %3
store i8* %1, i8** %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %45
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i16, i16* %3
%17 = call i32 @htons(i16 signext %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %28
21: ; preds = %13
%22 = call i32 @assert(i32 1)
%23 = load i32, i32* @AF_INET
%24 = load i8*, i8** %4
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = call i32 @inet_pton(i32 %23, i8* %24, i32* %26)
br label %32
28: ; preds = %13
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
br label %32
32: ; preds = %28, %21
%33 = load i32, i32* %5
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 4)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @errno
switch i32 %40, label %42 [
i32 138, label %41
i32 137, label %41
i32 135, label %41
i32 134, label %41
i32 129, label %41
]
41: ; preds = %37, %37, %37, %37, %37
br label %42
42: ; preds = %37, %41
br label %43
43: ; preds = %42
br label %44
44: ; preds = %43, %32
br label %50
45: ; preds = %2
%46 = load i32, i32* @errno
switch i32 %46, label %48 [
i32 138, label %47
i32 136, label %47
i32 134, label %47
i32 133, label %47
i32 132, label %47
i32 131, label %47
i32 130, label %47
i32 128, label %47
]
47: ; preds = %45, %45, %45, %45, %45, %45, %45, %45
br label %48
48: ; preds = %45, %47
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49, %44
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_ANY = global i32 0
@errno = global i32 0
define i32 @udp_init_socket(i16 signext %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_UDP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %31
9: ; preds = %2
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i16 signext %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = icmp eq i8* %1, null
br i1 %15, label %21, label %16
16: ; preds = %9
%17 = tail call i32 @assert(i32 1)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%20 = call i32 @inet_pton(i32 %18, i8* %1, i32* %19)
br label %24
21: ; preds = %9
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %22, i32* %23
br label %24
24: ; preds = %21, %16
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %7, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @close(i32 %7)
br label %30
30: ; preds = %28, %24
br label %31
31: ; preds = %2, %30
ret i32 %7
}