#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_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int socket_create(const char *port) {
  struct addrinfo hints, *result, *rp;
  static int yes = 1;
  int sfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if (getaddrinfo(NULL, port, &hints, &result)) {
    perror("getaddrinfo()");
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
      continue;
    if (!bind(sfd, rp->ai_addr, rp->ai_addrlen))
      break;
    close(sfd);
  }
  freeaddrinfo(result);
  if (!rp) {
    fprintf(stderr, "Could not connect socket to any address!\n");
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1207 <socket_create+0x77>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <socket_create+0x162>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12b7 <socket_create+0x127>
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   10b0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1246 <socket_create+0xb6>
jmp    12a6 <socket_create+0x116>
mov    -0x4c(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
lea    0x2e06(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    127b <socket_create+0xeb>
jmp    12a6 <socket_create+0x116>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%eax
jne    129e <socket_create+0x10e>
jmp    12b7 <socket_create+0x127>
mov    -0x4c(%rbp),%edi
call   1050 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    120f <socket_create+0x7f>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x48(%rbp)
jne    12ec <socket_create+0x15c>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <socket_create+0x162>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <getaddrinfo@plt>
test   %rax,%rax
jne    12b4 <socket_create+0x124>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1279 <socket_create+0xe9>
mov    0x2de5(%rip),%r15
mov    0x2dae(%rip),%r12
lea    0x2e57(%rip),%r14
jmp    1229 <socket_create+0x99>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1273 <socket_create+0xe3>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <socket_create+0x90>
mov    %eax,%ebp
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1220 <socket_create+0x90>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
test   %eax,%eax
je     12a9 <socket_create+0x119>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <socket_create+0x90>
mov    (%rsp),%rdi
jmp    127b <socket_create+0xeb>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
jmp    129a <socket_create+0x10a>
lea    0xd45(%rip),%rdi
call   1080 <perror@plt>
jmp    1295 <socket_create+0x105>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@socket_create.yes = internal global i32 1
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [14 x i8] c"getaddrinfo()\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@stderr = global i32 0
@.str.1 = [42 x i8] c"Could not connect socket to any address!\0A\00"
define i32 @socket_create(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
%7 = alloca i32
store i8* %0, i8** %3
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 6
store i32 %13, i32* %14
%15 = load i8*, i8** %3
%16 = call i64 @getaddrinfo(i32* null, i8* %15, %struct.addrinfo* %4, %struct.addrinfo** %5)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %74
20: ; preds = %1
%21 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %21, %struct.addrinfo** %6
br label %22
22: ; preds = %60, %20
%23 = load %struct.addrinfo*, %struct.addrinfo** %6
%24 = icmp ne %struct.addrinfo* %23, null
br i1 %24, label %25, label %64
25: ; preds = %22
%26 = load %struct.addrinfo*, %struct.addrinfo** %6
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 4
%28 = load i32, i32* %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 3
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %28, i32 %31, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %25
br label %60
39: ; preds = %25
%40 = load i32, i32* %7
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEADDR
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i32* @socket_create.yes, i32 4)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %39
br label %60
46: ; preds = %39
%47 = load i32, i32* %7
%48 = load %struct.addrinfo*, %struct.addrinfo** %6
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %6
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @bind(i32 %47, i32 %50, i32 %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %46
br label %64
57: ; preds = %46
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %45, %38
%61 = load %struct.addrinfo*, %struct.addrinfo** %6
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 5
%63 = load %struct.addrinfo*, %struct.addrinfo** %62
store %struct.addrinfo* %63, %struct.addrinfo** %6
br label %22
64: ; preds = %56, %22
%65 = load %struct.addrinfo*, %struct.addrinfo** %5
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
%67 = load %struct.addrinfo*, %struct.addrinfo** %6
%68 = icmp ne %struct.addrinfo* %67, null
br i1 %68, label %72, label %69
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %74
72: ; preds = %64
%73 = load i32, i32* %7
store i32 %73, i32* %2
br label %74
74: ; preds = %72, %69, %18
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@socket_create.yes = internal global i32 1
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [14 x i8] c"getaddrinfo()\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@stderr = global i32 0
@.str.1 = [42 x i8] c"Could not connect socket to any address!\0A\00"
define i32 @socket_create(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %11, i32* %12
%13 = call i64 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %53, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %58
20: ; preds = %15, %44
%21 = phi %struct.addrinfo* [ %46, %44 ], [ %16, %15 ]
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 4
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 3
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %23, i32 %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %44, label %30
30: ; preds = %20
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEADDR
%33 = call i32 @setsockopt(i32 %28, i32 %31, i32 %32, i32* @socket_create.yes, i32 4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %44, label %35
35: ; preds = %30
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @bind(i32 %28, i32 %37, i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %35
%43 = call i32 @close(i32 %28)
br label %44
44: ; preds = %30, %20, %42
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 5
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
%47 = icmp eq %struct.addrinfo* %46, null
br i1 %47, label %51, label %20
48: ; preds = %35
%49 = load %struct.addrinfo*, %struct.addrinfo** %3
%50 = call i32 @freeaddrinfo(%struct.addrinfo* %49)
br label %58
51: ; preds = %44
%52 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %53
53: ; preds = %51, %15
%54 = phi %struct.addrinfo* [ %52, %51 ], [ null, %15 ]
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %58
58: ; preds = %48, %53, %18
%59 = phi i32 [ -1, %18 ], [ -1, %53 ], [ %28, %48 ]
ret i32 %59
}