#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 sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hint;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ request(int);
int retry_connect(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                  struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "Usage: %s host service\n", argv[0]);
    exit(1);
  }
  setbuf(stderr, NULL);
  setbuf(stdout, NULL);
  struct addrinfo hint, *ailist, *aip;
  int err, sfd;
  memset(&hint, 0, sizeof(hint));
  if ((err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(err));
    exit(1);
  }
  for (aip = ailist; aip != NULL; aip = aip->ai_next) {
    if ((sfd = retry_connect(aip->ai_family, SOCK_STREAM, 0,
                             (struct sockaddr *)aip->ai_addr,
                             aip->ai_addrlen)) >= 0) {
      request(sfd);
      fprintf(stderr, "request done\n");
      exit(0);
    }
  }
  freeaddrinfo(ailist);
  fprintf(stderr, "cant connect\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d8 <main+0x48>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1267 <main+0xd7>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12e8 <main+0x158>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x40(%rbp),%rax
mov    (%rax),%r8d
xor    %edx,%edx
call   1030 <retry_connect@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jl     12d2 <main+0x142>
mov    -0x48(%rbp),%edi
call   1080 <request@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    12d7 <main+0x147>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    126f <main+0xdf>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
mov    0x2e3b(%rip),%r15
mov    (%r15),%edi
cmp    $0x3,%eax
jne    127e <main+0xee>
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    128a <main+0xfa>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1238 <main+0xa8>
mov    0x2dce(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x10(%rbx),%edi
mov    (%r14),%esi
mov    0x8(%rbx),%rcx
mov    (%rbx),%r8d
xor    %edx,%edx
call   1030 <retry_connect@plt>
test   %eax,%eax
jns    125f <main+0xcf>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1210 <main+0x80>
mov    0x8(%rsp),%rdi
jmp    123a <main+0xaa>
xor    %edi,%edi
call   10b0 <freeaddrinfo@plt>
mov    (%r15),%edi
lea    0xdf4(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %eax,%edi
call   1080 <request@plt>
mov    (%r15),%edi
lea    0xdbf(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    (%rbx),%rdx
lea    0xd78(%rip),%rsi
jmp    12a0 <main+0x110>
mov    (%r15),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd7d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s host service\0A\00"
@stdout = global i32 0
@.str.1 = [23 x i8] c"getaddrinfo error: %s\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [14 x i8] c"request done\0A\00"
@.str.3 = [14 x i8] c"cant connect\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 @setbuf(i32 %21, i32* null)
%23 = load i32, i32* @stdout
%24 = call i32 @setbuf(i32 %23, i32* null)
%25 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @getaddrinfo(i8* %28, i8* %31, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %32, i32* %9
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %20
%35 = load i32, i32* @stderr
%36 = load i32, i32* %9
%37 = call i8* @gai_strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %20
%41 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %41, %struct.addrinfo** %8
br label %42
42: ; preds = %66, %40
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = icmp ne %struct.addrinfo* %43, null
br i1 %44, label %45, label %70
45: ; preds = %42
%46 = load %struct.addrinfo*, %struct.addrinfo** %8
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = load i32, i32* @SOCK_STREAM
%50 = load %struct.addrinfo*, %struct.addrinfo** %8
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = inttoptr i64 %52 to %struct.sockaddr*
%54 = load %struct.addrinfo*, %struct.addrinfo** %8
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @retry_connect(i32 %48, i32 %49, i32 0, %struct.sockaddr* %53, i32 %56)
store i32 %57, i32* %10
%58 = icmp sge i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %45
%60 = load i32, i32* %10
%61 = call i32 @request(i32 %60)
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %45
br label %66
66: ; preds = %65
%67 = load %struct.addrinfo*, %struct.addrinfo** %8
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 3
%69 = load %struct.addrinfo*, %struct.addrinfo** %68
store %struct.addrinfo* %69, %struct.addrinfo** %8
br label %42
70: ; preds = %42
%71 = load %struct.addrinfo*, %struct.addrinfo** %7
%72 = call i32 @freeaddrinfo(%struct.addrinfo* %71)
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
ret i32 1
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s host service\0A\00"
@stdout = global i32 0
@.str.1 = [23 x i8] c"getaddrinfo error: %s\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [14 x i8] c"request done\0A\00"
@.str.3 = [14 x i8] c"cant connect\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = icmp eq i32 %0, 3
%6 = load i32, i32* @stderr
br i1 %5, label %11, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @setbuf(i32 %6, i32* null)
%13 = load i32, i32* @stdout
%14 = tail call i32 @setbuf(i32 %13, i32* null)
%15 = bitcast %struct.addrinfo* %3 to i8*
%16 = bitcast %struct.addrinfo** %4 to i8*
%17 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @getaddrinfo(i8* %19, i8* %21, %struct.addrinfo* %3, %struct.addrinfo** %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %11
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %55, label %36
27: ; preds = %11
%28 = load i32, i32* @stderr
%29 = call i8* @gai_strerror(i32 %22)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %36
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 3
%34 = load %struct.addrinfo*, %struct.addrinfo** %33
%35 = icmp eq %struct.addrinfo* %34, null
br i1 %35, label %53, label %36
36: ; preds = %24, %32
%37 = phi %struct.addrinfo* [ %34, %32 ], [ %25, %24 ]
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 2
%39 = load i32, i32* %38
%40 = load i32, i32* @SOCK_STREAM
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @retry_connect(i32 %39, i32 %40, i32 0, %struct.sockaddr* %43, i32 %45)
%47 = icmp sgt i32 %46, -1
br i1 %47, label %48, label %32
48: ; preds = %36
%49 = call i32 @request(i32 %46)
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %32
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %55
55: ; preds = %53, %24
%56 = phi %struct.addrinfo* [ %54, %53 ], [ null, %24 ]
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
ret i32 1
}