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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int anetSetReuseAddr(char *, int);
int /*<<< orphan*/ bzero(struct sockaddr *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int anetCreateUdpSocket(char *err, char *ip, int port, struct sockaddr *addr,
                        size_t addr_len) {
  int s;
  struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    anetSetError(err, "creating udp socket: %s", strerror(errno));
    return -1;
  }
  if (anetSetReuseAddr(err, s) == -1) {
    close(s);
    return -1;
  }
  bzero(addr, addr_len);
  addr_in->sin_family = AF_INET;
  addr_in->sin_addr.s_addr = inet_addr(ip);
  addr_in->sin_port = htons(port);
  return s;
}


x86 assembly (clang, -O0):
<anetCreateUdpSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <anetCreateUdpSocket+0x7c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xe15(%rip),%rsi
call   1060 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <anetCreateUdpSocket+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
jne    1225 <anetCreateUdpSocket+0xa5>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <anetCreateUdpSocket+0xea>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <bzero@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetCreateUdpSocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%r13
mov    %edx,0x4(%rsp)
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e46(%rip),%r14
mov    (%r14),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     11f8 <anetCreateUdpSocket+0x78>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
je     1219 <anetCreateUdpSocket+0x99>
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x8(%r13)
mov    %r15,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x4(%r13)
mov    0x4(%rsp),%edi
call   1030 <htons@plt>
mov    %eax,0x0(%r13)
jmp    1225 <anetCreateUdpSocket+0xa5>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf3(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <anetSetError@plt>
jmp    1220 <anetCreateUdpSocket+0xa0>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [24 x i8] c"creating udp socket: %s\00"
@errno = global i32 0
define i32 @anetCreateUdpSocket(i8* %0, i8* %1, i32 %2, %struct.sockaddr* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr*
%11 = alloca i64
%12 = alloca i32
%13 = alloca %struct.sockaddr_in*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.sockaddr* %3, %struct.sockaddr** %10
store i64 %4, i64* %11
%14 = load %struct.sockaddr*, %struct.sockaddr** %10
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_in*
store %struct.sockaddr_in* %15, %struct.sockaddr_in** %13
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %12
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %5
%21 = load i8*, i8** %7
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 @anetSetError(i8* %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 -1, i32* %6
br label %50
25: ; preds = %5
%26 = load i8*, i8** %7
%27 = load i32, i32* %12
%28 = call i32 @anetSetReuseAddr(i8* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* %12
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %6
br label %50
33: ; preds = %25
%34 = load %struct.sockaddr*, %struct.sockaddr** %10
%35 = load i64, i64* %11
%36 = call i32 @bzero(%struct.sockaddr* %34, i64 %35)
%37 = load i32, i32* @AF_INET
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 2
store i32 %37, i32* %39
%40 = load i8*, i8** %8
%41 = call i32 @inet_addr(i8* %40)
%42 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 1
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
store i32 %41, i32* %44
%45 = load i32, i32* %9
%46 = call i32 @htons(i32 %45)
%47 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %47, i32 0, i32 0
store i32 %46, i32* %48
%49 = load i32, i32* %12
store i32 %49, i32* %6
br label %50
50: ; preds = %33, %30, %20
%51 = load i32, i32* %6
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [24 x i8] c"creating udp socket: %s\00"
@errno = global i32 0
define i32 @anetCreateUdpSocket(i8* %0, i8* %1, i32 %2, %struct.sockaddr* nocapture writeonly %3, i64 %4) {
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %27
14: ; preds = %5
%15 = tail call i32 @anetSetReuseAddr(i8* %0, i32 %8)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @close(i32 %8)
br label %27
19: ; preds = %14
%20 = tail call i32 @bzero(%struct.sockaddr* %3, i64 %4)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %3, i64 2, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @inet_addr(i8* %1)
%24 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %3, i64 1, i32 0
store i32 %23, i32* %24
%25 = tail call i32 @htons(i32 %2)
%26 = getelementptr %struct.sockaddr, %struct.sockaddr* %3, i64 0, i32 0
store i32 %25, i32* %26
br label %27
27: ; preds = %19, %17, %10
%28 = phi i32 [ -1, %10 ], [ -1, %17 ], [ %8, %19 ]
ret i32 %28
}