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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
void *INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_socket_create(void) {
  int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0)
    return -1;
  struct sockaddr_in listen_addr;
  memset(&listen_addr, 0, sizeof(struct sockaddr_in));
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_port = INADDR_ANY;
  listen_addr.sin_addr.s_addr = INADDR_ANY;
  int res =
      bind(sock, (struct sockaddr *)&listen_addr, sizeof(struct sockaddr_in));
  if (res < 0)
    return -2;
  return sock;
}


x86 assembly (clang, -O0):
<udp_socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1171 <udp_socket_create+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <udp_socket_create+0xa9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1040 <bind@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d3 <udp_socket_create+0xa3>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d9 <udp_socket_create+0xa9>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_socket_create>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
test   %eax,%eax
js     11aa <udp_socket_create+0x7a>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    %rax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   1040 <bind@plt>
test   %eax,%eax
mov    $0xfffffffe,%eax
cmovns %ebx,%eax
jmp    11af <udp_socket_create+0x7f>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i8* null
define i32 @udp_socket_create() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @PF_INET
%3 = load i32, i32* @SOCK_DGRAM
%4 = load i32, i32* @IPPROTO_UDP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %0
%8 = bitcast %struct.sockaddr_in* %1 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 24)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i8*, i8** @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i8* %12, i8** %13
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i8* %12, i8** %14
%15 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%16 = call i32 @bind(i32 %5, %struct.sockaddr* %15, i32 24)
%17 = icmp slt i32 %16, 0
%18 = select i1 %17, i32 -2, i32 %5
br label %19
19: ; preds = %0, %7
%20 = phi i32 [ %18, %7 ], [ -1, %0 ]
ret i32 %20
}