#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 sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int alloc_server_udp_sock(struct sockaddr *bindaddr, int szbindaddr) {
  int fd = -1;
  int rv;
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("cannot create udp socket");
    return fd;
  }
  if (rv = bind(fd, bindaddr, szbindaddr) < 0) {
    perror("udp bind failed");
    return rv;
  }
  return fd;
}


x86 assembly (clang, -O0):
<alloc_server_udp_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1182 <alloc_server_udp_sock+0x52>
lea    0xe8e(%rip),%rdi
call   1040 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cd <alloc_server_udp_sock+0x9d>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <bind@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1c(%rbp)
test   $0x1,%al
jne    11b0 <alloc_server_udp_sock+0x80>
jmp    11c7 <alloc_server_udp_sock+0x97>
lea    0xe62(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cd <alloc_server_udp_sock+0x9d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_server_udp_sock>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1172 <alloc_server_udp_sock+0x42>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <bind@plt>
test   %rax,%rax
js     117b <alloc_server_udp_sock+0x4b>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe87(%rip),%rdi
jmp    1187 <alloc_server_udp_sock+0x57>
mov    $0x1,%ebx
lea    0xe92(%rip),%rdi
call   1040 <perror@plt>
jmp    116b <alloc_server_udp_sock+0x3b>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [25 x i8] c"cannot create udp socket\00"
@.str.1 = [16 x i8] c"udp bind failed\00"
define i32 @alloc_server_udp_sock(%struct.sockaddr* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i32 %1, i32* %5
store i32 -1, i32* %6
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %27
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load %struct.sockaddr*, %struct.sockaddr** %4
%18 = load i32, i32* %5
%19 = call i64 @bind(i32 %16, %struct.sockaddr* %17, i32 %18)
%20 = icmp slt i64 %19, 0
%21 = zext i1 %20 to i32
store i32 %21, i32* %7
br i1 %20, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %27
25: ; preds = %15
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %25, %22, %12
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [25 x i8] c"cannot create udp socket\00"
@.str.1 = [16 x i8] c"udp bind failed\00"
define i32 @alloc_server_udp_sock(%struct.sockaddr* %0, i32 %1) {
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i64 @bind(i32 %5, %struct.sockaddr* %0, i32 %1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7, %2
%11 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %7 ]
%12 = phi i32 [ %5, %2 ], [ 1, %7 ]
%13 = tail call i32 @perror(i8* %11)
br label %14
14: ; preds = %10, %7
%15 = phi i32 [ %5, %7 ], [ %12, %10 ]
ret i32 %15
}