#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;
  int /*<<< orphan*/ ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int dial(struct addrinfo *ai) {
  struct addrinfo *aip;
  int s, err;
  for (aip = ai; aip != NULL; aip->ai_next) {
    s = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
    if (s == -1) {
      perror("socket");
      continue;
    }
    err = connect(s, aip->ai_addr, aip->ai_addrlen);
    if (err != -1)
      break;
    close(s);
    s = -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<dial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d2 <dial+0x92>
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)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1197 <dial+0x57>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
jmp    11cd <dial+0x8d>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11be <dial+0x7e>
jmp    11d2 <dial+0x92>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1154 <dial+0x14>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dial>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     118f <dial+0x4f>
mov    %rdi,%rbx
lea    0xead(%rip),%r14
jmp    1160 <dial+0x20>
mov    %r14,%rdi
call   1040 <perror@plt>
nopl   (%rax)
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     1155 <dial+0x15>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1194 <dial+0x54>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1160 <dial+0x20>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@.str = [7 x i8] c"socket\00"
define i32 @dial(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %2
%6 = load %struct.addrinfo*, %struct.addrinfo** %2
store %struct.addrinfo* %6, %struct.addrinfo** %3
br label %7
7: ; preds = %40, %1
%8 = load %struct.addrinfo*, %struct.addrinfo** %3
%9 = icmp ne %struct.addrinfo* %8, null
br i1 %9, label %10, label %44
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 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %10
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %40
25: ; preds = %10
%26 = load i32, i32* %4
%27 = load %struct.addrinfo*, %struct.addrinfo** %3
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load %struct.addrinfo*, %struct.addrinfo** %3
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @connect(i32 %26, i32 %29, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %25
br label %44
37: ; preds = %25
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %4
br label %40
40: ; preds = %37, %23
%41 = load %struct.addrinfo*, %struct.addrinfo** %3
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 5
%43 = load i32, i32* %42
br label %7
44: ; preds = %36, %7
%45 = load i32, i32* %4
ret i32 %45
}

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