#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*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ 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 server_socket_create() {
  struct addrinfo hints;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  struct addrinfo *res;
  if (getaddrinfo(NULL, "69", &hints, &res))
    return -1;
  int sockfd = -1;
  struct addrinfo *i;
  for (i = res; i != NULL && sockfd == -1; i = i->ai_next) {
    sockfd = socket(i->ai_family, i->ai_socktype, i->ai_protocol);
    if (sockfd != -1 && bind(sockfd, i->ai_addr, i->ai_addrlen) == -1) {
      close(sockfd);
      sockfd = -1;
    }
  }
  freeaddrinfo(res);
  return sockfd;
}


x86 assembly (clang, -O0):
<server_socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe59(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11ca <server_socket_create+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <server_socket_create+0x11c>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x48(%rbp)
mov    %al,-0x49(%rbp)
je     11f3 <server_socket_create+0x93>
cmpl   $0xffffffff,-0x3c(%rbp)
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1203 <server_socket_create+0xa3>
jmp    126d <server_socket_create+0x10d>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
je     1257 <server_socket_create+0xf7>
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    1257 <server_socket_create+0xf7>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x3c(%rbp)
jmp    125c <server_socket_create+0xfc>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11d9 <server_socket_create+0x79>
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_socket_create>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0xe58(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1060 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1227 <server_socket_create+0xc7>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11e9 <server_socket_create+0x89>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1222 <server_socket_create+0xc2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1218 <server_socket_create+0xb8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11e0 <server_socket_create+0x80>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    121d <server_socket_create+0xbd>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11e0 <server_socket_create+0x80>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

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

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