#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_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ echoserver;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int prober_bind_port(int port) {
  int sock;
  struct sockaddr_in echoserver;
  int sndsize = 1024 * 1024, ret = 0;
  sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock == -1) {
    fprintf(stderr, "couldn't creat socket");
    return -1;
  }
  ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&sndsize, sizeof(int));
  sndsize = 1024 * 1024;
  ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&sndsize, sizeof(int));
  memset(&echoserver, 0, sizeof(echoserver));
  echoserver.sin_family = AF_INET;
  echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
  echoserver.sin_port = htons(port);
  if (bind(sock, (struct sockaddr *)&echoserver, sizeof(echoserver)) < 0) {
    fprintf(stderr, "Failed to bind the server socket");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<prober_bind_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x100000,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d7 <prober_bind_port+0x67>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <prober_bind_port+0x136>
mov    -0xc(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
movl   $0x100000,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12a0 <prober_bind_port+0x130>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <prober_bind_port+0x136>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prober_bind_port>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
movl   $0x100000,0x4(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1261 <prober_bind_port+0xf1>
mov    %eax,%ebx
mov    0x2e31(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x100000,0x4(%rsp)
mov    0x0(%rbp),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    127b <prober_bind_port+0x10b>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
jmp    1271 <prober_bind_port+0x101>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"couldn't creat socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [33 x i8] c"Failed to bind the server socket\00"
define i32 @prober_bind_port(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 1048576, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_SNDBUF
%21 = bitcast i32* %6 to i8*
%22 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i8* %21, i32 4)
store i32 %22, i32* %7
store i32 1048576, i32* %6
%23 = load i32, i32* %4
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_RCVBUF
%26 = bitcast i32* %6 to i8*
%27 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i8* %26, i32 4)
store i32 %27, i32* %7
%28 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* %3
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %4
%39 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%40 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %17
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
45: ; preds = %17
%46 = load i32, i32* %4
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %42, %14
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"couldn't creat socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [33 x i8] c"Failed to bind the server socket\00"
define i32 @prober_bind_port(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 1048576, i32* %3
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_UDP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %35
14: ; preds = %1
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_SNDBUF
%17 = call i32 @setsockopt(i32 %9, i32 %15, i32 %16, i8* %5, i32 4)
store i32 1048576, i32* %3
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_RCVBUF
%20 = call i32 @setsockopt(i32 %9, i32 %18, i32 %19, i8* %5, i32 4)
%21 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = call i32 @htons(i32 %0)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i64 @bind(i32 %9, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %14
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %14, %32, %11
%36 = phi i32 [ -1, %11 ], [ -1, %32 ], [ %9, %14 ]
ret i32 %36
}