#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 */
typedef int /*<<< orphan*/ u_short;
typedef int u_long;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FIONBIO;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ WINS_ErrorMessage(int /*<<< orphan*/);
int /*<<< orphan*/ WSAGetLastError();
int /*<<< orphan*/ WinPrint(char *, int /*<<< orphan*/);
int bind(int, void *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int WINS_OpenSocket(int port) {
  int newsocket;
  struct sockaddr_in address;
  u_long _true = 1;
  if ((newsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
    return -1;
  }
  if (ioctl(newsocket, FIONBIO, &_true) == -1) {
    WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
    close(newsocket);
    return -1;
  }
  memset((char *)&address, 0, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons((u_short)port);
  if (bind(newsocket, (void *)&address, sizeof(address)) == -1) {
    WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
    close(newsocket);
    return -1;
  }
  return newsocket;
}


x86 assembly (clang, -O0):
<WINS_OpenSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11f6 <WINS_OpenSocket+0x66>
mov    $0x0,%al
call   1090 <WSAGetLastError@plt>
mov    %eax,%edi
call   1040 <WINS_ErrorMessage@plt>
mov    %eax,%esi
lea    0xe1b(%rip),%rdi
call   1070 <WinPrint@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <WINS_OpenSocket+0x137>
mov    -0xc(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1244 <WINS_OpenSocket+0xb4>
mov    $0x0,%al
call   1090 <WSAGetLastError@plt>
mov    %eax,%edi
call   1040 <WINS_ErrorMessage@plt>
mov    %eax,%esi
lea    0xdd5(%rip),%rdi
call   1070 <WinPrint@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <WINS_OpenSocket+0x137>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c1 <WINS_OpenSocket+0x131>
mov    $0x0,%al
call   1090 <WSAGetLastError@plt>
mov    %eax,%edi
call   1040 <WINS_ErrorMessage@plt>
mov    %eax,%esi
lea    0xd58(%rip),%rdi
call   1070 <WinPrint@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <WINS_OpenSocket+0x137>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WINS_OpenSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1259 <WINS_OpenSocket+0xc9>
mov    %eax,%ebx
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1234 <WINS_OpenSocket+0xa4>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    127a <WINS_OpenSocket+0xea>
xor    %eax,%eax
call   1090 <WSAGetLastError@plt>
mov    %eax,%edi
call   1040 <WINS_ErrorMessage@plt>
lea    0xdb7(%rip),%rdi
mov    %eax,%esi
call   1070 <WinPrint@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    1275 <WINS_OpenSocket+0xe5>
xor    %eax,%eax
call   1090 <WSAGetLastError@plt>
mov    %eax,%edi
call   1040 <WINS_ErrorMessage@plt>
lea    0xd92(%rip),%rdi
mov    %eax,%esi
call   1070 <WinPrint@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%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 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [21 x i8] c"WINS_OpenSocket: %s\0A\00"
@FIONBIO = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @WINS_OpenSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = call i32 (...) @WSAGetLastError()
%14 = call i32 @WINS_ErrorMessage(i32 %13)
%15 = call i32 @WinPrint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %2
br label %50
16: ; preds = %1
%17 = load i32, i32* %4
%18 = load i32, i32* @FIONBIO
%19 = call i32 @ioctl(i32 %17, i32 %18, i32* %6)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = call i32 (...) @WSAGetLastError()
%23 = call i32 @WINS_ErrorMessage(i32 %22)
%24 = call i32 @WinPrint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %50
27: ; preds = %16
%28 = bitcast %struct.sockaddr_in* %5 to i8*
%29 = call i32 @memset(i8* %28, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%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 i8*
%40 = call i32 @bind(i32 %38, i8* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %27
%43 = call i32 (...) @WSAGetLastError()
%44 = call i32 @WINS_ErrorMessage(i32 %43)
%45 = call i32 @WinPrint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %2
br label %50
48: ; preds = %27
%49 = load i32, i32* %4
store i32 %49, i32* %2
br label %50
50: ; preds = %48, %42, %21, %12
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [21 x i8] c"WINS_OpenSocket: %s\0A\00"
@FIONBIO = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @WINS_OpenSocket(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 1, 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 %15
11: ; preds = %1
%12 = tail call i32 (...) @WSAGetLastError()
%13 = tail call i32 @WINS_ErrorMessage(i32 %12)
%14 = tail call i32 @WinPrint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %39
15: ; preds = %1
%16 = load i32, i32* @FIONBIO
%17 = call i32 @ioctl(i32 %9, i32 %16, i32* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = call i32 (...) @WSAGetLastError()
%21 = call i32 @WINS_ErrorMessage(i32 %20)
%22 = call i32 @WinPrint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @close(i32 %9)
br label %39
24: ; preds = %15
%25 = call i32 @memset(i8* %4, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %0)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %30, i32* %31
%32 = call i32 @bind(i32 %9, i8* %4, i32 12)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %24
%35 = call i32 (...) @WSAGetLastError()
%36 = call i32 @WINS_ErrorMessage(i32 %35)
%37 = call i32 @WinPrint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = call i32 @close(i32 %9)
br label %39
39: ; preds = %24, %34, %19, %11
%40 = phi i32 [ -1, %11 ], [ -1, %19 ], [ -1, %34 ], [ %9, %24 ]
ret i32 %40
}