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

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ccnl_open_udpdev(int port) {
  int s;
  struct sockaddr_in si;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("udp socket");
    return -1;
  }
  si.sin_addr.s_addr = INADDR_ANY;
  si.sin_port = htons(port);
  si.sin_family = PF_INET;
  if (bind(s, (struct sockaddr *)&si, sizeof(si)) < 0) {
    perror("udp sock bind");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<ccnl_open_udpdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1189 <ccnl_open_udpdev+0x49>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <ccnl_open_udpdev+0xa5>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    11df <ccnl_open_udpdev+0x9f>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <ccnl_open_udpdev+0xa5>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ccnl_open_udpdev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e7f(%rip),%r14
mov    (%r14),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11a7 <ccnl_open_udpdev+0x67>
mov    %eax,%ebx
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r14),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1040 <bind@plt>
test   %rax,%rax
js     11b0 <ccnl_open_udpdev+0x70>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe52(%rip),%rdi
jmp    11b7 <ccnl_open_udpdev+0x77>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    119c <ccnl_open_udpdev+0x5c>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [11 x i8] c"udp socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"udp sock bind\00"
define i32 @ccnl_open_udpdev(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = load i32, i32* @PF_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 slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
13: ; preds = %1
%14 = load i32, i32* @INADDR_ANY
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* %3
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @PF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
28: ; preds = %13
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %26, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [11 x i8] c"udp socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"udp sock bind\00"
define i32 @ccnl_open_udpdev(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %22
10: ; preds = %1
%11 = load i32, i32* @INADDR_ANY
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @PF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %6, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %10, %20, %8
%23 = phi i32 [ -1, %8 ], [ -1, %20 ], [ %6, %10 ]
ret i32 %23
}