#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct addrinfo {
  int /*<<< orphan*/ *ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ *ai_canonname;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int getSockAddrFromDomainName(char *hostname, struct sockaddr_in *server) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int error;
  int serverAddrIsSet = 0;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_protocol = 0;
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  error = getaddrinfo(hostname, "http", &hints, &result);
  if (error != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
    return error;
  } else {
    struct sockaddr_in *h;
    for (rp = result; rp != NULL; rp = rp->ai_next) {
      h = (struct sockaddr_in *)rp->ai_addr;
      if (h->sin_addr.s_addr != 0) {
        server->sin_addr.s_addr = h->sin_addr.s_addr;
        server->sin_family = h->sin_family;
        server->sin_port = htons(80);
        serverAddrIsSet = 1;
        break;
      }
    }
  }
  freeaddrinfo(result);
  return serverAddrIsSet == 1 ? 0 : 1;
}


x86 assembly (clang, -O0):
<getSockAddrFromDomainName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x60(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2a(%rip),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     1221 <getSockAddrFromDomainName+0xc1>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x5c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c1 <getSockAddrFromDomainName+0x161>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12a0 <getSockAddrFromDomainName+0x140>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     128a <getSockAddrFromDomainName+0x12a>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x60(%rbp)
jmp    12a0 <getSockAddrFromDomainName+0x140>
jmp    128f <getSockAddrFromDomainName+0x12f>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1229 <getSockAddrFromDomainName+0xc9>
jmp    12a5 <getSockAddrFromDomainName+0x145>
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x60(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x1,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getSockAddrFromDomainName>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
lea    0xe43(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11f6 <getSockAddrFromDomainName+0x96>
mov    %eax,%ebp
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1040 <gai_strerror@plt>
lea    0xe1b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    124f <getSockAddrFromDomainName+0xef>
mov    0x8(%rsp),%rdi
mov    $0x1,%ebp
test   %rdi,%rdi
je     1227 <getSockAddrFromDomainName+0xc7>
mov    %rdi,%rax
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rcx
mov    0x8(%rcx),%rdx
test   %rdx,%rdx
jne    122b <getSockAddrFromDomainName+0xcb>
mov    0x8(%rax),%rax
test   %rax,%rax
jne    1210 <getSockAddrFromDomainName+0xb0>
jmp    124a <getSockAddrFromDomainName+0xea>
xor    %edi,%edi
jmp    124a <getSockAddrFromDomainName+0xea>
mov    %rdx,0x8(%r14)
mov    0x4(%rcx),%eax
mov    %eax,0x4(%r14)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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