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

/* Variables and functions */
int AI_ADDRCONFIG;
int AI_NUMERICHOST;
int AI_NUMERICSERV;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int peer_call(void) {
  int fd;
  struct addrinfo hints;
  struct addrinfo *servinfo;
  struct addrinfo *cur;
  hints.ai_flags =
      AI_ADDRCONFIG | AI_NUMERICHOST | AI_NUMERICSERV | AI_NUMERICSERV;
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  if (0 !=
      getaddrinfo(
          "l0.0.0.0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0a" + 1,
          "31337\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0p",
          &hints, &servinfo)) {
    return -5;
  }
  fd = -1;
  for (cur = servinfo; NULL != cur; cur = cur->ai_next) {
    if (-1 ==
        (fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol))) {
      continue;
    }
    if (-1 == connect(fd, cur->ai_addr, cur->ai_addrlen)) {
      close(fd);
      fd = -1;
      continue;
    }
    break;
  }
  freeaddrinfo(servinfo);
  if (-1 == fd) {
    return -8;
  }
  return fd;
}


x86 assembly (clang, -O0):
<peer_call>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e70(%rip),%rcx
or     (%rcx),%eax
mov    0x2e87(%rip),%rcx
or     (%rcx),%eax
mov    0x2e7e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe56(%rip),%rdi
add    $0x1,%rdi
lea    0xe70(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11dc <peer_call+0x8c>
movl   $0xfffffffb,-0x4(%rbp)
jmp    12a0 <peer_call+0x150>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
je     1277 <peer_call+0x127>
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%edx
call   1070 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1228 <peer_call+0xd8>
jmp    1266 <peer_call+0x116>
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edx
call   1040 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1261 <peer_call+0x111>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    1266 <peer_call+0x116>
jmp    1277 <peer_call+0x127>
mov    -0x38(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11eb <peer_call+0x9b>
mov    -0x30(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    129a <peer_call+0x14a>
movl   $0xfffffff8,-0x4(%rbp)
jmp    12a0 <peer_call+0x150>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peer_call>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e51(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e87(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0xe5c(%rip),%rdi
lea    0xe79(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1050 <getaddrinfo@plt>
mov    $0xfffffffb,%r14d
test   %rax,%rax
jne    122d <peer_call+0xdd>
mov    0x8(%rsp),%rbx
mov    $0xfffffff8,%r14d
test   %rbx,%rbx
jne    11e9 <peer_call+0x99>
xor    %edi,%edi
jmp    1228 <peer_call+0xd8>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1219 <peer_call+0xc9>
mov    0x14(%rbx),%edi
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%esi
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11e0 <peer_call+0x90>
mov    %eax,%ebp
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %eax,%edi
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <peer_call+0xd0>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11e0 <peer_call+0x90>
mov    0x8(%rsp),%rdi
jmp    1228 <peer_call+0xd8>
mov    0x8(%rsp),%rdi
mov    %ebp,%r14d
call   1060 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32, %struct.addrinfo* }
@AI_ADDRCONFIG = global i32 0
@AI_NUMERICHOST = global i32 0
@AI_NUMERICSERV = global i32 0
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [37 x i8] c"l0.0.0.0\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00a\00"
@.str.1 = [36 x i8] c"31337\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00p\00"
define i32 @peer_call() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo*
%6 = load i32, i32* @AI_ADDRCONFIG
%7 = load i32, i32* @AI_NUMERICHOST
%8 = or i32 %6, %7
%9 = load i32, i32* @AI_NUMERICSERV
%10 = or i32 %8, %9
%11 = load i32, i32* @AI_NUMERICSERV
%12 = or i32 %10, %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @PF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 5
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @IPPROTO_TCP
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 3
store i32 %18, i32* %19
%20 = call i64 @getaddrinfo(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 1), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%21 = icmp ne i64 0, %20
br i1 %21, label %22, label %23
22: ; preds = %0
store i32 -5, i32* %1
br label %67
23: ; preds = %0
store i32 -1, i32* %2
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %24, %struct.addrinfo** %5
br label %25
25: ; preds = %55, %23
%26 = load %struct.addrinfo*, %struct.addrinfo** %5
%27 = icmp ne %struct.addrinfo* null, %26
br i1 %27, label %28, label %59
28: ; preds = %25
%29 = load %struct.addrinfo*, %struct.addrinfo** %5
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 5
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %5
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %5
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %31, i32 %34, i32 %37)
store i32 %38, i32* %2
%39 = icmp eq i32 -1, %38
br i1 %39, label %40, label %41
40: ; preds = %28
br label %55
41: ; preds = %28
%42 = load i32, i32* %2
%43 = load %struct.addrinfo*, %struct.addrinfo** %5
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %5
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = call i32 @connect(i32 %42, i32 %45, i32 %48)
%50 = icmp eq i32 -1, %49
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = load i32, i32* %2
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %2
br label %55
54: ; preds = %41
br label %59
55: ; preds = %51, %40
%56 = load %struct.addrinfo*, %struct.addrinfo** %5
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 6
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
store %struct.addrinfo* %58, %struct.addrinfo** %5
br label %25
59: ; preds = %54, %25
%60 = load %struct.addrinfo*, %struct.addrinfo** %4
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
%62 = load i32, i32* %2
%63 = icmp eq i32 -1, %62
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 -8, i32* %1
br label %67
65: ; preds = %59
%66 = load i32, i32* %2
store i32 %66, i32* %1
br label %67
67: ; preds = %65, %64, %22
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32, %struct.addrinfo* }
@AI_ADDRCONFIG = global i32 0
@AI_NUMERICHOST = global i32 0
@AI_NUMERICSERV = global i32 0
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [37 x i8] c"l0.0.0.0\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00a\00"
@.str.1 = [36 x i8] c"31337\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00p\00"
define i32 @peer_call() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = load i32, i32* @AI_ADDRCONFIG
%6 = load i32, i32* @AI_NUMERICHOST
%7 = or i32 %6, %5
%8 = load i32, i32* @AI_NUMERICSERV
%9 = or i32 %7, %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @PF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 5
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @IPPROTO_TCP
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %15, i32* %16
%17 = call i64 @getaddrinfo(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 1), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %53
19: ; preds = %0
%20 = load %struct.addrinfo*, %struct.addrinfo** %2
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %49, label %22
22: ; preds = %19, %41
%23 = phi %struct.addrinfo* [ %43, %41 ], [ %20, %19 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %25, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %41, label %32
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @connect(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = call i32 @close(i32 %30)
br label %41
41: ; preds = %22, %39
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 6
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
%44 = icmp eq %struct.addrinfo* %43, null
br i1 %44, label %47, label %22
45: ; preds = %32
%46 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %49
47: ; preds = %41
%48 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %49
49: ; preds = %19, %47, %45
%50 = phi %struct.addrinfo* [ %46, %45 ], [ %48, %47 ], [ null, %19 ]
%51 = phi i32 [ %30, %45 ], [ -8, %47 ], [ -8, %19 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
br label %53
53: ; preds = %49, %0
%54 = phi i32 [ -5, %0 ], [ %51, %49 ]
ret i32 %54
}