#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 */
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_and_connect(struct addrinfo *ps) {
  struct addrinfo *rp;
  int client_socket;
  int success = 0;
  for (rp = ps; rp != NULL; rp = rp->ai_next) {
    if ((client_socket =
             socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1) {
      continue;
    }
    if (connect(client_socket, rp->ai_addr, rp->ai_addrlen) != -1) {
      success = 1;
      break;
    } else {
      continue;
    }
  }
  if (success > 0)
    printf("client: connected ... \n");
  else {
    printf("client: cannot connect: exit. \n");
    exit(-1);
  }
  return client_socket;
}


x86 assembly (clang, -O0):
<socket_and_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d1 <socket_and_connect+0x91>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1191 <socket_and_connect+0x51>
jmp    11c0 <socket_and_connect+0x80>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     11bb <socket_and_connect+0x7b>
movl   $0x1,-0x18(%rbp)
jmp    11d1 <socket_and_connect+0x91>
jmp    11c0 <socket_and_connect+0x80>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    115b <socket_and_connect+0x1b>
cmpl   $0x0,-0x18(%rbp)
jle    11ec <socket_and_connect+0xac>
lea    0xe1e(%rip),%rdi
call   1030 <printf@plt>
jmp    1202 <socket_and_connect+0xc2>
lea    0xe25(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_and_connect>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1194 <socket_and_connect+0x54>
mov    %rdi,%rbx
jmp    1159 <socket_and_connect+0x19>
nopl   (%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1194 <socket_and_connect+0x54>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     1150 <socket_and_connect+0x10>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     1150 <socket_and_connect+0x10>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@str = [31 x i8] c"client: cannot connect: exit. \00"
@str.2 = [23 x i8] c"client: connected ... \00"
define i32 @socket_and_connect(%struct.addrinfo* %0) {
%2 = icmp eq %struct.addrinfo* %0, null
br i1 %2, label %26, label %3
3: ; preds = %1, %20
%4 = phi %struct.addrinfo* [ %22, %20 ], [ %0, %1 ]
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%10 = load i32, i32* %9
%11 = tail call i32 @socket(i32 %6, i32 %8, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %20, label %13
13: ; preds = %3
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @connect(i32 %11, i32 %15, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %13, %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
%22 = load %struct.addrinfo*, %struct.addrinfo** %21
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %26, label %3
24: ; preds = %13
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.2, i64 0, i64 0))
ret i32 %11
26: ; preds = %20, %1
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -1)
unreachable
}