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

/* Variables and functions */
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
char *getenv(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int openSocket(void) {
  int sock, err;
  struct addrinfo hints, *res, *rp;
  const char *addrStr, *portStr;
  portStr = getenv("PIGPIO_PORT");
  if (!portStr)
    portStr = "8888";
  addrStr = getenv("PIGPIO_ADDR");
  if (!addrStr)
    addrStr = "127.0.0.1";
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags |= AI_CANONNAME;
  err = getaddrinfo(addrStr, portStr, &hints, &res);
  if (err)
    return -1;
  for (rp = res; rp != NULL; rp = rp->ai_next) {
    sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sock == -1)
      continue;
    if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
  }
  freeaddrinfo(res);
  if (rp == NULL)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<openSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    118e <openSocket+0x2e>
lea    0xe82(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe7c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    11b4 <openSocket+0x54>
lea    0xe6d(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1219 <openSocket+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <openSocket+0x157>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1291 <openSocket+0x131>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1258 <openSocket+0xf8>
jmp    1280 <openSocket+0x120>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     127b <openSocket+0x11b>
jmp    1291 <openSocket+0x131>
jmp    1280 <openSocket+0x120>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1221 <openSocket+0xc1>
mov    -0x40(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
cmpq   $0x0,-0x48(%rbp)
jne    12b1 <openSocket+0x151>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <openSocket+0x157>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0xe8f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe8c(%rip),%r14
cmovne %rax,%r14
lea    0xe86(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe83(%rip),%rbx
cmovne %rax,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
or     %eax,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1250 <openSocket+0xf0>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1219 <openSocket+0xb9>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    124b <openSocket+0xeb>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1241 <openSocket+0xe1>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <openSocket+0xb0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     1210 <openSocket+0xb0>
jmp    1246 <openSocket+0xe6>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@.str = [12 x i8] c"PIGPIO_PORT\00"
@.str.1 = [5 x i8] c"8888\00"
@.str.2 = [12 x i8] c"PIGPIO_ADDR\00"
@.str.3 = [10 x i8] c"127.0.0.1\00"
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
define i32 @openSocket() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %5
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %8
%11 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 40)
%12 = load i32, i32* @PF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @AI_CANONNAME
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 6
%18 = load i32, i32* %17
%19 = or i32 %18, %16
store i32 %19, i32* %17
%20 = call i32 @getaddrinfo(i8* %10, i8* %7, %struct.addrinfo* %1, %struct.addrinfo** %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %53
22: ; preds = %0
%23 = load %struct.addrinfo*, %struct.addrinfo** %2
%24 = icmp eq %struct.addrinfo* %23, null
br i1 %24, label %49, label %25
25: ; preds = %22, %42
%26 = phi %struct.addrinfo* [ %44, %42 ], [ %23, %22 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %42, label %35
35: ; preds = %25
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @connect(i32 %33, i32 %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %35, %25
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %25
46: ; preds = %35, %42
%47 = phi i32 [ -1, %42 ], [ %33, %35 ]
%48 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %49
49: ; preds = %46, %22
%50 = phi %struct.addrinfo* [ null, %22 ], [ %48, %46 ]
%51 = phi i32 [ -1, %22 ], [ %47, %46 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
br label %53
53: ; preds = %49, %0
%54 = phi i32 [ -1, %0 ], [ %51, %49 ]
ret i32 %54
}