#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 */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char const *, struct addrinfo *, struct addrinfo **);
char *getenv(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
uint32_t checkAddr(char *addrStr) {
  int err;
  struct addrinfo hints, *res;
  struct sockaddr_in *sin;
  const char *portStr;
  uint32_t addr;
  portStr = getenv("PIGPIO_PORT");
  if (!portStr)
    portStr = "8888";
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags |= AI_CANONNAME;
  err = getaddrinfo(addrStr, portStr, &hints, &res);
  if (err)
    return 0;
  sin = (struct sockaddr_in *)res->ai_addr;
  addr = sin->sin_addr.s_addr;
  freeaddrinfo(res);
  return addr;
}


x86 assembly (clang, -O0):
<checkAddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1172 <checkAddr+0x32>
lea    0xe9e(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d7 <checkAddr+0x97>
movl   $0x0,-0x4(%rbp)
jmp    11fa <checkAddr+0xba>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkAddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0xeac(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xea9(%rip),%rbx
cmovne %rax,%rbx
lea    0x10(%rsp),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
or     %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
test   %eax,%eax
jne    11ca <checkAddr+0x8a>
mov    0x8(%rsp),%rdi
mov    (%rdi),%rax
mov    (%rax),%ebp
call   1060 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [12 x i8] c"PIGPIO_PORT\00"
@.str.1 = [5 x i8] c"8888\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
define i32 @checkAddr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.sockaddr_in*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
%10 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %14
14: ; preds = %13, %1
%15 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 24)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @AI_CANONNAME
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 1
%22 = load i32, i32* %21
%23 = or i32 %22, %20
store i32 %23, i32* %21
%24 = load i8*, i8** %3
%25 = load i8*, i8** %8
%26 = call i32 @getaddrinfo(i8* %24, i8* %25, %struct.addrinfo* %5, %struct.addrinfo** %6)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %14
store i32 0, i32* %2
br label %42
30: ; preds = %14
%31 = load %struct.addrinfo*, %struct.addrinfo** %6
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to %struct.sockaddr_in*
store %struct.sockaddr_in* %34, %struct.sockaddr_in** %7
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %9
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
%41 = load i32, i32* %9
store i32 %41, i32* %2
br label %42
42: ; preds = %30, %29
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [12 x i8] c"PIGPIO_PORT\00"
@.str.1 = [5 x i8] c"8888\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
define i32 @checkAddr(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %6
%9 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @AI_CANONNAME
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = or i32 %16, %14
store i32 %17, i32* %15
%18 = call i32 @getaddrinfo(i8* %0, i8* %8, %struct.addrinfo* %2, %struct.addrinfo** %3)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %1
%21 = load %struct.addrinfo*, %struct.addrinfo** %3
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to %struct.sockaddr_in*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i64 0, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @freeaddrinfo(%struct.addrinfo* %21)
br label %28
28: ; preds = %1, %20
%29 = phi i32 [ %26, %20 ], [ 0, %1 ]
ret i32 %29
}