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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ handle_tftp_read(int, int /*<<< orphan*/, int /*<<< orphan*/,
                                    char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sock;
  struct addrinfo hints;
  struct addrinfo *results, *rp, *server;
  int retval;
  if (argc < 3) {
    printf("Usage: %s <server-name> <filename>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  if ((retval = getaddrinfo(argv[1], "tftp", &hints, &results)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retval));
    exit(EXIT_FAILURE);
  }
  server = NULL;
  rp = results;
  while (rp != NULL) {
    if ((sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) !=
        -1) {
      server = rp;
      rp = NULL;
    } else {
      printf("Failed socket creation, moving to next\n");
      rp = rp->ai_next;
    }
  }
  if (server == NULL) {
    fprintf(stderr, "Unable to connect to server %s:%s\n", argv[1], argv[2]);
    exit(EXIT_FAILURE);
  }
  handle_tftp_read(sock, server->ai_addr, server->ai_addrlen, argv[2]);
  freeaddrinfo(results);
  close(sock);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0a(%rip),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1269 <main+0xc9>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movq   $0x0,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12de <main+0x13e>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12bf <main+0x11f>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
jmp    12d9 <main+0x139>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1279 <main+0xd9>
cmpq   $0x0,-0x50(%rbp)
jne    131e <main+0x17e>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
call   1080 <handle_tftp_read@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jle    12b7 <main+0x107>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x8(%r14),%rdi
lea    0xe25(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
jne    12ca <main+0x11a>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1254 <main+0xa4>
lea    0xe3b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rbx),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1281 <main+0xd1>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    1230 <main+0x80>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
mov    0x10(%r14),%rcx
lea    0xdce(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    0x10(%r14),%rcx
mov    %eax,%edi
call   1090 <handle_tftp_read@plt>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd3f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1273 <main+0xc3>
mov    0x2cff(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd48(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1273 <main+0xc3>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32 }
@.str = [36 x i8] c"Usage: %s <server-name> <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [5 x i8] c"tftp\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.3 = [40 x i8] c"Failed socket creation, moving to next\0A\00"
@.str.4 = [35 x i8] c"Unable to connect to server %s:%s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
store i32 %23, i32* %24
%25 = load i32, i32* @SOCK_DGRAM
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %25, i32* %26
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @getaddrinfo(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %30, i32* %11
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %21
%33 = load i32, i32* @stderr
%34 = load i32, i32* %11
%35 = call i8* @gai_strerror(i32 %34)
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %21
store %struct.addrinfo* null, %struct.addrinfo** %10
%40 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %40, %struct.addrinfo** %9
br label %41
41: ; preds = %63, %39
%42 = load %struct.addrinfo*, %struct.addrinfo** %9
%43 = icmp ne %struct.addrinfo* %42, null
br i1 %43, label %44, label %64
44: ; preds = %41
%45 = load %struct.addrinfo*, %struct.addrinfo** %9
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 5
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %9
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 4
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 3
%53 = load i32, i32* %52
%54 = call i32 @socket(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %6
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %44
%57 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %57, %struct.addrinfo** %10
store %struct.addrinfo* null, %struct.addrinfo** %9
br label %63
58: ; preds = %44
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%60 = load %struct.addrinfo*, %struct.addrinfo** %9
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 2
%62 = load %struct.addrinfo*, %struct.addrinfo** %61
store %struct.addrinfo* %62, %struct.addrinfo** %9
br label %63
63: ; preds = %58, %56
br label %41
64: ; preds = %41
%65 = load %struct.addrinfo*, %struct.addrinfo** %10
%66 = icmp eq %struct.addrinfo* %65, null
br i1 %66, label %67, label %78
67: ; preds = %64
%68 = load i32, i32* @stderr
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load i8*, i8** %70
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %71, i8* %74)
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %64
%79 = load i32, i32* %6
%80 = load %struct.addrinfo*, %struct.addrinfo** %10
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 1
%82 = load i32, i32* %81
%83 = load %struct.addrinfo*, %struct.addrinfo** %10
%84 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %83, i32 0, i32 0
%85 = load i32, i32* %84
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 2
%88 = load i8*, i8** %87
%89 = call i32 @handle_tftp_read(i32 %79, i32 %82, i32 %85, i8* %88)
%90 = load %struct.addrinfo*, %struct.addrinfo** %8
%91 = call i32 @freeaddrinfo(%struct.addrinfo* %90)
%92 = load i32, i32* %6
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* @EXIT_SUCCESS
ret i32 %94
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32 }
@.str = [36 x i8] c"Usage: %s <server-name> <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [5 x i8] c"tftp\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.4 = [35 x i8] c"Unable to connect to server %s:%s\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [39 x i8] c"Failed socket creation, moving to next\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_DGRAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %17, i32* %18
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @getaddrinfo(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = call i8* @gai_strerror(i32 %21)
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %13
%30 = load %struct.addrinfo*, %struct.addrinfo** %4
%31 = icmp eq %struct.addrinfo* %30, null
br i1 %31, label %47, label %32
32: ; preds = %29, %42
%33 = phi %struct.addrinfo* [ %45, %42 ], [ %30, %29 ]
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 5
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 4
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 3
%39 = load i32, i32* %38
%40 = call i32 @socket(i32 %35, i32 %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %55
42: ; preds = %32
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 2
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %47, label %32
47: ; preds = %42, %29
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %19
%50 = getelementptr inbounds i8*, i8** %1, i64 2
%51 = load i8*, i8** %50
%52 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %49, i8* %51)
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %32
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 1
%57 = load i32, i32* %56
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 0
%59 = load i32, i32* %58
%60 = getelementptr inbounds i8*, i8** %1, i64 2
%61 = load i8*, i8** %60
%62 = call i32 @handle_tftp_read(i32 %40, i32 %57, i32 %59, i8* %61)
%63 = load %struct.addrinfo*, %struct.addrinfo** %4
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
%65 = call i32 @close(i32 %40)
%66 = load i32, i32* @EXIT_SUCCESS
ret i32 %66
}