#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_storage {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct sockaddr_storage *ai_next;
  int /*<<< orphan*/ ai_family;
  scalar_t__ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_family;
  scalar_t__ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ ss;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct sockaddr_storage *,
                struct sockaddr_storage **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_storage *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  struct addrinfo hints;
  struct addrinfo *ailist = NULL;
  struct addrinfo *res = NULL;
  int ret = -1;
  int i = 0;
  memset(&hints, 0x0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  hints.ai_family = AF_INET;
  ret = getaddrinfo(argv[1], NULL, &hints, &ailist);
  if (ret) {
    printf("getaddrinfo failed for name %s [%s]\n", argv[1], gai_strerror(ret));
  }
  for (res = ailist; res; res = res->ai_next) {
    struct sockaddr_storage ss;
    if (!res->ai_addr || res->ai_addrlen == 0) {
      continue;
    }
    memset(&ss, '\0', sizeof(ss));
    memcpy(&ss, res->ai_addr, res->ai_addrlen);
    printf("foo\n");
  }
  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)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1206 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x80(%rbp)
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x80(%rbp),%rsi
mov    %rax,%rdx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     127e <main+0x12e>
mov    -0x48(%rbp),%rax
cmpl   $0x0,0x4(%rax)
je     1234 <main+0xe4>
mov    -0x48(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1239 <main+0xe9>
jmp    126d <main+0x11d>
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
lea    -0x78(%rbp),%rdi
call   1060 <memcpy@plt>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    120e <main+0xbe>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11e2 <main+0x82>
mov    0x8(%rbx),%rbx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     123a <main+0xda>
lea    0x38(%rsp),%r15
lea    0xe2d(%rip),%r14
jmp    1209 <main+0xa9>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
je     123a <main+0xda>
cmpl   $0x0,0x4(%rbx)
je     1200 <main+0xa0>
cmpl   $0x0,(%rbx)
je     1200 <main+0xa0>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r15,%rdi
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    1200 <main+0xa0>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i64, i32 }
%struct.sockaddr_storage = type { i32, i32, %struct.sockaddr_storage*, i32, i64, i32 }
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [37 x i8] c"getaddrinfo failed for name %s [%s]\0A\00"
@.str.1 = [5 x i8] c"foo\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_storage
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.addrinfo* null, %struct.addrinfo** %7
store %struct.addrinfo* null, %struct.addrinfo** %8
store i32 -1, i32* %9
store i32 0, i32* %10
%12 = bitcast %struct.addrinfo* %6 to %struct.sockaddr_storage*
%13 = call i32 @memset(%struct.sockaddr_storage* %12, i8 signext 0, i32 40)
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i64 0, i64* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %17, i32* %18
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = bitcast %struct.addrinfo* %6 to %struct.sockaddr_storage*
%23 = bitcast %struct.addrinfo** %7 to %struct.sockaddr_storage**
%24 = call i32 @getaddrinfo(i8* %21, i32* null, %struct.sockaddr_storage* %22, %struct.sockaddr_storage** %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* %9
%32 = call i8* @gai_strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %30, i8* %32)
br label %34
34: ; preds = %27, %2
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %35, %struct.addrinfo** %8
br label %36
36: ; preds = %60, %34
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = icmp ne %struct.addrinfo* %37, null
br i1 %38, label %39, label %64
39: ; preds = %36
%40 = load %struct.addrinfo*, %struct.addrinfo** %8
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load %struct.addrinfo*, %struct.addrinfo** %8
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44, %39
br label %60
50: ; preds = %44
%51 = call i32 @memset(%struct.sockaddr_storage* %11, i8 signext 0, i32 40)
%52 = load %struct.addrinfo*, %struct.addrinfo** %8
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 1
%54 = load i32, i32* %53
%55 = load %struct.addrinfo*, %struct.addrinfo** %8
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @memcpy(%struct.sockaddr_storage* %11, i32 %54, i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %60
60: ; preds = %50, %49
%61 = load %struct.addrinfo*, %struct.addrinfo** %8
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 2
%63 = load %struct.addrinfo*, %struct.addrinfo** %62
store %struct.addrinfo* %63, %struct.addrinfo** %8
br label %36
64: ; preds = %36
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i64, i32 }
%struct.sockaddr_storage = type { i32, i32, %struct.sockaddr_storage*, i32, i64, i32 }
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [37 x i8] c"getaddrinfo failed for name %s [%s]\0A\00"
@str = [4 x i8] c"foo\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.sockaddr_storage
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%8 = bitcast %struct.addrinfo* %3 to %struct.sockaddr_storage*
%9 = call i32 @memset(%struct.sockaddr_storage* %8, i8 signext 0, i32 40)
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i64 0, i64* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %13, i32* %14
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = bitcast %struct.addrinfo** %4 to %struct.sockaddr_storage**
%18 = call i32 @getaddrinfo(i8* %16, i32* null, %struct.sockaddr_storage* %8, %struct.sockaddr_storage** %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %2
%21 = load i8*, i8** %15
%22 = call i8* @gai_strerror(i32 %18)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %22)
br label %24
24: ; preds = %20, %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %48, label %27
27: ; preds = %24
%28 = bitcast %struct.sockaddr_storage* %5 to i8*
br label %29
29: ; preds = %27, %44
%30 = phi %struct.addrinfo* [ %25, %27 ], [ %46, %44 ]
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 1
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %44, label %34
34: ; preds = %29
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 0
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %34
%39 = call i32 @memset(%struct.sockaddr_storage* %5, i8 signext 0, i32 40)
%40 = load i32, i32* %31
%41 = load i32, i32* %35
%42 = call i32 @memcpy(%struct.sockaddr_storage* %5, i32 %40, i32 %41)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %29, %34, %38
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 2
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
%47 = icmp eq %struct.addrinfo* %46, null
br i1 %47, label %48, label %29
48: ; preds = %44, %24
ret i32 0
}