#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 EAI_SYSTEM;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo const *,
                struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int sock_getaddrinfo(const char *hostname, const char *service) {
  struct addrinfo *result, *iter;
  const struct addrinfo hints = {.ai_family = AF_UNSPEC,
                                 .ai_socktype = SOCK_STREAM,
                                 .ai_flags = AI_PASSIVE};
  int error = getaddrinfo(hostname, service, &hints, &result);
  if (error != 0) {
    if (error == EAI_SYSTEM) {
      perror("getaddrinfo");
    } else {
      fprintf(stderr, "error in getaddrinfo: %s\r\n", gai_strerror(error));
    }
    exit(EXIT_FAILURE);
  }
  int sock = -1;
  for (iter = result; iter != NULL; iter = iter->ai_next) {
    sock = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);
    if (sock < 0)
      continue;
    if (connect(sock, iter->ai_addr, iter->ai_addrlen) == -1) {
      close(sock);
      continue;
    }
    break;
  }
  freeaddrinfo(result);
  return iter == NULL ? -1 : sock;
}


x86 assembly (clang, -O0):
<sock_getaddrinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     125a <sock_getaddrinfo+0xca>
mov    -0x4c(%rbp),%eax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
jne    1226 <sock_getaddrinfo+0x96>
lea    0xde4(%rip),%rdi
call   1060 <perror@plt>
jmp    124c <sock_getaddrinfo+0xbc>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xdc5(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12e1 <sock_getaddrinfo+0x151>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    12a0 <sock_getaddrinfo+0x110>
jmp    12d0 <sock_getaddrinfo+0x140>
mov    -0x50(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12cb <sock_getaddrinfo+0x13b>
mov    -0x50(%rbp),%edi
call   1040 <close@plt>
jmp    12d0 <sock_getaddrinfo+0x140>
jmp    12e1 <sock_getaddrinfo+0x151>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1269 <sock_getaddrinfo+0xd9>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1302 <sock_getaddrinfo+0x172>
mov    $0xffffffff,%eax
mov    %eax,-0x58(%rbp)
jmp    1308 <sock_getaddrinfo+0x178>
mov    -0x50(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_getaddrinfo>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x0,0x18(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    124f <sock_getaddrinfo+0xbf>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1209 <sock_getaddrinfo+0x79>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1241 <sock_getaddrinfo+0xb1>
nopl   (%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1237 <sock_getaddrinfo+0xa7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     1200 <sock_getaddrinfo+0x70>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    123c <sock_getaddrinfo+0xac>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1200 <sock_getaddrinfo+0x70>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%eax
jne    1274 <sock_getaddrinfo+0xe4>
lea    0xd9f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd81(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
jmp    1266 <sock_getaddrinfo+0xd6>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@EAI_SYSTEM = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"error in getaddrinfo: %s\0D\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @sock_getaddrinfo(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 0
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
%14 = load i32, i32* @SOCK_STREAM
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
%16 = load i32, i32* @AF_UNSPEC
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 6
%19 = load i32, i32* @AI_PASSIVE
store i32 %19, i32* %18
%20 = load i8*, i8** %3
%21 = load i8*, i8** %4
%22 = call i32 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %7, %struct.addrinfo** %5)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %2
%26 = load i32, i32* %8
%27 = load i32, i32* @EAI_SYSTEM
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %36
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = load i32, i32* %8
%34 = call i8* @gai_strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %31, %29
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %2
store i32 -1, i32* %9
%40 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %40, %struct.addrinfo** %6
br label %41
41: ; preds = %72, %39
%42 = load %struct.addrinfo*, %struct.addrinfo** %6
%43 = icmp ne %struct.addrinfo* %42, null
br i1 %43, label %44, label %76
44: ; preds = %41
%45 = load %struct.addrinfo*, %struct.addrinfo** %6
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 4
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %6
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 3
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %6
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = call i32 @socket(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %44
br label %72
58: ; preds = %44
%59 = load i32, i32* %9
%60 = load %struct.addrinfo*, %struct.addrinfo** %6
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = load %struct.addrinfo*, %struct.addrinfo** %6
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @connect(i32 %59, i32 %62, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %58
%69 = load i32, i32* %9
%70 = call i32 @close(i32 %69)
br label %72
71: ; preds = %58
br label %76
72: ; preds = %68, %57
%73 = load %struct.addrinfo*, %struct.addrinfo** %6
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 5
%75 = load %struct.addrinfo*, %struct.addrinfo** %74
store %struct.addrinfo* %75, %struct.addrinfo** %6
br label %41
76: ; preds = %71, %41
%77 = load %struct.addrinfo*, %struct.addrinfo** %5
%78 = call i32 @freeaddrinfo(%struct.addrinfo* %77)
%79 = load %struct.addrinfo*, %struct.addrinfo** %6
%80 = icmp eq %struct.addrinfo* %79, null
br i1 %80, label %81, label %82
81: ; preds = %76
br label %84
82: ; preds = %76
%83 = load i32, i32* %9
br label %84
84: ; preds = %82, %81
%85 = phi i32 [ -1, %81 ], [ %83, %82 ]
ret i32 %85
}

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