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

/* Variables and functions */
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ dbgprintf(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ rs_strerror_r(scalar_t__, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int CreateSocket(struct addrinfo *addrDest) {
  int fd;
  struct addrinfo *r;
  r = addrDest;
  while (r != NULL) {
    fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
    if (fd != -1) {
      if (connect(fd, r->ai_addr, r->ai_addrlen) != 0) {
        if (errno == EINPROGRESS) {
          return fd;
        } else {
          char errStr[1024];
          dbgprintf("create tcp connection failed, reason %s",
                    rs_strerror_r(errno, errStr, sizeof(errStr)));
        }
      } else {
        return fd;
      }
      close(fd);
    } else {
      char errStr[1024];
      dbgprintf("couldn't create send socket, reason %s",
                rs_strerror_r(errno, errStr, sizeof(errStr)));
    }
    r = r->ai_next;
  }
  dbgprintf("no working socket could be obtained");
  return -1;
}


x86 assembly (clang, -O0):
<CreateSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1261 <CreateSocket+0x111>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1226 <CreateSocket+0xd6>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     120e <CreateSocket+0xbe>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <CreateSocket+0x8e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <CreateSocket+0x126>
mov    0x2df3(%rip),%rax
mov    (%rax),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <rs_strerror_r@plt>
mov    %eax,%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <dbgprintf@plt>
jmp    1219 <CreateSocket+0xc9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <CreateSocket+0x126>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    1251 <CreateSocket+0x101>
mov    0x2dab(%rip),%rax
mov    (%rax),%rdi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1030 <rs_strerror_r@plt>
mov    %eax,%esi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <dbgprintf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1167 <CreateSocket+0x17>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <dbgprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateSocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
test   %rdi,%rdi
je     120b <CreateSocket+0xbb>
mov    %rdi,%rbx
mov    0x2e64(%rip),%r13
mov    %rsp,%r14
mov    0x2e6a(%rip),%r15
lea    0xe7b(%rip),%r12
jmp    11b9 <CreateSocket+0x69>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%rdi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <rs_strerror_r@plt>
lea    0xe80(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <dbgprintf@plt>
mov    (%rbx),%rbx
test   %rbx,%rbx
je     120b <CreateSocket+0xbb>
mov    0x18(%rbx),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     1190 <CreateSocket+0x40>
mov    %eax,%ebp
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
test   %rax,%rax
je     121e <CreateSocket+0xce>
mov    0x0(%r13),%rdi
cmp    (%r15),%rdi
je     121e <CreateSocket+0xce>
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <rs_strerror_r@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <dbgprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11b1 <CreateSocket+0x61>
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1050 <dbgprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { %struct.addrinfo*, i32, i32, i32, i32, i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [40 x i8] c"create tcp connection failed, reason %s\00"
@.str.1 = [39 x i8] c"couldn't create send socket, reason %s\00"
@.str.2 = [36 x i8] c"no working socket could be obtained\00"
define i32 @CreateSocket(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca %struct.addrinfo*
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
store %struct.addrinfo* %0, %struct.addrinfo** %3
%8 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %8, %struct.addrinfo** %5
br label %9
9: ; preds = %57, %1
%10 = load %struct.addrinfo*, %struct.addrinfo** %5
%11 = icmp ne %struct.addrinfo* %10, null
br i1 %11, label %12, label %61
12: ; preds = %9
%13 = load %struct.addrinfo*, %struct.addrinfo** %5
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 5
%15 = load i32, i32* %14
%16 = load %struct.addrinfo*, %struct.addrinfo** %5
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 4
%18 = load i32, i32* %17
%19 = load %struct.addrinfo*, %struct.addrinfo** %5
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 3
%21 = load i32, i32* %20
%22 = call i32 @socket(i32 %15, i32 %18, i32 %21)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %52
25: ; preds = %12
%26 = load i32, i32* %4
%27 = load %struct.addrinfo*, %struct.addrinfo** %5
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = load %struct.addrinfo*, %struct.addrinfo** %5
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i64 @connect(i32 %26, i32 %29, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %25
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINPROGRESS
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %63
41: ; preds = %35
%42 = load i64, i64* @errno
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = call i32 @rs_strerror_r(i64 %42, i8* %43, i32 1024)
%45 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %41
br label %49
47: ; preds = %25
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %63
49: ; preds = %46
%50 = load i32, i32* %4
%51 = call i32 @close(i32 %50)
br label %57
52: ; preds = %12
%53 = load i64, i64* @errno
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%55 = call i32 @rs_strerror_r(i64 %53, i8* %54, i32 1024)
%56 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %52, %49
%58 = load %struct.addrinfo*, %struct.addrinfo** %5
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 0
%60 = load %struct.addrinfo*, %struct.addrinfo** %59
store %struct.addrinfo* %60, %struct.addrinfo** %5
br label %9
61: ; preds = %9
%62 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %63
63: ; preds = %61, %47, %39
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.addrinfo = type { %struct.addrinfo*, i32, i32, i32, i32, i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [40 x i8] c"create tcp connection failed, reason %s\00"
@.str.1 = [39 x i8] c"couldn't create send socket, reason %s\00"
@.str.2 = [36 x i8] c"no working socket could be obtained\00"
define i32 @CreateSocket(%struct.addrinfo* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = icmp eq %struct.addrinfo* %0, null
br i1 %4, label %41, label %5
5: ; preds = %1
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %8
8: ; preds = %5, %37
%9 = phi %struct.addrinfo* [ %0, %5 ], [ %39, %37 ]
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 5
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 4
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 3
%15 = load i32, i32* %14
%16 = call i32 @socket(i32 %11, i32 %13, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %33, label %18
18: ; preds = %8
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 2
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 1
%22 = load i32, i32* %21
%23 = call i64 @connect(i32 %16, i32 %20, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %43, label %25
25: ; preds = %18
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINPROGRESS
%28 = icmp eq i64 %26, %27
br i1 %28, label %43, label %29
29: ; preds = %25
%30 = call i32 @rs_strerror_r(i64 %26, i8* %6, i32 1024)
%31 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = call i32 @close(i32 %16)
br label %37
33: ; preds = %8
%34 = load i64, i64* @errno
%35 = call i32 @rs_strerror_r(i64 %34, i8* %7, i32 1024)
%36 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %29
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 0
%39 = load %struct.addrinfo*, %struct.addrinfo** %38
%40 = icmp eq %struct.addrinfo* %39, null
br i1 %40, label %41, label %8
41: ; preds = %37, %1
%42 = call i32 (i8*, ...) @dbgprintf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %18, %25, %41
%44 = phi i32 [ -1, %41 ], [ %16, %25 ], [ %16, %18 ]
ret i32 %44
}