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

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errx(int, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(const char *addr, const char *port_s, int server) {
  int gai_res;
  struct addrinfo ai_hints;
  struct addrinfo *ai_res, *ai_iter;
  int sock;
  sock = -1;
  memset(&ai_hints, 0, sizeof(ai_hints));
  ai_hints.ai_family = AF_INET;
  ai_hints.ai_socktype = SOCK_DGRAM;
  if (server) {
    ai_hints.ai_flags = AI_PASSIVE;
  }
  gai_res = getaddrinfo(addr, port_s, &ai_hints, &ai_res);
  if (gai_res != 0) {
    errx(1, "getaddrinfo failed: %s\n", gai_strerror(gai_res));
  }
  for (ai_iter = ai_res; ai_iter != NULL; ai_iter = ai_iter->ai_next) {
    if (ai_iter->ai_family != AF_INET) {
      continue;
    }
    sock =
        socket(ai_iter->ai_family, ai_iter->ai_socktype, ai_iter->ai_protocol);
    if (sock == -1) {
      continue;
    }
    if (!server) {
      if (connect(sock, ai_iter->ai_addr, ai_iter->ai_addrlen) != -1) {
        break;
      }
    } else {
      if (bind(sock, ai_iter->ai_addr, ai_iter->ai_addrlen) != -1) {
        break;
      }
    }
    close(sock);
  }
  if (ai_iter == NULL) {
    errx(1, "No available address");
  }
  freeaddrinfo(ai_res);
  return (sock);
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ea <create_socket+0x5a>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1229 <create_socket+0x99>
mov    -0x18(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12f9 <create_socket+0x169>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
je     1258 <create_socket+0xc8>
jmp    12e8 <create_socket+0x158>
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1284 <create_socket+0xf4>
jmp    12e8 <create_socket+0x158>
cmpl   $0x0,-0x14(%rbp)
jne    12b7 <create_socket+0x127>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     12b2 <create_socket+0x122>
jmp    12f9 <create_socket+0x169>
jmp    12e0 <create_socket+0x150>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     12db <create_socket+0x14b>
jmp    12f9 <create_socket+0x169>
jmp    12e0 <create_socket+0x150>
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1231 <create_socket+0xa1>
cmpq   $0x0,-0x50(%rbp)
jne    1317 <create_socket+0x187>
mov    $0x1,%edi
lea    0xd08(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e23(%rip),%r12
mov    (%r12),%rax
mov    %rax,0x8(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
test   %r15d,%r15d
je     11e5 <create_socket+0x55>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1218 <create_socket+0x88>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdf6(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <errx@plt>
mov    (%rsp),%rbx
mov    $0xffffffff,%ebp
test   %rbx,%rbx
jne    1269 <create_socket+0xd9>
lea    0xdeb(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <errx@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1226 <create_socket+0x96>
mov    (%rbx),%rdi
cmp    (%r12),%rdi
jne    1260 <create_socket+0xd0>
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     129d <create_socket+0x10d>
mov    %eax,%ebp
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %eax,%edi
test   %r15d,%r15d
je     12b0 <create_socket+0x120>
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     12be <create_socket+0x12e>
jmp    1239 <create_socket+0xa9>
mov    $0xffffffff,%ebp
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1269 <create_socket+0xd9>
jmp    1226 <create_socket+0x96>
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1239 <create_socket+0xa9>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1260 <create_socket+0xd0>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [24 x i8] c"getaddrinfo failed: %s\0A\00"
@.str.1 = [21 x i8] c"No available address\00"
define i32 @create_socket(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %11
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 40)
%13 = load i64, i64* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i64 %13, i64* %14
%15 = load i32, i32* @SOCK_DGRAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %15, i32* %16
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* @AI_PASSIVE
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store i32 %20, i32* %21
br label %22
22: ; preds = %19, %3
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = call i32 @getaddrinfo(i8* %23, i8* %24, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* %7
%30 = call i32 @gai_strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %22
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %33, %struct.addrinfo** %10
br label %34
34: ; preds = %88, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %92
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %10
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %37
br label %88
44: ; preds = %37
%45 = load %struct.addrinfo*, %struct.addrinfo** %10
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %10
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 4
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %10
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 3
%53 = load i32, i32* %52
%54 = call i32 @socket(i64 %47, i32 %50, i32 %53)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %44
br label %88
58: ; preds = %44
%59 = load i32, i32* %6
%60 = icmp ne i32 %59, 0
br i1 %60, label %73, label %61
61: ; preds = %58
%62 = load i32, i32* %11
%63 = load %struct.addrinfo*, %struct.addrinfo** %10
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 2
%65 = load i32, i32* %64
%66 = load %struct.addrinfo*, %struct.addrinfo** %10
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 1
%68 = load i32, i32* %67
%69 = call i32 @connect(i32 %62, i32 %65, i32 %68)
%70 = icmp ne i32 %69, -1
br i1 %70, label %71, label %72
71: ; preds = %61
br label %92
72: ; preds = %61
br label %85
73: ; preds = %58
%74 = load i32, i32* %11
%75 = load %struct.addrinfo*, %struct.addrinfo** %10
%76 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %75, i32 0, i32 2
%77 = load i32, i32* %76
%78 = load %struct.addrinfo*, %struct.addrinfo** %10
%79 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %78, i32 0, i32 1
%80 = load i32, i32* %79
%81 = call i32 @bind(i32 %74, i32 %77, i32 %80)
%82 = icmp ne i32 %81, -1
br i1 %82, label %83, label %84
83: ; preds = %73
br label %92
84: ; preds = %73
br label %85
85: ; preds = %84, %72
%86 = load i32, i32* %11
%87 = call i32 @close(i32 %86)
br label %88
88: ; preds = %85, %57, %43
%89 = load %struct.addrinfo*, %struct.addrinfo** %10
%90 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %89, i32 0, i32 5
%91 = load %struct.addrinfo*, %struct.addrinfo** %90
store %struct.addrinfo* %91, %struct.addrinfo** %10
br label %34
92: ; preds = %83, %71, %34
%93 = load %struct.addrinfo*, %struct.addrinfo** %10
%94 = icmp eq %struct.addrinfo* %93, null
br i1 %94, label %95, label %97
95: ; preds = %92
%96 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %97
97: ; preds = %95, %92
%98 = load %struct.addrinfo*, %struct.addrinfo** %9
%99 = call i32 @freeaddrinfo(%struct.addrinfo* %98)
%100 = load i32, i32* %11
ret i32 %100
}

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