#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
struct addrinfo {
  int ai_flags;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ atoi(char const *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htonl(int);
unsigned short htons(int /*<<< orphan*/);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, struct in_addr *);
struct addrinfo *new_ai(unsigned short, int /*<<< orphan*/);
int getaddrinfo(const char *node, const char *service,
                const struct addrinfo *hints, struct addrinfo **res) {
  struct addrinfo *cur, *prev = NULL;
  struct hostent *he;
  struct in_addr ip;
  unsigned short portno;
  int i;
  if (service)
    portno = htons(atoi(service));
  else
    portno = 0;
  if (hints && hints->ai_flags & 1) {
    *res = new_ai(portno, htonl(0x00000000));
    return 0;
  }
  if (!node) {
    *res = new_ai(portno, htonl(0x7f000001));
    return 0;
  }
  if (inet_pton(AF_INET, node, &ip)) {
    *res = new_ai(portno, ip.s_addr);
    return 0;
  }
  he = gethostbyname(node);
  if (he && he->h_addr_list[0]) {
    for (i = 0; he->h_addr_list[i]; i++) {
      cur = new_ai(portno, ((struct in_addr *)he->h_addr_list[i])->s_addr);
      if (prev)
        prev->ai_next = cur;
      else
        *res = cur;
      prev = cur;
    }
    return 0;
  }
  return 7;
}


x86 assembly (clang, -O0):
<getaddrinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a4 <getaddrinfo+0x44>
mov    -0x18(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %ax,-0x4a(%rbp)
jmp    11aa <getaddrinfo+0x4a>
movw   $0x0,-0x4a(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fa <getaddrinfo+0x9a>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11fa <getaddrinfo+0x9a>
mov    -0x4a(%rbp),%ax
mov    %ax,-0x52(%rbp)
xor    %edi,%edi
call   1040 <htonl@plt>
mov    %eax,%esi
mov    -0x52(%rbp),%ax
movzwl %ax,%edi
call   1070 <new_ai@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1337 <getaddrinfo+0x1d7>
cmpq   $0x0,-0x10(%rbp)
jne    123b <getaddrinfo+0xdb>
mov    -0x4a(%rbp),%ax
mov    %ax,-0x54(%rbp)
mov    $0x7f000001,%edi
call   1040 <htonl@plt>
mov    %eax,%esi
mov    -0x54(%rbp),%ax
movzwl %ax,%edi
call   1070 <new_ai@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1337 <getaddrinfo+0x1d7>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x0,%rax
je     1280 <getaddrinfo+0x120>
mov    -0x4a(%rbp),%ax
mov    -0x48(%rbp),%esi
movzwl %ax,%edi
call   1070 <new_ai@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1337 <getaddrinfo+0x1d7>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1330 <getaddrinfo+0x1d0>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     1330 <getaddrinfo+0x1d0>
movl   $0x0,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movslq -0x50(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1324 <getaddrinfo+0x1c4>
mov    -0x4a(%rbp),%ax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rcx
movslq -0x50(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
mov    (%rcx),%esi
movzwl %ax,%edi
call   1070 <new_ai@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1303 <getaddrinfo+0x1a3>
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    130e <getaddrinfo+0x1ae>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    12b0 <getaddrinfo+0x150>
movl   $0x0,-0x4(%rbp)
jmp    1337 <getaddrinfo+0x1d7>
movl   $0x7,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaddrinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rdi,%rbp
test   %rsi,%rsi
je     1192 <getaddrinfo+0x32>
mov    %rsi,%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,%r14d
test   %rbx,%rbx
jne    119a <getaddrinfo+0x3a>
jmp    119f <getaddrinfo+0x3f>
xor    %r14d,%r14d
test   %rbx,%rbx
je     119f <getaddrinfo+0x3f>
testb  $0x1,(%rbx)
jne    11c6 <getaddrinfo+0x66>
test   %rbp,%rbp
je     11ca <getaddrinfo+0x6a>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rdx
mov    %rbp,%rsi
call   1060 <inet_pton@plt>
test   %rax,%rax
je     11f7 <getaddrinfo+0x97>
mov    (%rsp),%esi
movzwl %r14w,%edi
jmp    11da <getaddrinfo+0x7a>
xor    %edi,%edi
jmp    11cf <getaddrinfo+0x6f>
mov    $0x7f000001,%edi
call   1040 <htonl@plt>
movzwl %r14w,%edi
mov    %eax,%esi
call   1070 <new_ai@plt>
mov    %rax,(%r15)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
mov    $0x7,%r12d
test   %rax,%rax
je     11e5 <getaddrinfo+0x85>
mov    %rax,%r13
mov    (%rax),%rax
mov    (%rax),%rcx
test   %rcx,%rcx
je     11e5 <getaddrinfo+0x85>
mov    $0x8,%ebx
xor    %r12d,%r12d
movzwl %r14w,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%rcx),%esi
mov    %r14d,%edi
call   1070 <new_ai@plt>
lea    0x8(%rbp),%rcx
test   %rbp,%rbp
cmove  %r15,%rcx
mov    %rax,(%rcx)
mov    0x0(%r13),%rcx
mov    (%rcx,%rbx,1),%rcx
add    $0x8,%rbx
mov    %rax,%rbp
test   %rcx,%rcx
jne    1230 <getaddrinfo+0xd0>
jmp    11e5 <getaddrinfo+0x85>

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.addrinfo* }
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
define i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %2, %struct.addrinfo** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo**
%10 = alloca %struct.addrinfo*
%11 = alloca %struct.addrinfo*
%12 = alloca %struct.hostent*
%13 = alloca %struct.in_addr
%14 = alloca i16
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store %struct.addrinfo* %2, %struct.addrinfo** %8
store %struct.addrinfo** %3, %struct.addrinfo*** %9
store %struct.addrinfo* null, %struct.addrinfo** %11
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = call i32 @atoi(i8* %19)
%21 = call zeroext i16 @htons(i32 %20)
store i16 %21, i16* %14
br label %23
22: ; preds = %4
store i16 0, i16* %14
br label %23
23: ; preds = %22, %18
%24 = load %struct.addrinfo*, %struct.addrinfo** %8
%25 = icmp ne %struct.addrinfo* %24, null
br i1 %25, label %26, label %37
26: ; preds = %23
%27 = load %struct.addrinfo*, %struct.addrinfo** %8
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = and i32 %29, 1
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i16, i16* %14
%34 = call i32 @htonl(i32 0)
%35 = call %struct.addrinfo* @new_ai(i16 zeroext %33, i32 %34)
%36 = load %struct.addrinfo**, %struct.addrinfo*** %9
store %struct.addrinfo* %35, %struct.addrinfo** %36
store i32 0, i32* %5
br label %107
37: ; preds = %26, %23
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %45, label %40
40: ; preds = %37
%41 = load i16, i16* %14
%42 = call i32 @htonl(i32 2130706433)
%43 = call %struct.addrinfo* @new_ai(i16 zeroext %41, i32 %42)
%44 = load %struct.addrinfo**, %struct.addrinfo*** %9
store %struct.addrinfo* %43, %struct.addrinfo** %44
store i32 0, i32* %5
br label %107
45: ; preds = %37
%46 = load i32, i32* @AF_INET
%47 = load i8*, i8** %6
%48 = call i64 @inet_pton(i32 %46, i8* %47, %struct.in_addr* %13)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = load i16, i16* %14
%52 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call %struct.addrinfo* @new_ai(i16 zeroext %51, i32 %53)
%55 = load %struct.addrinfo**, %struct.addrinfo*** %9
store %struct.addrinfo* %54, %struct.addrinfo** %55
store i32 0, i32* %5
br label %107
56: ; preds = %45
%57 = load i8*, i8** %6
%58 = call %struct.hostent* @gethostbyname(i8* %57)
store %struct.hostent* %58, %struct.hostent** %12
%59 = load %struct.hostent*, %struct.hostent** %12
%60 = icmp ne %struct.hostent* %59, null
br i1 %60, label %61, label %106
61: ; preds = %56
%62 = load %struct.hostent*, %struct.hostent** %12
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i32 0, i32 0
%64 = load i64*, i64** %63
%65 = getelementptr inbounds i64, i64* %64, i64 0
%66 = load i64, i64* %65
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %106
68: ; preds = %61
store i32 0, i32* %15
br label %69
69: ; preds = %102, %68
%70 = load %struct.hostent*, %struct.hostent** %12
%71 = getelementptr inbounds %struct.hostent, %struct.hostent* %70, i32 0, i32 0
%72 = load i64*, i64** %71
%73 = load i32, i32* %15
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i64, i64* %72, i64 %74
%76 = load i64, i64* %75
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %105
78: ; preds = %69
%79 = load i16, i16* %14
%80 = load %struct.hostent*, %struct.hostent** %12
%81 = getelementptr inbounds %struct.hostent, %struct.hostent* %80, i32 0, i32 0
%82 = load i64*, i64** %81
%83 = load i32, i32* %15
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i64, i64* %82, i64 %84
%86 = load i64, i64* %85
%87 = inttoptr i64 %86 to %struct.in_addr*
%88 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %87, i32 0, i32 0
%89 = load i32, i32* %88
%90 = call %struct.addrinfo* @new_ai(i16 zeroext %79, i32 %89)
store %struct.addrinfo* %90, %struct.addrinfo** %10
%91 = load %struct.addrinfo*, %struct.addrinfo** %11
%92 = icmp ne %struct.addrinfo* %91, null
br i1 %92, label %93, label %97
93: ; preds = %78
%94 = load %struct.addrinfo*, %struct.addrinfo** %10
%95 = load %struct.addrinfo*, %struct.addrinfo** %11
%96 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %95, i32 0, i32 1
store %struct.addrinfo* %94, %struct.addrinfo** %96
br label %100
97: ; preds = %78
%98 = load %struct.addrinfo*, %struct.addrinfo** %10
%99 = load %struct.addrinfo**, %struct.addrinfo*** %9
store %struct.addrinfo* %98, %struct.addrinfo** %99
br label %100
100: ; preds = %97, %93
%101 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %101, %struct.addrinfo** %11
br label %102
102: ; preds = %100
%103 = load i32, i32* %15
%104 = add nsw i32 %103, 1
store i32 %104, i32* %15
br label %69
105: ; preds = %69
store i32 0, i32* %5
br label %107
106: ; preds = %61, %56
store i32 7, i32* %5
br label %107
107: ; preds = %106, %105, %50, %40, %32
%108 = load i32, i32* %5
ret i32 %108
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo* }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
@AF_INET = global i32 0
define i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %2, %struct.addrinfo** nocapture writeonly %3) {
%5 = alloca %struct.in_addr
%6 = bitcast %struct.in_addr* %5 to i8*
%7 = icmp eq i8* %1, null
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i32 @atoi(i8* %1)
%10 = tail call zeroext i16 @htons(i32 %9)
br label %11
11: ; preds = %4, %8
%12 = phi i16 [ %10, %8 ], [ 0, %4 ]
%13 = icmp eq %struct.addrinfo* %2, null
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, 1
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @htonl(i32 0)
%21 = tail call %struct.addrinfo* @new_ai(i16 zeroext %12, i32 %20)
br label %59
22: ; preds = %14, %11
%23 = icmp eq i8* %0, null
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = tail call i32 @htonl(i32 2130706433)
%26 = tail call %struct.addrinfo* @new_ai(i16 zeroext %12, i32 %25)
br label %59
27: ; preds = %22
%28 = load i32, i32* @AF_INET
%29 = call i64 @inet_pton(i32 %28, i8* %0, %struct.in_addr* %5)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call %struct.addrinfo* @new_ai(i16 zeroext %12, i32 %33)
br label %59
35: ; preds = %27
%36 = call %struct.hostent* @gethostbyname(i8* %0)
%37 = icmp eq %struct.hostent* %36, null
br i1 %37, label %61, label %38
38: ; preds = %35
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i64 0, i32 0
%40 = load i64*, i64** %39
%41 = load i64, i64* %40
%42 = icmp eq i64 %41, 0
br i1 %42, label %61, label %43
43: ; preds = %38, %43
%44 = phi i64 [ %54, %43 ], [ 0, %38 ]
%45 = phi i64 [ %57, %43 ], [ %41, %38 ]
%46 = phi %struct.addrinfo* [ %50, %43 ], [ null, %38 ]
%47 = inttoptr i64 %45 to %struct.in_addr*
%48 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %47, i64 0, i32 0
%49 = load i32, i32* %48
%50 = call %struct.addrinfo* @new_ai(i16 zeroext %12, i32 %49)
%51 = icmp eq %struct.addrinfo* %46, null
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i64 0, i32 1
%53 = select i1 %51, %struct.addrinfo** %3, %struct.addrinfo** %52
store %struct.addrinfo* %50, %struct.addrinfo** %53
%54 = add nuw i64 %44, 1
%55 = load i64*, i64** %39
%56 = getelementptr inbounds i64, i64* %55, i64 %54
%57 = load i64, i64* %56
%58 = icmp eq i64 %57, 0
br i1 %58, label %61, label %43
59: ; preds = %19, %24, %31
%60 = phi %struct.addrinfo* [ %34, %31 ], [ %26, %24 ], [ %21, %19 ]
store %struct.addrinfo* %60, %struct.addrinfo** %3
br label %61
61: ; preds = %43, %59, %35, %38
%62 = phi i32 [ 7, %38 ], [ 7, %35 ], [ 0, %59 ], [ 0, %43 ]
ret i32 %62
}