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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int p2p_serv_bind(struct in_addr *inaddr, unsigned short port) {
  int sock;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock == -1)
    return -1;
  struct sockaddr_in sin;
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  if (inaddr)
    sin.sin_addr = *inaddr;
  else
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<p2p_serv_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1185 <p2p_serv_bind+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <p2p_serv_bind+0xb3>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b6 <p2p_serv_bind+0x76>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11c7 <p2p_serv_bind+0x87>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ed <p2p_serv_bind+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <p2p_serv_bind+0xb3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p2p_serv_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e92(%rip),%r15
mov    (%r15),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     118d <p2p_serv_bind+0x4d>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movzwl %bp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
test   %r14,%r14
je     1194 <p2p_serv_bind+0x54>
mov    (%r14),%eax
jmp    11a2 <p2p_serv_bind+0x62>
mov    $0xffffffff,%ebx
jmp    11bd <p2p_serv_bind+0x7d>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%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 { %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @p2p_serv_bind(%struct.in_addr* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca %struct.in_addr*
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store %struct.in_addr* %0, %struct.in_addr** %4
store i16 %1, i16* %5
%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 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %40
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i16, i16* %5
%18 = call i32 @htons(i16 zeroext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load %struct.in_addr*, %struct.in_addr** %4
%21 = icmp ne %struct.in_addr* %20, null
br i1 %21, label %22, label %27
22: ; preds = %14
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%24 = load %struct.in_addr*, %struct.in_addr** %4
%25 = bitcast %struct.in_addr* %23 to i8*
%26 = bitcast %struct.in_addr* %24 to i8*
br label %32
27: ; preds = %14
%28 = load i32, i32* @INADDR_ANY
%29 = call i32 @htonl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%31 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %30, i32 0, i32 0
store i32 %29, i32* %31
br label %32
32: ; preds = %27, %22
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %3
br label %40
38: ; preds = %32
%39 = load i32, i32* %6
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %37, %13
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @p2p_serv_bind(%struct.in_addr* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%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 %28, label %8
8: ; preds = %2
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i16 zeroext %1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = icmp eq %struct.in_addr* %0, null
br i1 %14, label %18, label %15
15: ; preds = %8
%16 = getelementptr %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%17 = load i32, i32* %16
br label %21
18: ; preds = %8
%19 = load i32, i32* @INADDR_ANY
%20 = tail call i32 @htonl(i32 %19)
br label %21
21: ; preds = %18, %15
%22 = phi i32 [ %20, %18 ], [ %17, %15 ]
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %6, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
%27 = select i1 %26, i32 -1, i32 %6
br label %28
28: ; preds = %2, %21
%29 = phi i32 [ %27, %21 ], [ -1, %2 ]
ret i32 %29
}