#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*/ AF_UNSPEC;
int EAI_SYSTEM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ rsp_log(char *, ...);
int /*<<< orphan*/ rsp_log_error(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_backend(char *backend_host, char *backend_port_str) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  int getaddrinfo_error;
  struct addrinfo *addrs;
  getaddrinfo_error =
      getaddrinfo(backend_host, backend_port_str, &hints, &addrs);
  if (getaddrinfo_error != 0) {
    if (getaddrinfo_error == EAI_SYSTEM) {
      rsp_log_error("Couldn't find backend");
    } else {
      rsp_log("Couldn't find backend: %s", gai_strerror(getaddrinfo_error));
    }
    exit(1);
  }
  int backend_socket_fd;
  struct addrinfo *addrs_iter;
  for (addrs_iter = addrs; addrs_iter != NULL;
       addrs_iter = addrs_iter->ai_next) {
    backend_socket_fd = socket(addrs_iter->ai_family, addrs_iter->ai_socktype,
                               addrs_iter->ai_protocol);
    if (backend_socket_fd == -1) {
      continue;
    }
    if (connect(backend_socket_fd, addrs_iter->ai_addr,
                addrs_iter->ai_addrlen) != -1) {
      break;
    }
    close(backend_socket_fd);
  }
  if (addrs_iter == NULL) {
    rsp_log("Couldn't connect to backend");
    exit(1);
  }
  freeaddrinfo(addrs);
  return backend_socket_fd;
}


x86 assembly (clang, -O0):
<connect_to_backend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     123f <connect_to_backend+0x9f>
mov    -0x34(%rbp),%eax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%eax
jne    121d <connect_to_backend+0x7d>
lea    0xded(%rip),%rdi
call   1060 <rsp_log_error@plt>
jmp    1235 <connect_to_backend+0x95>
mov    -0x34(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %eax,%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <rsp_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12ba <connect_to_backend+0x11a>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    127e <connect_to_backend+0xde>
jmp    12a9 <connect_to_backend+0x109>
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     12a1 <connect_to_backend+0x101>
jmp    12ba <connect_to_backend+0x11a>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1247 <connect_to_backend+0xa7>
cmpq   $0x0,-0x50(%rbp)
jne    12dd <connect_to_backend+0x13d>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1070 <rsp_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_backend>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    1264 <connect_to_backend+0xc4>
mov    (%rsp),%rbx
jmp    120b <connect_to_backend+0x6b>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     124c <connect_to_backend+0xac>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1207 <connect_to_backend+0x67>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1200 <connect_to_backend+0x60>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1070 <rsp_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
jne    1285 <connect_to_backend+0xe5>
lea    0xd8a(%rip),%rdi
call   1060 <rsp_log_error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <rsp_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@EAI_SYSTEM = global i32 0
@.str = [22 x i8] c"Couldn't find backend\00"
@.str.1 = [26 x i8] c"Couldn't find backend: %s\00"
@.str.2 = [28 x i8] c"Couldn't connect to backend\00"
define i32 @connect_to_backend(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo
%6 = alloca i32
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i8*, i8** %3
%16 = load i8*, i8** %4
%17 = call i32 @getaddrinfo(i8* %15, i8* %16, %struct.addrinfo* %5, %struct.addrinfo** %7)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* @EAI_SYSTEM
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @rsp_log_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %30
26: ; preds = %20
%27 = load i32, i32* %6
%28 = call i32 @gai_strerror(i32 %27)
%29 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %26, %24
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %2
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %33, %struct.addrinfo** %9
br label %34
34: ; preds = %65, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %69
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %9
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 4
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %9
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %9
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 @socket(i32 %40, i32 %43, i32 %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %37
br label %65
51: ; preds = %37
%52 = load i32, i32* %8
%53 = load %struct.addrinfo*, %struct.addrinfo** %9
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %9
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @connect(i32 %52, i32 %55, i32 %58)
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %51
br label %69
62: ; preds = %51
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %50
%66 = load %struct.addrinfo*, %struct.addrinfo** %9
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 5
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %9
br label %34
69: ; preds = %61, %34
%70 = load %struct.addrinfo*, %struct.addrinfo** %9
%71 = icmp eq %struct.addrinfo* %70, null
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %69
%76 = load %struct.addrinfo*, %struct.addrinfo** %7
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
%78 = load i32, i32* %8
ret i32 %78
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@EAI_SYSTEM = global i32 0
@.str = [22 x i8] c"Couldn't find backend\00"
@.str.1 = [26 x i8] c"Couldn't find backend: %s\00"
@.str.2 = [28 x i8] c"Couldn't connect to backend\00"
define i32 @connect_to_backend(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = bitcast %struct.addrinfo** %4 to i8*
%12 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %50, label %27
17: ; preds = %2
%18 = load i32, i32* @EAI_SYSTEM
%19 = icmp eq i32 %12, %18
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @rsp_log_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %25
22: ; preds = %17
%23 = call i32 @gai_strerror(i32 %12)
%24 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %20
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14, %46
%28 = phi %struct.addrinfo* [ %48, %46 ], [ %15, %14 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %46, label %37
37: ; preds = %27
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @connect(i32 %35, i32 %39, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %53
44: ; preds = %37
%45 = call i32 @close(i32 %35)
br label %46
46: ; preds = %27, %44
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %50, label %27
50: ; preds = %46, %14
%51 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %37
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
ret i32 %35
}