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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ AF_UNSPEC;
int AI_ADDRCONFIG;
int AI_NUMERICHOST;
int AI_PASSIVE;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int usock_connect(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                  int, int);
int usock_inet(int type, const char *host, const char *service, int socktype,
               bool server) {
  struct addrinfo *result, *rp;
  struct addrinfo hints = {
      .ai_family = (type & 0x2000)   ? AF_INET6
                   : (type & 0x4000) ? AF_INET
                                     : AF_UNSPEC,
      .ai_socktype = socktype,
      .ai_flags = AI_ADDRCONFIG | ((type & 0x0100) ? AI_PASSIVE : 0) |
                  ((type & 0x0800) ? AI_NUMERICHOST : 0),
  };
  int sock = -1;
  if (getaddrinfo(host, service, &hints, &result))
    return -1;
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sock = usock_connect(rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype,
                         server);
    if (sock >= 0)
      break;
  }
  freeaddrinfo(result);
  return sock;
}


x86 assembly (clang, -O0):
<usock_inet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x50(%rbp)
lea    -0x50(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x60(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x8(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     118d <usock_inet+0x5d>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
jmp    1197 <usock_inet+0x67>
xor    %eax,%eax
mov    %eax,-0x64(%rbp)
jmp    1197 <usock_inet+0x67>
mov    -0x58(%rbp),%eax
mov    -0x64(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x68(%rbp)
mov    -0x8(%rbp),%eax
and    $0x800,%eax
cmp    $0x0,%eax
je     11c4 <usock_inet+0x94>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
jmp    11ce <usock_inet+0x9e>
xor    %eax,%eax
mov    %eax,-0x6c(%rbp)
jmp    11ce <usock_inet+0x9e>
mov    -0x60(%rbp),%rax
mov    -0x68(%rbp),%ecx
mov    -0x6c(%rbp),%edx
or     %edx,%ecx
mov    %ecx,(%rax)
lea    -0x50(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x78(%rbp)
mov    -0x8(%rbp),%eax
and    $0x2000,%eax
cmp    $0x0,%eax
je     120a <usock_inet+0xda>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
jmp    123e <usock_inet+0x10e>
mov    -0x8(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
je     122c <usock_inet+0xfc>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
jmp    1238 <usock_inet+0x108>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
mov    -0x80(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x78(%rbp),%rax
mov    -0x7c(%rbp),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1040 <getaddrinfo@plt>
cmp    $0x0,%rax
je     128f <usock_inet+0x15f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <usock_inet+0x1ca>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12eb <usock_inet+0x1bb>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
call   1030 <usock_connect@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jl     12d5 <usock_inet+0x1a5>
jmp    12eb <usock_inet+0x1bb>
jmp    12da <usock_inet+0x1aa>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1297 <usock_inet+0x167>
mov    -0x28(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usock_inet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %edi,%eax
shl    $0x17,%eax
sar    $0x1f,%eax
mov    0x2e99(%rip),%rcx
and    (%rcx),%eax
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
mov    %edi,%ecx
shl    $0x14,%ecx
sar    $0x1f,%ecx
mov    0x2e67(%rip),%rbp
and    0x0(%rbp),%ecx
mov    %r15d,0x8(%rsp)
or     %eax,%ecx
mov    %ecx,0xc(%rsp)
test   $0x2000,%edi
jne    1190 <usock_inet+0x60>
test   $0x4000,%edi
jne    1199 <usock_inet+0x69>
mov    0x2e4a(%rip),%rax
jmp    11a0 <usock_inet+0x70>
mov    0x2e31(%rip),%rax
jmp    11a0 <usock_inet+0x70>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x14(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x8(%rsp),%rax
mov    %rsp,%rcx
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rax,%rdx
call   1040 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1225 <usock_inet+0xf5>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     1219 <usock_inet+0xe9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
mov    %r15d,%ecx
mov    %r14d,%r8d
call   1030 <usock_connect@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1213 <usock_inet+0xe3>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    11f0 <usock_inet+0xc0>
mov    (%rsp),%rdi
jmp    1220 <usock_inet+0xf0>
mov    $0xffffffff,%ebx
xor    %edi,%edi
call   1050 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AI_ADDRCONFIG = global i32 0
@AI_PASSIVE = global i32 0
@AI_NUMERICHOST = global i32 0
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@AF_UNSPEC = global i32 0
define i32 @usock_inet(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.addrinfo*
%13 = alloca %struct.addrinfo*
%14 = alloca %struct.addrinfo
%15 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 0
%17 = load i32, i32* %10
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 1
%19 = load i32, i32* @AI_ADDRCONFIG
%20 = load i32, i32* %7
%21 = and i32 %20, 256
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %5
%24 = load i32, i32* @AI_PASSIVE
br label %26
25: ; preds = %5
br label %26
26: ; preds = %25, %23
%27 = phi i32 [ %24, %23 ], [ 0, %25 ]
%28 = or i32 %19, %27
%29 = load i32, i32* %7
%30 = and i32 %29, 2048
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = load i32, i32* @AI_NUMERICHOST
br label %35
34: ; preds = %26
br label %35
35: ; preds = %34, %32
%36 = phi i32 [ %33, %32 ], [ 0, %34 ]
%37 = or i32 %28, %36
store i32 %37, i32* %18
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 2
%39 = load i32, i32* %7
%40 = and i32 %39, 8192
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = load i32, i32* @AF_INET6
br label %54
44: ; preds = %35
%45 = load i32, i32* %7
%46 = and i32 %45, 16384
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* @AF_INET
br label %52
50: ; preds = %44
%51 = load i32, i32* @AF_UNSPEC
br label %52
52: ; preds = %50, %48
%53 = phi i32 [ %49, %48 ], [ %51, %50 ]
br label %54
54: ; preds = %52, %42
%55 = phi i32 [ %43, %42 ], [ %53, %52 ]
store i32 %55, i32* %38
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 3
store i32 0, i32* %56
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 4
store i32 0, i32* %57
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %58
store i32 -1, i32* %15
%59 = load i8*, i8** %8
%60 = load i8*, i8** %9
%61 = call i64 @getaddrinfo(i8* %59, i8* %60, %struct.addrinfo* %14, %struct.addrinfo** %12)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %54
store i32 -1, i32* %6
br label %94
64: ; preds = %54
%65 = load %struct.addrinfo*, %struct.addrinfo** %12
store %struct.addrinfo* %65, %struct.addrinfo** %13
br label %66
66: ; preds = %86, %64
%67 = load %struct.addrinfo*, %struct.addrinfo** %13
%68 = icmp ne %struct.addrinfo* %67, null
br i1 %68, label %69, label %90
69: ; preds = %66
%70 = load %struct.addrinfo*, %struct.addrinfo** %13
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 4
%72 = load i32, i32* %71
%73 = load %struct.addrinfo*, %struct.addrinfo** %13
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 3
%75 = load i32, i32* %74
%76 = load %struct.addrinfo*, %struct.addrinfo** %13
%77 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %76, i32 0, i32 2
%78 = load i32, i32* %77
%79 = load i32, i32* %10
%80 = load i32, i32* %11
%81 = call i32 @usock_connect(i32 %72, i32 %75, i32 %78, i32 %79, i32 %80)
store i32 %81, i32* %15
%82 = load i32, i32* %15
%83 = icmp sge i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %69
br label %90
85: ; preds = %69
br label %86
86: ; preds = %85
%87 = load %struct.addrinfo*, %struct.addrinfo** %13
%88 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %87, i32 0, i32 5
%89 = load %struct.addrinfo*, %struct.addrinfo** %88
store %struct.addrinfo* %89, %struct.addrinfo** %13
br label %66
90: ; preds = %84, %66
%91 = load %struct.addrinfo*, %struct.addrinfo** %12
%92 = call i32 @freeaddrinfo(%struct.addrinfo* %91)
%93 = load i32, i32* %15
store i32 %93, i32* %6
br label %94
94: ; preds = %90, %63
%95 = load i32, i32* %6
ret i32 %95
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AI_ADDRCONFIG = global i32 0
@AI_PASSIVE = global i32 0
@AI_NUMERICHOST = global i32 0
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@AF_UNSPEC = global i32 0
define i32 @usock_inet(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
%8 = bitcast %struct.addrinfo** %6 to i8*
%9 = bitcast %struct.addrinfo* %7 to i8*
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 0
store i32 %3, i32* %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 1
%12 = load i32, i32* @AI_ADDRCONFIG
%13 = and i32 %0, 256
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* @AI_PASSIVE
%16 = select i1 %14, i32 0, i32 %15
%17 = or i32 %16, %12
%18 = and i32 %0, 2048
%19 = icmp eq i32 %18, 0
%20 = load i32, i32* @AI_NUMERICHOST
%21 = select i1 %19, i32 0, i32 %20
%22 = or i32 %17, %21
store i32 %22, i32* %11
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 2
%24 = and i32 %0, 8192
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %5
%27 = load i32, i32* @AF_INET6
br label %34
28: ; preds = %5
%29 = and i32 %0, 16384
%30 = icmp eq i32 %29, 0
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @AF_UNSPEC
%33 = select i1 %30, i32 %32, i32 %31
br label %34
34: ; preds = %28, %26
%35 = phi i32 [ %27, %26 ], [ %33, %28 ]
store i32 %35, i32* %23
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 3
store i32 0, i32* %36
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 4
store i32 0, i32* %37
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %38
%39 = call i64 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %7, %struct.addrinfo** %6)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %64
41: ; preds = %34
%42 = load %struct.addrinfo*, %struct.addrinfo** %6
%43 = icmp eq %struct.addrinfo* %42, null
br i1 %43, label %60, label %48
44: ; preds = %48
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 5
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
%47 = icmp eq %struct.addrinfo* %46, null
br i1 %47, label %58, label %48
48: ; preds = %41, %44
%49 = phi %struct.addrinfo* [ %46, %44 ], [ %42, %41 ]
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 4
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 3
%53 = load i32, i32* %52
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 2
%55 = load i32, i32* %54
%56 = call i32 @usock_connect(i32 %51, i32 %53, i32 %55, i32 %3, i32 %4)
%57 = icmp sgt i32 %56, -1
br i1 %57, label %58, label %44
58: ; preds = %48, %44
%59 = load %struct.addrinfo*, %struct.addrinfo** %6
br label %60
60: ; preds = %58, %41
%61 = phi %struct.addrinfo* [ null, %41 ], [ %59, %58 ]
%62 = phi i32 [ -1, %41 ], [ %56, %58 ]
%63 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
br label %64
64: ; preds = %34, %60
%65 = phi i32 [ %62, %60 ], [ -1, %34 ]
ret i32 %65
}