#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 sockaddr_in6 {
  int sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ ipstr;
typedef int /*<<< orphan*/ hint;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ getaddrinfo(char const *, int /*<<< orphan*/ *,
                               struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *);
int main(int argc, const char *argv[]) {
  const char *name = argv[1];
  printf(">>> %s\n", name);
  union {
    struct sockaddr addr;
    struct sockaddr_in addr4;
    struct sockaddr_in6 addr6;
  } ipaddr;
  struct addrinfo hint, *res;
  memset(&hint, 0, sizeof(hint));
  hint.ai_family = AF_UNSPEC;
  hint.ai_socktype = SOCK_STREAM;
  hint.ai_protocol = IPPROTO_TCP;
  for (int i = 0; i < 1000; ++i) {
    getaddrinfo(name, NULL, &hint, &res);
    char ipstr[INET6_ADDRSTRLEN];
    for (struct addrinfo *ai = res; ai; ai = ai->ai_next) {
      if (ai->ai_family == AF_INET) {
        ipaddr.addr4 = *(struct sockaddr_in *)ai->ai_addr;
        ipaddr.addr4.sin_port = 80;
        inet_ntop(AF_INET, &ipaddr.addr4.sin_addr, ipstr, sizeof(ipstr));
      } else if (ai->ai_family == AF_INET6) {
        ipaddr.addr6 = *(struct sockaddr_in6 *)ai->ai_addr;
        ipaddr.addr6.sin6_port = 80;
        inet_ntop(AF_INET6, &ipaddr.addr6.sin6_addr, ipstr, sizeof(ipstr));
      } else {
        continue;
      }
      printf("found: %s\n", ipstr);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe90(%rip),%rdi
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3e8,-0x4c(%rbp)
jge    1302 <main+0x1c2>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     12ed <main+0x1ad>
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <main+0x12a>
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rax
mov    -0x68(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    (%rcx),%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x50,-0x20(%rbp)
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
jmp    12c7 <main+0x187>
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%rax
jne    12bd <main+0x17d>
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rax
mov    -0x68(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    (%rcx),%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x50,-0x20(%rbp)
mov    0x2d22(%rip),%rcx
mov    (%rcx),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
jmp    12c2 <main+0x182>
jmp    12dc <main+0x19c>
jmp    12c7 <main+0x187>
mov    -0x70(%rbp),%rsi
lea    0xd36(%rip),%rdi
call   1040 <printf@plt>
jmp    12ed <main+0x1ad>
mov    -0x68(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    120c <main+0xcc>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11b2 <main+0x72>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x8(%rsi),%rbx
lea    0xea4(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    -0x58(%rbp),%rdi
xor    %r13d,%r13d
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x38(%rbp),%r12
jmp    11f1 <main+0xb1>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rdx),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x50,-0x30(%rbp)
lea    -0x2c(%rbp),%rsi
mov    %r15,%rdx
call   1030 <inet_ntop@plt>
lea    0xe33(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    %r14,%rsp
add    $0x1,%r13d
cmp    $0x3e8,%r13d
je     127a <main+0x13a>
mov    %rbx,%rdi
xor    %esi,%esi
lea    -0x58(%rbp),%rdx
mov    %r12,%rcx
call   1060 <getaddrinfo@plt>
mov    %rsp,%r14
mov    0x2ddc(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%r15
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    -0x38(%rbp),%rdx
test   %rdx,%rdx
je     11dd <main+0x9d>
mov    0x2db1(%rip),%rax
mov    (%rax),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
nopl   0x0(%rax)
mov    (%rdx),%rsi
cmp    %rdi,%rsi
je     11b0 <main+0x70>
cmp    %rax,%rsi
je     1260 <main+0x120>
mov    0x10(%rdx),%rdx
test   %rdx,%rdx
jne    1240 <main+0x100>
jmp    11dd <main+0x9d>
nopl   0x0(%rax)
mov    0x8(%rdx),%rdx
mov    (%rdx),%rdx
mov    %rdx,-0x30(%rbp)
movl   $0x50,-0x30(%rbp)
mov    %rax,%rdi
jmp    11c2 <main+0x82>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { %struct.sockaddr_in }
%struct.sockaddr_in = type { i32, i32 }
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@.str = [8 x i8] c">>> %s\0A\00"
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.1 = [11 x i8] c"found: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %union.anon
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.addrinfo*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%20 = load i64, i64* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i64 %20, i64* %21
%22 = load i32, i32* @SOCK_STREAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %22, i32* %23
%24 = load i32, i32* @IPPROTO_TCP
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %24, i32* %25
store i32 0, i32* %10
br label %26
26: ; preds = %92, %2
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 1000
br i1 %28, label %29, label %95
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = call i32 @getaddrinfo(i8* %30, i32* null, %struct.addrinfo* %8, %struct.addrinfo** %9)
%32 = load i32, i32* @INET6_ADDRSTRLEN
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
store i8* %34, i8** %11
%35 = alloca i8, i64 %33
store i64 %33, i64* %12
%36 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %36, %struct.addrinfo** %13
br label %37
37: ; preds = %86, %29
%38 = load %struct.addrinfo*, %struct.addrinfo** %13
%39 = icmp ne %struct.addrinfo* %38, null
br i1 %39, label %40, label %90
40: ; preds = %37
%41 = load %struct.addrinfo*, %struct.addrinfo** %13
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @AF_INET
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %61
46: ; preds = %40
%47 = bitcast %union.anon* %7 to %struct.sockaddr_in*
%48 = load %struct.addrinfo*, %struct.addrinfo** %13
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i64, i64* %49
%51 = inttoptr i64 %50 to %struct.sockaddr_in*
%52 = bitcast %struct.sockaddr_in* %47 to i8*
%53 = bitcast %struct.sockaddr_in* %51 to i8*
%54 = bitcast %union.anon* %7 to %struct.sockaddr_in*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
store i32 80, i32* %55
%56 = load i64, i64* @AF_INET
%57 = bitcast %union.anon* %7 to %struct.sockaddr_in*
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 1
%59 = trunc i64 %33 to i32
%60 = call i32 @inet_ntop(i64 %56, i32* %58, i8* %35, i32 %59)
br label %84
61: ; preds = %40
%62 = load %struct.addrinfo*, %struct.addrinfo** %13
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = load i64, i64* @AF_INET6
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %82
67: ; preds = %61
%68 = bitcast %union.anon* %7 to %struct.sockaddr_in6*
%69 = load %struct.addrinfo*, %struct.addrinfo** %13
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load i64, i64* %70
%72 = inttoptr i64 %71 to %struct.sockaddr_in6*
%73 = bitcast %struct.sockaddr_in6* %68 to i8*
%74 = bitcast %struct.sockaddr_in6* %72 to i8*
%75 = bitcast %union.anon* %7 to %struct.sockaddr_in6*
%76 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %75, i32 0, i32 0
store i32 80, i32* %76
%77 = load i64, i64* @AF_INET6
%78 = bitcast %union.anon* %7 to %struct.sockaddr_in6*
%79 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %78, i32 0, i32 1
%80 = trunc i64 %33 to i32
%81 = call i32 @inet_ntop(i64 %77, i32* %79, i8* %35, i32 %80)
br label %83
82: ; preds = %61
br label %86
83: ; preds = %67
br label %84
84: ; preds = %83, %46
%85 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %90
86: ; preds = %82
%87 = load %struct.addrinfo*, %struct.addrinfo** %13
%88 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %87, i32 0, i32 2
%89 = load %struct.addrinfo*, %struct.addrinfo** %88
store %struct.addrinfo* %89, %struct.addrinfo** %13
br label %37
90: ; preds = %84, %37
%91 = load i8*, i8** %11
br label %92
92: ; preds = %90
%93 = load i32, i32* %10
%94 = add nsw i32 %93, 1
store i32 %94, i32* %10
br label %26
95: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@.str = [8 x i8] c">>> %s\0A\00"
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.1 = [11 x i8] c"found: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = bitcast i64* %3 to i8*
%10 = bitcast %struct.addrinfo* %4 to i8*
%11 = bitcast %struct.addrinfo** %5 to i8*
%12 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%13 = load i64, i64* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i64 %13, i64* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %15, i32* %16
%17 = load i32, i32* @IPPROTO_TCP
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %17, i32* %18
%19 = bitcast i64* %3 to %struct.sockaddr_in6*
%20 = bitcast i64* %3 to i32*
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %19, i64 0, i32 1
%22 = bitcast i64* %3 to %struct.sockaddr_in*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i64 0, i32 1
br label %25
24: ; preds = %62
ret i32 0
25: ; preds = %2, %62
%26 = phi i32 [ 0, %2 ], [ %63, %62 ]
%27 = call i32 @getaddrinfo(i8* %7, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
%28 = load i32, i32* @INET6_ADDRSTRLEN
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
%31 = alloca i8, i64 %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %5
%33 = icmp eq %struct.addrinfo* %32, null
br i1 %33, label %62, label %34
34: ; preds = %25
%35 = load i64, i64* @AF_INET
%36 = load i64, i64* @AF_INET6
br label %37
37: ; preds = %34, %58
%38 = phi %struct.addrinfo* [ %32, %34 ], [ %60, %58 ]
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 0
%40 = load i64, i64* %39
%41 = icmp eq i64 %40, %35
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%44 = load i64, i64* %43
%45 = inttoptr i64 %44 to i64*
%46 = load i64, i64* %45
store i64 %46, i64* %3
store i32 80, i32* %20
%47 = call i32 @inet_ntop(i64 %35, i32* %23, i8* %31, i32 %28)
br label %56
48: ; preds = %37
%49 = icmp eq i64 %40, %36
br i1 %49, label %50, label %58
50: ; preds = %48
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%52 = load i64, i64* %51
%53 = inttoptr i64 %52 to i64*
%54 = load i64, i64* %53
store i64 %54, i64* %3
store i32 80, i32* %20
%55 = call i32 @inet_ntop(i64 %36, i32* %21, i8* %31, i32 %28)
br label %56
56: ; preds = %50, %42
%57 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %62
58: ; preds = %48
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 2
%60 = load %struct.addrinfo*, %struct.addrinfo** %59
%61 = icmp eq %struct.addrinfo* %60, null
br i1 %61, label %62, label %37
62: ; preds = %58, %25, %56
%63 = add nuw nsw i32 %26, 1
%64 = icmp eq i32 %63, 1000
br i1 %64, label %24, label %25
}