#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_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int);
scalar_t__ connect_through_socket(int, struct addrinfo *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int find_connectable_socket(struct addrinfo *res) {
  assert(res != NULL);
  printf("creating socket...\n");
  int socketfd;
  for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) {
    socketfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (socketfd == -1) {
      perror("find_usable_socket-socket()");
      continue;
    }
    if (connect_through_socket(socketfd, ai) == 0) {
      return socketfd;
    }
  }
  assert(socketfd == -1);
  fprintf(stderr, "Couldn't find a socket\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<find_connectable_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1219 <find_connectable_socket+0xa9>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e4 <find_connectable_socket+0x74>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
jmp    1208 <find_connectable_socket+0x98>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <connect_through_socket@plt>
cmp    $0x0,%rax
jne    1203 <find_connectable_socket+0x93>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    1208 <find_connectable_socket+0x98>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11a2 <find_connectable_socket+0x32>
cmpl   $0xffffffff,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<find_connectable_socket>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
je     1214 <find_connectable_socket+0xa4>
lea    0xe63(%rip),%r14
jmp    11b9 <find_connectable_socket+0x49>
mov    %r14,%rdi
call   1070 <perror@plt>
nopw   0x0(%rax,%rax,1)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11e3 <find_connectable_socket+0x73>
mov    0x8(%rbx),%edi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   1090 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     119f <find_connectable_socket+0x2f>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <connect_through_socket@plt>
test   %rax,%rax
jne    11b0 <find_connectable_socket+0x40>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
cmp    $0xffffffff,%ebp
sete   %dil
call   1050 <assert@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
jmp    11ec <find_connectable_socket+0x7c>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo* }
@.str = [20 x i8] c"creating socket...\0A\00"
@.str.1 = [28 x i8] c"find_usable_socket-socket()\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Couldn't find a socket\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @find_connectable_socket(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = alloca %struct.addrinfo*
store %struct.addrinfo* %0, %struct.addrinfo** %2
%5 = load %struct.addrinfo*, %struct.addrinfo** %2
%6 = icmp ne %struct.addrinfo* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = load %struct.addrinfo*, %struct.addrinfo** %2
store %struct.addrinfo* %10, %struct.addrinfo** %4
br label %11
11: ; preds = %37, %1
%12 = load %struct.addrinfo*, %struct.addrinfo** %4
%13 = icmp ne %struct.addrinfo* %12, null
br i1 %13, label %14, label %41
14: ; preds = %11
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 2
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %17, i32 %20, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %37
29: ; preds = %14
%30 = load i32, i32* %3
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = call i64 @connect_through_socket(i32 %30, %struct.addrinfo* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = load i32, i32* %3
ret i32 %35
36: ; preds = %29
br label %37
37: ; preds = %36, %27
%38 = load %struct.addrinfo*, %struct.addrinfo** %4
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 3
%40 = load %struct.addrinfo*, %struct.addrinfo** %39
store %struct.addrinfo* %40, %struct.addrinfo** %4
br label %11
41: ; preds = %11
%42 = load i32, i32* %3
%43 = icmp eq i32 %42, -1
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo* }
@.str.1 = [28 x i8] c"find_usable_socket-socket()\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Couldn't find a socket\0A\00"
@EXIT_FAILURE = global i32 0
@str = [19 x i8] c"creating socket...\00"
define i32 @find_connectable_socket(%struct.addrinfo* %0) {
%2 = icmp ne %struct.addrinfo* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%6 = icmp eq %struct.addrinfo* %0, null
br i1 %6, label %29, label %7
7: ; preds = %1, %22
%8 = phi %struct.addrinfo* [ %24, %22 ], [ %0, %1 ]
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 2
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @socket(i32 %10, i32 %12, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %22
19: ; preds = %7
%20 = tail call i64 @connect_through_socket(i32 %15, %struct.addrinfo* %8)
%21 = icmp eq i64 %20, 0
br i1 %21, label %36, label %22
22: ; preds = %19, %17
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 3
%24 = load %struct.addrinfo*, %struct.addrinfo** %23
%25 = icmp eq %struct.addrinfo* %24, null
br i1 %25, label %26, label %7
26: ; preds = %22
%27 = icmp eq i32 %15, -1
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %26, %1
%30 = phi i32 [ 0, %1 ], [ %28, %26 ]
%31 = tail call i32 @assert(i32 %30)
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %19
ret i32 %15
}