#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ PF_INET6;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_socket() {
  int s = -1;
  struct addrinfo hints;
  struct addrinfo *res;
  struct addrinfo *p;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = PF_INET6;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  getaddrinfo(NULL, "5683", &hints, &res);
  for (p = res; p != NULL && s == -1; p = p->ai_next) {
    s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (s >= 0) {
      if (-1 == bind(s, p->ai_addr, p->ai_addrlen)) {
        close(s);
        s = -1;
      }
    }
  }
  freeaddrinfo(res);
  return s;
}


x86 assembly (clang, -O0):
<get_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0xffffffff,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe52(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x40(%rbp)
mov    %al,-0x41(%rbp)
je     11dd <get_socket+0x7d>
cmpl   $0xffffffff,-0x4(%rbp)
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11ed <get_socket+0x8d>
jmp    1262 <get_socket+0x102>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     124c <get_socket+0xec>
mov    -0x4(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1247 <get_socket+0xe7>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <get_socket+0xec>
jmp    1251 <get_socket+0xf1>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11c3 <get_socket+0x63>
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0xe53(%rip),%rsi
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1060 <getaddrinfo@plt>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1215 <get_socket+0xb5>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1201 <get_socket+0xa1>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    120f <get_socket+0xaf>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     120f <get_socket+0xaf>
cmp    $0xffffffff,%ebp
je     11d0 <get_socket+0x70>
mov    (%rsp),%r14
jmp    121a <get_socket+0xba>
mov    $0xffffffff,%ebp
mov    %r14,%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@PF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [5 x i8] c"5683\00"
define i32 @get_socket() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo*
store i32 -1, i32* %1
%5 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%6 = load i32, i32* @PF_INET6
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_DGRAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @AI_PASSIVE
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 6
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %13, %struct.addrinfo** %4
br label %14
14: ; preds = %50, %0
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp ne %struct.addrinfo* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %1
%19 = icmp eq i32 %18, -1
br label %20
20: ; preds = %17, %14
%21 = phi i1 [ false, %14 ], [ %19, %17 ]
br i1 %21, label %22, label %54
22: ; preds = %20
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 4
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 3
%28 = load i32, i32* %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %4
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %25, i32 %28, i32 %31)
store i32 %32, i32* %1
%33 = load i32, i32* %1
%34 = icmp sge i32 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %22
%36 = load i32, i32* %1
%37 = load %struct.addrinfo*, %struct.addrinfo** %4
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %4
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @bind(i32 %36, i32 %39, i32 %42)
%44 = icmp eq i32 -1, %43
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = load i32, i32* %1
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %1
br label %48
48: ; preds = %45, %35
br label %49
49: ; preds = %48, %22
br label %50
50: ; preds = %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %4
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 5
%53 = load %struct.addrinfo*, %struct.addrinfo** %52
store %struct.addrinfo* %53, %struct.addrinfo** %4
br label %14
54: ; preds = %20
%55 = load %struct.addrinfo*, %struct.addrinfo** %3
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@PF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [5 x i8] c"5683\00"
define i32 @get_socket() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 40)
%6 = load i32, i32* @PF_INET6
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_DGRAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @AI_PASSIVE
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 6
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%13 = load %struct.addrinfo*, %struct.addrinfo** %2
%14 = icmp eq %struct.addrinfo* %13, null
br i1 %14, label %44, label %15
15: ; preds = %0, %34
%16 = phi %struct.addrinfo* [ %37, %34 ], [ %13, %0 ]
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 4
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 @socket(i32 %18, i32 %20, i32 %22)
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %15
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @bind(i32 %23, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %41
32: ; preds = %25
%33 = call i32 @close(i32 %23)
br label %34
34: ; preds = %15, %32
%35 = phi i32 [ -1, %32 ], [ %23, %15 ]
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 5
%37 = load %struct.addrinfo*, %struct.addrinfo** %36
%38 = icmp ne %struct.addrinfo* %37, null
%39 = icmp eq i32 %35, -1
%40 = select i1 %38, i1 %39, i1 false
br i1 %40, label %15, label %41
41: ; preds = %25, %34
%42 = phi i32 [ %35, %34 ], [ %23, %25 ]
%43 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %44
44: ; preds = %41, %0
%45 = phi %struct.addrinfo* [ null, %0 ], [ %43, %41 ]
%46 = phi i32 [ -1, %0 ], [ %42, %41 ]
%47 = call i32 @freeaddrinfo(%struct.addrinfo* %45)
ret i32 %46
}