#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 /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
struct addrinfo *resolve_host(char const *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_memcached(const char *hostaddr, const char *port) {
  struct addrinfo *host, *rp;
  int sfd;
  host = resolve_host(hostaddr, port);
  if (!host) {
    exit(1);
  }
  for (rp = host; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(sfd);
  }
  if (rp == NULL) {
    fprintf(stderr, "no server found\n");
    exit(1);
  }
  freeaddrinfo(host);
  return sfd;
}


x86 assembly (clang, -O0):
<connect_memcached>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <resolve_host@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <connect_memcached+0x36>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1221 <connect_memcached+0xb1>
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   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11e5 <connect_memcached+0x75>
jmp    1210 <connect_memcached+0xa0>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1208 <connect_memcached+0x98>
jmp    1221 <connect_memcached+0xb1>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11ae <connect_memcached+0x3e>
cmpq   $0x0,-0x20(%rbp)
jne    124b <connect_memcached+0xdb>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_memcached>:
push   %rbp
push   %r14
push   %rbx
call   1050 <resolve_host@plt>
test   %rax,%rax
je     11ea <connect_memcached+0x7a>
mov    %rax,%r14
mov    %rax,%rbx
jmp    11a0 <connect_memcached+0x30>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     11d5 <connect_memcached+0x65>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1197 <connect_memcached+0x27>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1190 <connect_memcached+0x20>
mov    %r14,%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@stderr = global i32 0
@.str = [17 x i8] c"no server found\0A\00"
define i32 @connect_memcached(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = load i8*, i8** %4
%10 = call %struct.addrinfo* @resolve_host(i8* %8, i8* %9)
store %struct.addrinfo* %10, %struct.addrinfo** %5
%11 = load %struct.addrinfo*, %struct.addrinfo** %5
%12 = icmp ne %struct.addrinfo* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %16, %struct.addrinfo** %6
br label %17
17: ; preds = %48, %15
%18 = load %struct.addrinfo*, %struct.addrinfo** %6
%19 = icmp ne %struct.addrinfo* %18, null
br i1 %19, label %20, label %52
20: ; preds = %17
%21 = load %struct.addrinfo*, %struct.addrinfo** %6
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 4
%23 = load i32, i32* %22
%24 = load %struct.addrinfo*, %struct.addrinfo** %6
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 3
%26 = load i32, i32* %25
%27 = load %struct.addrinfo*, %struct.addrinfo** %6
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %23, i32 %26, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %20
br label %48
34: ; preds = %20
%35 = load i32, i32* %7
%36 = load %struct.addrinfo*, %struct.addrinfo** %6
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @connect(i32 %35, i32 %38, i32 %41)
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %34
br label %52
45: ; preds = %34
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %33
%49 = load %struct.addrinfo*, %struct.addrinfo** %6
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 5
%51 = load %struct.addrinfo*, %struct.addrinfo** %50
store %struct.addrinfo* %51, %struct.addrinfo** %6
br label %17
52: ; preds = %44, %17
%53 = load %struct.addrinfo*, %struct.addrinfo** %6
%54 = icmp eq %struct.addrinfo* %53, null
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %52
%60 = load %struct.addrinfo*, %struct.addrinfo** %5
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
%62 = load i32, i32* %7
ret i32 %62
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@stderr = global i32 0
@.str = [17 x i8] c"no server found\0A\00"
define i32 @connect_memcached(i8* %0, i8* %1) {
%3 = tail call %struct.addrinfo* @resolve_host(i8* %0, i8* %1)
%4 = icmp eq %struct.addrinfo* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2, %26
%8 = phi %struct.addrinfo* [ %28, %26 ], [ %3, %2 ]
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 4
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 3
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 2
%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 %26, label %17
17: ; preds = %7
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @connect(i32 %15, i32 %19, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %34
24: ; preds = %17
%25 = tail call i32 @close(i32 %15)
br label %26
26: ; preds = %7, %24
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 5
%28 = load %struct.addrinfo*, %struct.addrinfo** %27
%29 = icmp eq %struct.addrinfo* %28, null
br i1 %29, label %30, label %7
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %17
%35 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
ret i32 %15
}