#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*/ saddrBindingSocket;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
void bind_socket(int sockfd) {
  static int port = 1050;
  struct sockaddr_in saddrBindingSocket;
  int bound = 0;
  int times = 0;
  saddrBindingSocket.sin_family = AF_INET;
  saddrBindingSocket.sin_addr.s_addr = INADDR_ANY;
  do {
    int res;
    saddrBindingSocket.sin_port = htons(port);
    res = bind(sockfd, (struct sockaddr *)&saddrBindingSocket,
               sizeof(saddrBindingSocket));
    if (res == 0)
      bound = 1;
    port++;
  } while (!bound && (times++ < 20));
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2ea8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2ed9(%rip),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1040 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1184 <bind_socket+0x64>
movl   $0x1,-0x14(%rbp)
mov    0x2ea6(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e9d(%rip)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x1d(%rbp)
jne    11b6 <bind_socket+0x96>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x14,%eax
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1151 <bind_socket+0x31>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2eb7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
xor    %ebx,%ebx
mov    0x2ee4(%rip),%edi
mov    %rsp,%r14
nop
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <bind@plt>
mov    0x2ec3(%rip),%edi
add    $0x1,%edi
mov    %edi,0x2eba(%rip)
test   %eax,%eax
je     1184 <bind_socket+0x64>
lea    0x1(%rbx),%eax
cmp    $0x14,%ebx
mov    %eax,%ebx
jb     1150 <bind_socket+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@bind_socket.port = internal global i32 1050
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define void @bind_socket(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
store i32 %9, i32* %11
br label %12
12: ; preds = %32, %1
%13 = load i32, i32* @bind_socket.port
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %2
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @bind(i32 %16, %struct.sockaddr* %17, i32 12)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %12
store i32 1, i32* %4
br label %22
22: ; preds = %21, %12
%23 = load i32, i32* @bind_socket.port
%24 = add nsw i32 %23, 1
store i32 %24, i32* @bind_socket.port
br label %25
25: ; preds = %22
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
%31 = icmp slt i32 %29, 20
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %12, label %34
34: ; preds = %32
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@bind_socket.port = internal unnamed_addr global i32 1050
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define void @bind_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %4, i32* %5
%6 = load i32, i32* @INADDR_ANY
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%9 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%10 = load i32, i32* @bind_socket.port
br label %11
11: ; preds = %11, %1
%12 = phi i32 [ %10, %1 ], [ %18, %11 ]
%13 = phi i32 [ 0, %1 ], [ %19, %11 ]
%14 = call i32 @htons(i32 %12)
store i32 %14, i32* %8
%15 = call i32 @bind(i32 %0, %struct.sockaddr* %9, i32 12)
%16 = icmp ne i32 %15, 0
%17 = load i32, i32* @bind_socket.port
%18 = add nsw i32 %17, 1
store i32 %18, i32* @bind_socket.port
%19 = add nuw nsw i32 %13, 1
%20 = icmp ult i32 %13, 20
%21 = select i1 %16, i1 %20, i1 false
br i1 %21, label %11, label %22
22: ; preds = %11
ret void
}