#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;
};

/* Variables and functions */
scalar_t__ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
bool checksock(int *sockfd, struct addrinfo *results) {
  struct addrinfo *addr;
  for (addr = results; addr != NULL; addr = addr->ai_next) {
    if ((*sockfd = socket(addr->ai_family, addr->ai_socktype,
                          addr->ai_protocol)) < 0) {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      continue;
    }
    if (connect(*sockfd, addr->ai_addr, addr->ai_addrlen) != -1)
      break;
    else
      fprintf(stderr, "Error: %s\n", strerror(errno));
    if (*sockfd)
      if (close(*sockfd) < 0) {
        fprintf(stderr, "Error: %s\n", strerror(errno));
        return true;
      }
  }
  if (addr == NULL) {
    fprintf(stderr, "Error: could not find a working address.\n");
    return true;
  } else
    return false;
}


x86 assembly (clang, -O0):
<checksock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     129a <checksock+0x14a>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <socket@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    11cf <checksock+0x7f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1289 <checksock+0x139>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     11f5 <checksock+0xa5>
jmp    129a <checksock+0x14a>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     1284 <checksock+0x134>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    127f <checksock+0x12f>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cf <checksock+0x17f>
jmp    1284 <checksock+0x134>
jmp    1289 <checksock+0x139>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1168 <checksock+0x18>
cmpq   $0x0,-0x20(%rbp)
jne    12c8 <checksock+0x178>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cf <checksock+0x17f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     122a <checksock+0xda>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e6f(%rip),%r12
mov    0x2e60(%rip),%r13
lea    0xe81(%rip),%r15
jmp    11b5 <checksock+0x65>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     122a <checksock+0xda>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1070 <socket@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     1190 <checksock+0x40>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1248 <checksock+0xf8>
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%edi
test   %edi,%edi
je     11ac <checksock+0x5c>
call   1030 <close@plt>
test   %rax,%rax
jns    11ac <checksock+0x5c>
mov    (%r12),%ebx
mov    0x0(%r13),%edi
call   1060 <strerror@plt>
lea    0xde4(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1241 <checksock+0xf1>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
jmp    124a <checksock+0xfa>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@stderr = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"Error: could not find a working address.\0A\00"
define i32 @checksock(i32* %0, %struct.addrinfo* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
store i32* %0, i32** %4
store %struct.addrinfo* %1, %struct.addrinfo** %5
%7 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %7, %struct.addrinfo** %6
br label %8
8: ; preds = %62, %2
%9 = load %struct.addrinfo*, %struct.addrinfo** %6
%10 = icmp ne %struct.addrinfo* %9, null
br i1 %10, label %11, label %66
11: ; preds = %8
%12 = load %struct.addrinfo*, %struct.addrinfo** %6
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 4
%14 = load i32, i32* %13
%15 = load %struct.addrinfo*, %struct.addrinfo** %6
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 3
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %6
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %14, i32 %17, i32 %20)
%22 = load i32*, i32** %4
store i32 %21, i32* %22
%23 = icmp slt i32 %21, 0
br i1 %23, label %24, label %29
24: ; preds = %11
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %62
29: ; preds = %11
%30 = load i32*, i32** %4
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %6
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @connect(i32 %31, i32 %34, i32 %37)
%39 = icmp ne i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %29
br label %66
41: ; preds = %29
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %41
%47 = load i32*, i32** %4
%48 = load i32, i32* %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %46
%51 = load i32*, i32** %4
%52 = load i32, i32* %51
%53 = call i64 @close(i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %58)
store i32 1, i32* %3
br label %73
60: ; preds = %50
br label %61
61: ; preds = %60, %46
br label %62
62: ; preds = %61, %24
%63 = load %struct.addrinfo*, %struct.addrinfo** %6
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 5
%65 = load %struct.addrinfo*, %struct.addrinfo** %64
store %struct.addrinfo* %65, %struct.addrinfo** %6
br label %8
66: ; preds = %40, %8
%67 = load %struct.addrinfo*, %struct.addrinfo** %6
%68 = icmp eq %struct.addrinfo* %67, null
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %73
72: ; preds = %66
store i32 0, i32* %3
br label %73
73: ; preds = %72, %69, %55
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@stderr = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"Error: could not find a working address.\0A\00"
define i32 @checksock(i32* nocapture %0, %struct.addrinfo* %1) {
%3 = icmp eq %struct.addrinfo* %1, null
br i1 %3, label %45, label %4
4: ; preds = %2, %41
%5 = phi %struct.addrinfo* [ %43, %41 ], [ %1, %2 ]
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
%11 = load i32, i32* %10
%12 = tail call i32 @socket(i32 %7, i32 %9, i32 %11)
store i32 %12, i32* %0
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %41
19: ; preds = %4
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i32 @connect(i32 %12, i32 %21, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %48
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %0
%32 = icmp eq i32 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %26
%34 = tail call i64 @close(i32 %31)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = tail call i8* @strerror(i32 %38)
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %39)
br label %48
41: ; preds = %26, %33, %14
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 5
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
%44 = icmp eq %struct.addrinfo* %43, null
br i1 %44, label %45, label %4
45: ; preds = %41, %2
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %19, %45, %36
%49 = phi i32 [ 1, %45 ], [ 1, %36 ], [ 0, %19 ]
ret i32 %49
}