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

/* Variables and functions */
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ STDOUT_FILENO;
int connect_retry(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/,
                  int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  struct addrinfo *ailist, *aip;
  struct addrinfo hint;
  hint.ai_flags = AI_CANONNAME;
  hint.ai_family = 0;
  hint.ai_socktype = 0;
  int err;
  if ((err = getaddrinfo("localhost", "8080", &hint, &ailist)) != 0) {
    printf("error: %s\n", gai_strerror(err));
    exit(1);
  }
  for (aip = ailist; aip != NULL; aip = aip->ai_next) {
    int sockfd;
    if ((sockfd = connect_retry(aip->ai_family, SOCK_STREAM, 0, aip->ai_addr,
                                aip->ai_addrlen)) < 0) {
      err = errno;
    } else {
      int n;
      char buf[128];
      while ((n = recv(sockfd, buf, 128, 0)) > 0) {
        write(STDOUT_FILENO, buf, n);
      }
      if (n < 0) {
        printf("recv error\n");
      }
      exit(0);
    }
  }
  printf("cannot connect to server\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe54(%rip),%rdi
lea    0xe57(%rip),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     11ef <main+0x7f>
mov    -0x4c(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12be <main+0x14e>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
xor    %edx,%edx
call   1070 <connect_retry@plt>
mov    %eax,-0x50(%rbp)
cmp    $0x0,%eax
jge    1245 <main+0xd5>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    12a8 <main+0x138>
jmp    124a <main+0xda>
mov    -0x50(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jle    1289 <main+0x119>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    -0xe0(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1040 <write@plt>
jmp    124a <main+0xda>
cmpl   $0x0,-0x54(%rbp)
jge    12a1 <main+0x131>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    12ad <main+0x13d>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11f7 <main+0x87>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xb0,%rsp
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe4f(%rip),%rdi
lea    0xe52(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    1278 <main+0xf8>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1201 <main+0x81>
mov    0x2df3(%rip),%r14
nopl   (%rax)
mov    0x8(%rbx),%rdi
mov    (%r14),%esi
mov    (%rbx),%r8d
mov    0x4(%rbx),%ecx
xor    %edx,%edx
call   1080 <connect_retry@plt>
test   %eax,%eax
jns    121b <main+0x9b>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    11e0 <main+0x60>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebp
lea    0x30(%rsp),%rsi
mov    %eax,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1261 <main+0xe1>
mov    0x2db5(%rip),%rbx
lea    0x30(%rsp),%r14
mov    (%rbx),%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1240 <main+0xc0>
test   %eax,%eax
jns    1271 <main+0xf1>
lea    0xdc7(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    %eax,%edi
call   1070 <gai_strerror@plt>
lea    0xd89(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, %struct.addrinfo*, i64, i32 }
@AI_CANONNAME = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"8080\00"
@.str.2 = [11 x i8] c"error: %s\0A\00"
@SOCK_STREAM = global i32 0
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"recv error\0A\00"
@.str.4 = [26 x i8] c"cannot connect to server\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @AI_CANONNAME
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i64 0, i64* %16
%17 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %8, %struct.addrinfo** %6)
store i32 %17, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i32, i32* %9
%21 = call i8* @gai_strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %25, %struct.addrinfo** %7
br label %26
26: ; preds = %63, %24
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = icmp ne %struct.addrinfo* %27, null
br i1 %28, label %29, label %67
29: ; preds = %26
%30 = load %struct.addrinfo*, %struct.addrinfo** %7
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 2
%32 = load i64, i64* %31
%33 = load i32, i32* @SOCK_STREAM
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @connect_retry(i64 %32, i32 %33, i32 0, i32 %36, i32 %39)
store i32 %40, i32* %10
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %29
%43 = load i32, i32* @errno
store i32 %43, i32* %9
br label %62
44: ; preds = %29
br label %45
45: ; preds = %50, %44
%46 = load i32, i32* %10
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%48 = call i32 @recv(i32 %46, i8* %47, i32 128, i32 0)
store i32 %48, i32* %11
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* @STDOUT_FILENO
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%53 = load i32, i32* %11
%54 = call i32 @write(i32 %51, i8* %52, i32 %53)
br label %45
55: ; preds = %45
%56 = load i32, i32* %11
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %42
br label %63
63: ; preds = %62
%64 = load %struct.addrinfo*, %struct.addrinfo** %7
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 3
%66 = load %struct.addrinfo*, %struct.addrinfo** %65
store %struct.addrinfo* %66, %struct.addrinfo** %7
br label %26
67: ; preds = %26
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, %struct.addrinfo*, i64, i32 }
@AI_CANONNAME = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"8080\00"
@.str.2 = [11 x i8] c"error: %s\0A\00"
@SOCK_STREAM = global i32 0
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@str = [25 x i8] c"cannot connect to server\00"
@str.5 = [11 x i8] c"recv error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = alloca [128 x i8]
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = load i32, i32* @AI_CANONNAME
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i64 0, i64* %11
%12 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %3
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %53, label %25
17: ; preds = %2
%18 = call i8* @gai_strerror(i32 %12)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %25
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%23 = load %struct.addrinfo*, %struct.addrinfo** %22
%24 = icmp eq %struct.addrinfo* %23, null
br i1 %24, label %53, label %25
25: ; preds = %14, %21
%26 = phi %struct.addrinfo* [ %23, %21 ], [ %15, %14 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%28 = load i64, i64* %27
%29 = load i32, i32* @SOCK_STREAM
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect_retry(i64 %28, i32 %29, i32 0, i32 %31, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %21, label %36
36: ; preds = %25
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%38 = call i32 @recv(i32 %34, i8* %37, i32 128, i32 0)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %36, %40
%41 = phi i32 [ %44, %40 ], [ %38, %36 ]
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @write(i32 %42, i8* %37, i32 %41)
%44 = call i32 @recv(i32 %34, i8* %37, i32 128, i32 0)
%45 = icmp sgt i32 %44, 0
br i1 %45, label %40, label %46
46: ; preds = %40, %36
%47 = phi i32 [ %38, %36 ], [ %44, %40 ]
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %21, %14
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
ret i32 0
}