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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int make_udp_socket(const struct in_addr *my_ip) {
  int ret;
  struct sockaddr_in addr;
  ret = socket(AF_INET, SOCK_DGRAM, 0);
  if (-1 == ret) {
    fprintf(stderr, "Error opening UDP socket: %s\n", strerror(errno));
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr = *my_ip;
  addr.sin_port = htons(22225);
  if (0 != bind(ret, &addr, sizeof(addr))) {
    fprintf(stderr, "Error binding UDP socket to port %u: %s\n", 22225,
            strerror(errno));
    (void)close(ret);
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<make_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11e0 <make_udp_socket+0x70>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <make_udp_socket+0x10e>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x56d1,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1278 <make_udp_socket+0x108>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rcx
lea    0xdc6(%rip),%rsi
mov    $0x56d1,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <make_udp_socket+0x10e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_udp_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e64(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     121a <make_udp_socket+0xaa>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    $0x56d1,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     1249 <make_udp_socket+0xd9>
mov    0x2de7(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1e(%rip),%rsi
mov    %ebp,%edi
mov    $0x56d1,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1244 <make_udp_socket+0xd4>
mov    0x2daf(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"Error opening UDP socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [41 x i8] c"Error binding UDP socket to port %u: %s\0A\00"
define i32 @make_udp_socket(%struct.in_addr* %0) {
%2 = alloca i32
%3 = alloca %struct.in_addr*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store %struct.in_addr* %0, %struct.in_addr** %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 -1, i32* %2
br label %38
16: ; preds = %1
%17 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%21 = load %struct.in_addr*, %struct.in_addr** %3
%22 = bitcast %struct.in_addr* %20 to i8*
%23 = bitcast %struct.in_addr* %21 to i8*
%24 = call i32 @htons(i32 22225)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = call i64 @bind(i32 %26, %struct.sockaddr_in* %5, i32 12)
%28 = icmp ne i64 0, %27
br i1 %28, label %29, label %36
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 22225, i8* %32)
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %38
36: ; preds = %16
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %36, %29, %11
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"Error opening UDP socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [41 x i8] c"Error binding UDP socket to port %u: %s\0A\00"
define i32 @make_udp_socket(%struct.in_addr* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %30
13: ; preds = %1
%14 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%19 = load i32, i32* %17
store i32 %19, i32* %18
%20 = call i32 @htons(i32 22225)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = call i64 @bind(i32 %6, %struct.sockaddr_in* %2, i32 12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %13
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 22225, i8* %27)
%29 = call i32 @close(i32 %6)
br label %30
30: ; preds = %13, %24, %8
%31 = phi i32 [ -1, %8 ], [ -1, %24 ], [ %6, %13 ]
ret i32 %31
}