#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  long s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  char *h_name;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
  scalar_t__ *h_addr_list;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ Address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int getopt(int, char **, char *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(struct in_addr);
char *malloc(int);
int /*<<< orphan*/ memcpy(long *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
size_t optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int readHeaders(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int hSocket;
  struct hostent *pHostInfo;
  struct sockaddr_in Address;
  long nHostAddress;
  char *body;
  ssize_t bytesRead;
  char strHostName[255];
  int contentLength, downloadCount, c;
  int nHostPort;
  int debug = 0, verbose = 0, timesToDownload = 1;
  extern char *optarg;
  if (argc < 4) {
    printf("usage: %s [-d] [-c COUNT] <host-name> <port> <file>\n", argv[0]);
    return 1;
  }
  while ((c = getopt(argc, argv, "c:dv")) != -1) {
    switch (c) {
    case 'c':
      timesToDownload = atoi(optarg);
      if (timesToDownload <= 0) {
        fprintf(
            stderr,
            "Error: Times to download (-c COUNT) must be a positive number.\n");
        return 1;
      }
      break;
    case 'd':
      debug = 1;
      break;
    case 'v':
      verbose = 1;
      break;
    default:
      printf("Option -%c not recognized. Ignoring.\n", c);
      break;
    }
  }
  if (strlen(argv[optind]) >= 255) {
    fprintf(
        stderr,
        "Error: Host name too large. Must be less than %d characters long.\n",
        255);
    return 1;
  }
  strcpy(strHostName, argv[optind]);
  nHostPort = atoi(argv[optind + 1]);
  if (nHostPort <= 0) {
    fprintf(stderr, "Error: Invalid port number provided.\n");
    return 1;
  }
  pHostInfo = gethostbyname(strHostName);
  if (pHostInfo == NULL) {
    perror("DNS Lookup Failure");
    fprintf(stderr, "Error: No such hostname: %s\n", strHostName);
    return 1;
  }
  if (verbose) {
    printf("--Host: %s -> %s\n", pHostInfo->h_name,
           inet_ntoa(*(struct in_addr *)pHostInfo->h_addr_list[0]));
  }
  memcpy(&nHostAddress, pHostInfo->h_addr, pHostInfo->h_length);
  memset(&Address, 0, sizeof(Address));
  Address.sin_addr.s_addr = nHostAddress;
  Address.sin_port = htons(nHostPort);
  Address.sin_family = AF_INET;
  char *message = malloc(1024);
  sprintf(message,
          "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nAccept: "
          "text/html, text/plain, */*\r\nUser-Agent: "
          "CS360Downloader/1.0\r\nReferer: http://%s/\r\n\r\n",
          argv[optind + 2], strHostName, strHostName);
  for (downloadCount = 0; downloadCount < timesToDownload; downloadCount++) {
    hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (hSocket == -1) {
      perror("Failure to create socket");
      fprintf(stderr, "Error: Could not create connection (%d)\n", errno);
      return 2;
    }
    if (verbose) {
      printf("--Connecting to http://%s:%d\n", strHostName, nHostPort);
    }
    if (connect(hSocket, (struct sockaddr *)&Address, sizeof(Address)) == -1) {
      perror("Connection error");
      fprintf(stderr, "Error: Could not connect to host (%d)\n", errno);
      return 2;
    }
    write(hSocket, message, strlen(message));
    if (debug) {
      printf("Request:\n%s\n", message);
    }
    contentLength = readHeaders(hSocket, debug);
    if (verbose) {
      printf("--Content Length: %d\n", contentLength);
    }
    if (contentLength >= 0) {
      body = malloc((contentLength + 1) * sizeof(char));
      bytesRead = read(hSocket, body, contentLength);
      if (bytesRead == -1) {
        perror("Failure reading from socket");
        fprintf(stderr, "Error: Problem reading response body (%d)\n", errno);
        return 2;
      }
      if (timesToDownload == 1) {
        body[bytesRead] = 0;
        printf("%s\n", body);
      }
    } else {
      if (verbose) {
        printf("--Reading to the end of the socket.\n");
      }
      body = malloc(512 * sizeof(char));
      while ((bytesRead = read(hSocket, body, 512)) != 0) {
        if (bytesRead == -1) {
          perror("Failure reading from socket");
          fprintf(stderr, "Error: Problem reading response body (%d)\n", errno);
          return 2;
        }
        body[bytesRead] = 0;
        if (timesToDownload == 1) {
          printf("%s", body);
        }
        if (bytesRead < 512) {
          break;
        }
      }
      printf("\n");
    }
    if (close(hSocket) == -1) {
      perror("Failed to close socket");
      fprintf(stderr, "Error: Could not close connection (%d)\n", errno);
      return 2;
    }
  }
  if (downloadCount > 1) {
    printf("Downloaded %d times.\n", downloadCount);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x164(%rbp)
movl   $0x0,-0x168(%rbp)
movl   $0x1,-0x16c(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    12a2 <main+0x62>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    18ff <main+0x6bf>
jmp    12a7 <main+0x67>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xd80(%rip),%rdx
call   1120 <getopt@plt>
mov    %eax,-0x15c(%rbp)
cmp    $0xffffffff,%eax
je     138c <main+0x14c>
mov    -0x15c(%rbp),%eax
mov    %eax,-0x17c(%rbp)
sub    $0x63,%eax
je     130b <main+0xcb>
jmp    12e3 <main+0xa3>
mov    -0x17c(%rbp),%eax
sub    $0x64,%eax
je     1355 <main+0x115>
jmp    12f7 <main+0xb7>
mov    -0x17c(%rbp),%eax
sub    $0x76,%eax
je     1364 <main+0x124>
jmp    1373 <main+0x133>
mov    0x2cb6(%rip),%rax
mov    (%rax),%rdi
call   1130 <atoi@plt>
mov    %eax,-0x16c(%rbp)
cmpl   $0x0,-0x16c(%rbp)
jg     1350 <main+0x110>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    18ff <main+0x6bf>
jmp    1387 <main+0x147>
movl   $0x1,-0x164(%rbp)
jmp    1387 <main+0x147>
movl   $0x1,-0x168(%rbp)
jmp    1387 <main+0x147>
mov    -0x15c(%rbp),%esi
lea    0xcfa(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    12a7 <main+0x67>
mov    -0x10(%rbp),%rax
mov    0x2c41(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <strlen@plt>
cmp    $0xff,%eax
jl     13d6 <main+0x196>
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
lea    0xce2(%rip),%rsi
mov    $0xff,%edx
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    18ff <main+0x6bf>
lea    -0x150(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x2bf0(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x2bd9(%rip),%rcx
mov    (%rcx),%rcx
mov    0x8(%rax,%rcx,8),%rdi
call   1130 <atoi@plt>
mov    %eax,-0x160(%rbp)
cmpl   $0x0,-0x160(%rbp)
jg     1442 <main+0x202>
mov    0x2b9a(%rip),%rax
mov    (%rax),%edi
lea    0xcb4(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    18ff <main+0x6bf>
lea    -0x150(%rbp),%rdi
call   10d0 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1493 <main+0x253>
lea    0xca5(%rip),%rdi
call   1110 <perror@plt>
mov    0x2b50(%rip),%rax
mov    (%rax),%edi
lea    -0x150(%rbp),%rdx
lea    0xc9c(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    18ff <main+0x6bf>
cmpl   $0x0,-0x168(%rbp)
je     14d8 <main+0x298>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x188(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1060 <inet_ntoa@plt>
mov    -0x188(%rbp),%rsi
mov    %rax,%rdx
lea    0xc68(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
lea    -0x38(%rbp),%rdi
call   10f0 <memcpy@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <memset@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x160(%rbp),%edi
call   1080 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2acc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x400,%edi
call   1100 <malloc@plt>
mov    %rax,-0x178(%rbp)
mov    -0x178(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x2a94(%rip),%rcx
mov    (%rcx),%rcx
mov    0x10(%rax,%rcx,8),%rdx
lea    -0x150(%rbp),%rcx
lea    -0x150(%rbp),%r8
lea    0xbea(%rip),%rsi
call   1140 <sprintf@plt>
movl   $0x0,-0x158(%rbp)
mov    -0x158(%rbp),%eax
cmp    -0x16c(%rbp),%eax
jge    18d7 <main+0x697>
mov    0x2a5f(%rip),%rax
mov    (%rax),%edi
mov    0x2a26(%rip),%rax
mov    (%rax),%esi
mov    0x2a45(%rip),%rax
mov    (%rax),%edx
call   1160 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    15e7 <main+0x3a7>
lea    0xc20(%rip),%rdi
call   1110 <perror@plt>
mov    0x29fe(%rip),%rax
mov    (%rax),%edi
mov    0x29e5(%rip),%rax
mov    (%rax),%edx
lea    0xc1b(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    18ff <main+0x6bf>
cmpl   $0x0,-0x168(%rbp)
je     160f <main+0x3cf>
lea    -0x150(%rbp),%rsi
mov    -0x160(%rbp),%edx
lea    0xc10(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1150 <connect@plt>
cmp    $0xffffffff,%eax
jne    1661 <main+0x421>
lea    0xc06(%rip),%rdi
call   1110 <perror@plt>
mov    0x2984(%rip),%rax
mov    (%rax),%edi
mov    0x296b(%rip),%rax
mov    (%rax),%edx
lea    0xbf9(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    18ff <main+0x6bf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x194(%rbp)
mov    -0x178(%rbp),%rax
mov    %rax,-0x190(%rbp)
mov    -0x178(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x194(%rbp),%edi
mov    -0x190(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmpl   $0x0,-0x164(%rbp)
je     16ba <main+0x47a>
mov    -0x178(%rbp),%rsi
lea    0xbbb(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x164(%rbp),%esi
call   1030 <readHeaders@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x168(%rbp)
je     16ef <main+0x4af>
mov    -0x154(%rbp),%esi
lea    0xb93(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
cmpl   $0x0,-0x154(%rbp)
jl     179d <main+0x55d>
mov    -0x154(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x154(%rbp),%edx
call   10c0 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    176d <main+0x52d>
lea    0xb55(%rip),%rdi
call   1110 <perror@plt>
mov    0x2878(%rip),%rax
mov    (%rax),%edi
mov    0x285f(%rip),%rax
mov    (%rax),%edx
lea    0xb53(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    18ff <main+0x6bf>
cmpl   $0x1,-0x16c(%rbp)
jne    1798 <main+0x558>
mov    -0x40(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rsi
lea    0xae6(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1875 <main+0x635>
cmpl   $0x0,-0x168(%rbp)
je     17b8 <main+0x578>
lea    0xb27(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    $0x200,%edi
call   1100 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    $0x200,%edx
call   10c0 <read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1867 <main+0x627>
cmpl   $0xffffffff,-0x44(%rbp)
jne    1825 <main+0x5e5>
lea    0xa9d(%rip),%rdi
call   1110 <perror@plt>
mov    0x27c0(%rip),%rax
mov    (%rax),%edi
mov    0x27a7(%rip),%rax
mov    (%rax),%edx
lea    0xa9b(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    18ff <main+0x6bf>
mov    -0x40(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1,-0x16c(%rbp)
jne    1850 <main+0x610>
mov    -0x40(%rbp),%rsi
lea    0xab4(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
cmpl   $0x200,-0x44(%rbp)
jge    1862 <main+0x622>
jmp    1867 <main+0x627>
jmp    17c6 <main+0x586>
lea    0x966(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
cmp    $0xffffffff,%eax
jne    18be <main+0x67e>
lea    0xa73(%rip),%rdi
call   1110 <perror@plt>
mov    0x2727(%rip),%rax
mov    (%rax),%edi
mov    0x270e(%rip),%rax
mov    (%rax),%edx
lea    0xa6c(%rip),%rsi
mov    $0x0,%al
call   10e0 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    18ff <main+0x6bf>
jmp    18c3 <main+0x683>
mov    -0x158(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x158(%rbp)
jmp    1570 <main+0x330>
cmpl   $0x1,-0x158(%rbp)
jle    18f8 <main+0x6b8>
mov    -0x158(%rbp),%esi
lea    0xa4e(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %rsi,%rbp
cmp    $0x4,%edi
jge    1290 <main+0x30>
mov    0x0(%rbp),%rsi
lea    0xd7c(%rip),%rdi
xor    %eax,%eax
call   10b0 <printf@plt>
jmp    1658 <main+0x3f8>
mov    %edi,%ebx
mov    $0x1,%r14d
mov    $0x1,%al
mov    %eax,0x4(%rsp)
lea    0xd90(%rip),%r12
mov    0x2d1c(%rip),%r15
lea    0xdc7(%rip),%r13
movl   $0x0,0xc(%rsp)
movl   $0x0,(%rsp)
jmp    12dc <main+0x7c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1140 <getopt@plt>
cmp    $0x63,%eax
jg     1310 <main+0xb0>
cmp    $0xffffffff,%eax
je     1335 <main+0xd5>
cmp    $0x63,%eax
jne    12d0 <main+0x70>
mov    (%r15),%rdi
call   1150 <atoi@plt>
mov    %eax,%r14d
test   %eax,%eax
jg     12dc <main+0x7c>
jmp    16a4 <main+0x444>
nopl   0x0(%rax)
cmp    $0x76,%eax
je     132c <main+0xcc>
cmp    $0x64,%eax
jne    12d0 <main+0x70>
movl   $0x1,0xc(%rsp)
movl   $0x0,0x4(%rsp)
jmp    12dc <main+0x7c>
movl   $0x1,(%rsp)
jmp    12dc <main+0x7c>
mov    0x2c9c(%rip),%r12
mov    (%r12),%rax
mov    0x0(%rbp,%rax,8),%rdi
call   1090 <strlen@plt>
cmp    $0xff,%eax
jl     1372 <main+0x112>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
mov    $0xff,%edx
xor    %eax,%eax
call   1100 <fprintf@plt>
jmp    1658 <main+0x3f8>
mov    (%r12),%rax
mov    0x0(%rbp,%rax,8),%rsi
lea    0x30(%rsp),%rdi
call   1050 <strcpy@plt>
mov    (%r12),%rax
mov    0x8(%rbp,%rax,8),%rdi
call   1150 <atoi@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
jle    1641 <main+0x3e1>
lea    0x30(%rsp),%rdi
call   10f0 <gethostbyname@plt>
test   %rax,%rax
je     16b6 <main+0x456>
mov    %rax,%rbx
cmpl   $0x0,(%rsp)
je     13e0 <main+0x180>
mov    (%rbx),%r15
mov    0x10(%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1080 <inet_ntoa@plt>
lea    0xd66(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10b0 <printf@plt>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
lea    0x28(%rsp),%rdi
call   1110 <memcpy@plt>
lea    0x18(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x10,%edx
call   10c0 <memset@plt>
mov    0x28(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    0x8(%rsp),%edi
call   10a0 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    0x2bc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x400,%edi
call   1120 <malloc@plt>
mov    %rax,%rdi
mov    (%r12),%rax
mov    0x10(%rbp,%rax,8),%rdx
lea    0xd07(%rip),%rsi
lea    0x30(%rsp),%rcx
mov    %rdi,0x10(%rsp)
mov    %rcx,%r8
call   1160 <sprintf@plt>
test   %r14d,%r14d
jle    165d <main+0x3fd>
lea    0xe72(%rip),%rbp
xor    %r12d,%r12d
mov    0x4(%rsp),%r15d
xchg   %ax,%ax
mov    0x2b71(%rip),%rax
mov    (%rax),%edi
mov    0x2b38(%rip),%rax
mov    (%rax),%esi
mov    0x2b57(%rip),%rax
mov    (%rax),%edx
call   1180 <socket@plt>
cmp    $0xffffffff,%eax
je     16e3 <main+0x483>
mov    %eax,%r13d
cmpl   $0x0,(%rsp)
je     14b9 <main+0x259>
lea    0xd6f(%rip),%rdi
lea    0x30(%rsp),%rsi
mov    0x8(%rsp),%edx
xor    %eax,%eax
call   10b0 <printf@plt>
mov    %r13d,%edi
lea    0x18(%rsp),%rsi
mov    $0x10,%edx
call   1170 <connect@plt>
cmp    $0xffffffff,%eax
je     170a <main+0x4aa>
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <write@plt>
test   $0x1,%r15b
jne    1507 <main+0x2a7>
lea    0xd73(%rip),%rdi
mov    0x10(%rsp),%rsi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    %r13d,%edi
mov    0xc(%rsp),%esi
call   1040 <readHeaders@plt>
mov    %eax,%ebx
cmpl   $0x0,(%rsp)
je     1540 <main+0x2e0>
lea    0xd59(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
test   %ebx,%ebx
jns    1544 <main+0x2e4>
lea    0xdfa(%rip),%rdi
call   1060 <puts@plt>
jmp    1590 <main+0x330>
nopl   (%rax)
test   %ebx,%ebx
js     1590 <main+0x330>
mov    %ebx,%edi
add    $0x1,%edi
call   1120 <malloc@plt>
mov    %rax,%rbp
mov    %r13d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   10e0 <read@plt>
cmp    $0xffffffff,%eax
je     1671 <main+0x411>
cmp    $0x1,%r14d
jne    15ff <main+0x39f>
cltq
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rdi
call   1060 <puts@plt>
jmp    15ff <main+0x39f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x200,%edi
call   1120 <malloc@plt>
mov    %rax,%rbx
jmp    15a9 <main+0x349>
nop
cmp    $0x1ff,%r15d
jle    15f0 <main+0x390>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   10e0 <read@plt>
test   %eax,%eax
je     15f0 <main+0x390>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     1671 <main+0x411>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
cmp    $0x1,%r14d
jne    15a0 <main+0x340>
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   10b0 <printf@plt>
jmp    15a0 <main+0x340>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x4(%rsp),%r15d
mov    %r13d,%edi
call   10d0 <close@plt>
cmp    $0xffffffff,%eax
je     1734 <main+0x4d4>
add    $0x1,%r12d
cmp    %r14d,%r12d
lea    0xcba(%rip),%rbp
jne    1470 <main+0x210>
xor    %ebx,%ebx
cmp    $0x2,%r14d
jb     165d <main+0x3fd>
lea    0xce7(%rip),%rdi
xor    %ebx,%ebx
mov    %r14d,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
jmp    165d <main+0x3fd>
mov    0x2978(%rip),%rax
mov    (%rax),%edi
lea    0xa92(%rip),%rsi
xor    %eax,%eax
call   1100 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc19(%rip),%rdi
call   1130 <perror@plt>
mov    0x293c(%rip),%rax
mov    (%rax),%edi
mov    0x2923(%rip),%rax
mov    (%rax),%edx
lea    0xc17(%rip),%rsi
xor    %eax,%eax
call   1100 <fprintf@plt>
mov    $0x2,%ebx
jmp    165d <main+0x3fd>
mov    0x2915(%rip),%rax
mov    (%rax),%edi
lea    0x986(%rip),%rsi
jmp    1651 <main+0x3f1>
lea    0xa4c(%rip),%rdi
call   1130 <perror@plt>
mov    0x28f7(%rip),%rax
mov    (%rax),%edi
lea    0xa4a(%rip),%rsi
lea    0x30(%rsp),%rdx
xor    %eax,%eax
call   1100 <fprintf@plt>
jmp    1658 <main+0x3f8>
lea    0xaec(%rip),%rdi
call   1130 <perror@plt>
mov    0x28ca(%rip),%rax
mov    (%rax),%edi
mov    0x28b1(%rip),%rax
mov    (%rax),%edx
lea    0xae7(%rip),%rsi
jmp    1696 <main+0x436>
lea    0xb25(%rip),%rdi
call   1130 <perror@plt>
mov    0x28a3(%rip),%rax
mov    (%rax),%edi
mov    0x288a(%rip),%rax
mov    (%rax),%edx
lea    0xb18(%rip),%rsi
jmp    1696 <main+0x436>
lea    0xba0(%rip),%rdi
call   1130 <perror@plt>
mov    0x2879(%rip),%rax
mov    (%rax),%edi
mov    0x2860(%rip),%rax
mov    (%rax),%edx
lea    0xb99(%rip),%rsi
jmp    1696 <main+0x436>

LLVM IR (-O0):
%struct.hostent = type { i8*, i32, i32, i64* }
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [53 x i8] c"usage: %s [-d] [-c COUNT] <host-name> <port> <file>\0A\00"
@.str.1 = [5 x i8] c"c:dv\00"
@optarg = external global i8*
@stderr = global i32 0
@.str.2 = [64 x i8] c"Error: Times to download (-c COUNT) must be a positive number.\0A\00"
@.str.3 = [38 x i8] c"Option -%c not recognized. Ignoring.\0A\00"
@optind = global i64 0
@.str.4 = [67 x i8] c"Error: Host name too large. Must be less than %d characters long.\0A\00"
@.str.5 = [38 x i8] c"Error: Invalid port number provided.\0A\00"
@.str.6 = [19 x i8] c"DNS Lookup Failure\00"
@.str.7 = [29 x i8] c"Error: No such hostname: %s\0A\00"
@.str.8 = [18 x i8] c"--Host: %s -> %s\0A\00"
@AF_INET = global i32 0
@.str.9 = [139 x i8] c"GET %s HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0AAccept: text/html, text/plain, */*\0D\0AUser-Agent: CS360Downloader/1.0\0D\0AReferer: http://%s/\0D\0A\0D\0A\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.10 = [25 x i8] c"Failure to create socket\00"
@.str.11 = [41 x i8] c"Error: Could not create connection (%d)\0A\00"
@errno = global i32 0
@.str.12 = [30 x i8] c"--Connecting to http://%s:%d\0A\00"
@.str.13 = [17 x i8] c"Connection error\00"
@.str.14 = [39 x i8] c"Error: Could not connect to host (%d)\0A\00"
@.str.15 = [13 x i8] c"Request:\0A%s\0A\00"
@.str.16 = [22 x i8] c"--Content Length: %d\0A\00"
@.str.17 = [28 x i8] c"Failure reading from socket\00"
@.str.18 = [43 x i8] c"Error: Problem reading response body (%d)\0A\00"
@.str.19 = [4 x i8] c"%s\0A\00"
@.str.20 = [37 x i8] c"--Reading to the end of the socket.\0A\00"
@.str.21 = [3 x i8] c"%s\00"
@.str.22 = [2 x i8] c"\0A\00"
@.str.23 = [23 x i8] c"Failed to close socket\00"
@.str.24 = [40 x i8] c"Error: Could not close connection (%d)\0A\00"
@.str.25 = [22 x i8] c"Downloaded %d times.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [255 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %17
store i32 0, i32* %18
store i32 1, i32* %19
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 4
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %26)
store i32 1, i32* %3
br label %275
28: ; preds = %2
br label %29
29: ; preds = %50, %28
%30 = load i32, i32* %4
%31 = load i8**, i8*** %5
%32 = call i32 @getopt(i32 %30, i8** %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %32, i32* %15
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %51
34: ; preds = %29
%35 = load i32, i32* %15
switch i32 %35, label %47 [
i32 99, label %36
i32 100, label %45
i32 118, label %46
]
36: ; preds = %34
%37 = load i8*, i8** @optarg
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %19
%39 = load i32, i32* %19
%40 = icmp sle i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %275
44: ; preds = %36
br label %50
45: ; preds = %34
store i32 1, i32* %17
br label %50
46: ; preds = %34
store i32 1, i32* %18
br label %50
47: ; preds = %34
%48 = load i32, i32* %15
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %47, %46, %45, %44
br label %29
51: ; preds = %29
%52 = load i8**, i8*** %5
%53 = load i64, i64* @optind
%54 = getelementptr inbounds i8*, i8** %52, i64 %53
%55 = load i8*, i8** %54
%56 = call i32 @strlen(i8* %55)
%57 = icmp sge i32 %56, 255
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.4, i64 0, i64 0), i32 255)
store i32 1, i32* %3
br label %275
61: ; preds = %51
%62 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%63 = load i8**, i8*** %5
%64 = load i64, i64* @optind
%65 = getelementptr inbounds i8*, i8** %63, i64 %64
%66 = load i8*, i8** %65
%67 = call i32 @strcpy(i8* %62, i8* %66)
%68 = load i8**, i8*** %5
%69 = load i64, i64* @optind
%70 = add i64 %69, 1
%71 = getelementptr inbounds i8*, i8** %68, i64 %70
%72 = load i8*, i8** %71
%73 = call i32 @atoi(i8* %72)
store i32 %73, i32* %16
%74 = load i32, i32* %16
%75 = icmp sle i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %61
%77 = load i32, i32* @stderr
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %275
79: ; preds = %61
%80 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%81 = call %struct.hostent* @gethostbyname(i8* %80)
store %struct.hostent* %81, %struct.hostent** %7
%82 = load %struct.hostent*, %struct.hostent** %7
%83 = icmp eq %struct.hostent* %82, null
br i1 %83, label %84, label %89
84: ; preds = %79
%85 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%86 = load i32, i32* @stderr
%87 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %87)
store i32 1, i32* %3
br label %275
89: ; preds = %79
%90 = load i32, i32* %18
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %106
92: ; preds = %89
%93 = load %struct.hostent*, %struct.hostent** %7
%94 = getelementptr inbounds %struct.hostent, %struct.hostent* %93, i32 0, i32 0
%95 = load i8*, i8** %94
%96 = load %struct.hostent*, %struct.hostent** %7
%97 = getelementptr inbounds %struct.hostent, %struct.hostent* %96, i32 0, i32 3
%98 = load i64*, i64** %97
%99 = getelementptr inbounds i64, i64* %98, i64 0
%100 = load i64, i64* %99
%101 = inttoptr i64 %100 to %struct.in_addr*
%102 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %101, i32 0, i32 0
%103 = load i32, i32* %102
%104 = call i8* @inet_ntoa(i32 %103)
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %95, i8* %104)
br label %106
106: ; preds = %92, %89
%107 = load %struct.hostent*, %struct.hostent** %7
%108 = getelementptr inbounds %struct.hostent, %struct.hostent* %107, i32 0, i32 2
%109 = load i32, i32* %108
%110 = load %struct.hostent*, %struct.hostent** %7
%111 = getelementptr inbounds %struct.hostent, %struct.hostent* %110, i32 0, i32 1
%112 = load i32, i32* %111
%113 = call i32 @memcpy(i64* %9, i32 %109, i32 %112)
%114 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 16)
%115 = load i64, i64* %9
%116 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%117 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %116, i32 0, i32 0
store i64 %115, i64* %117
%118 = load i32, i32* %16
%119 = call i32 @htons(i32 %118)
%120 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %119, i32* %120
%121 = load i32, i32* @AF_INET
%122 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %121, i32* %122
%123 = call i8* @malloc(i32 1024)
store i8* %123, i8** %20
%124 = load i8*, i8** %20
%125 = load i8**, i8*** %5
%126 = load i64, i64* @optind
%127 = add i64 %126, 2
%128 = getelementptr inbounds i8*, i8** %125, i64 %127
%129 = load i8*, i8** %128
%130 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%131 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%132 = call i32 @sprintf(i8* %124, i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str.9, i64 0, i64 0), i8* %129, i8* %130, i8* %131)
store i32 0, i32* %14
br label %133
133: ; preds = %265, %106
%134 = load i32, i32* %14
%135 = load i32, i32* %19
%136 = icmp slt i32 %134, %135
br i1 %136, label %137, label %268
137: ; preds = %133
%138 = load i32, i32* @AF_INET
%139 = load i32, i32* @SOCK_STREAM
%140 = load i32, i32* @IPPROTO_TCP
%141 = call i32 @socket(i32 %138, i32 %139, i32 %140)
store i32 %141, i32* %6
%142 = load i32, i32* %6
%143 = icmp eq i32 %142, -1
br i1 %143, label %144, label %149
144: ; preds = %137
%145 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
%146 = load i32, i32* @stderr
%147 = load i32, i32* @errno
%148 = call i32 (i32, i8*, ...) @fprintf(i32 %146, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.11, i64 0, i64 0), i32 %147)
store i32 2, i32* %3
br label %275
149: ; preds = %137
%150 = load i32, i32* %18
%151 = icmp ne i32 %150, 0
br i1 %151, label %152, label %156
152: ; preds = %149
%153 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%154 = load i32, i32* %16
%155 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0), i8* %153, i32 %154)
br label %156
156: ; preds = %152, %149
%157 = load i32, i32* %6
%158 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%159 = call i32 @connect(i32 %157, %struct.sockaddr* %158, i32 16)
%160 = icmp eq i32 %159, -1
br i1 %160, label %161, label %166
161: ; preds = %156
%162 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i64 0, i64 0))
%163 = load i32, i32* @stderr
%164 = load i32, i32* @errno
%165 = call i32 (i32, i8*, ...) @fprintf(i32 %163, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.14, i64 0, i64 0), i32 %164)
store i32 2, i32* %3
br label %275
166: ; preds = %156
%167 = load i32, i32* %6
%168 = load i8*, i8** %20
%169 = load i8*, i8** %20
%170 = call i32 @strlen(i8* %169)
%171 = call i32 @write(i32 %167, i8* %168, i32 %170)
%172 = load i32, i32* %17
%173 = icmp ne i32 %172, 0
br i1 %173, label %174, label %177
174: ; preds = %166
%175 = load i8*, i8** %20
%176 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.15, i64 0, i64 0), i8* %175)
br label %177
177: ; preds = %174, %166
%178 = load i32, i32* %6
%179 = load i32, i32* %17
%180 = call i32 @readHeaders(i32 %178, i32 %179)
store i32 %180, i32* %13
%181 = load i32, i32* %18
%182 = icmp ne i32 %181, 0
br i1 %182, label %183, label %186
183: ; preds = %177
%184 = load i32, i32* %13
%185 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.16, i64 0, i64 0), i32 %184)
br label %186
186: ; preds = %183, %177
%187 = load i32, i32* %13
%188 = icmp sge i32 %187, 0
br i1 %188, label %189, label %218
189: ; preds = %186
%190 = load i32, i32* %13
%191 = add nsw i32 %190, 1
%192 = sext i32 %191 to i64
%193 = mul i64 %192, 1
%194 = trunc i64 %193 to i32
%195 = call i8* @malloc(i32 %194)
store i8* %195, i8** %10
%196 = load i32, i32* %6
%197 = load i8*, i8** %10
%198 = load i32, i32* %13
%199 = call i32 @read(i32 %196, i8* %197, i32 %198)
store i32 %199, i32* %11
%200 = load i32, i32* %11
%201 = icmp eq i32 %200, -1
br i1 %201, label %202, label %207
202: ; preds = %189
%203 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.17, i64 0, i64 0))
%204 = load i32, i32* @stderr
%205 = load i32, i32* @errno
%206 = call i32 (i32, i8*, ...) @fprintf(i32 %204, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.18, i64 0, i64 0), i32 %205)
store i32 2, i32* %3
br label %275
207: ; preds = %189
%208 = load i32, i32* %19
%209 = icmp eq i32 %208, 1
br i1 %209, label %210, label %217
210: ; preds = %207
%211 = load i8*, i8** %10
%212 = load i32, i32* %11
%213 = sext i32 %212 to i64
%214 = getelementptr inbounds i8, i8* %211, i64 %213
store i8 0, i8* %214
%215 = load i8*, i8** %10
%216 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.19, i64 0, i64 0), i8* %215)
br label %217
217: ; preds = %210, %207
br label %255
218: ; preds = %186
%219 = load i32, i32* %18
%220 = icmp ne i32 %219, 0
br i1 %220, label %221, label %223
221: ; preds = %218
%222 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.20, i64 0, i64 0))
br label %223
223: ; preds = %221, %218
%224 = call i8* @malloc(i32 512)
store i8* %224, i8** %10
br label %225
225: ; preds = %252, %223
%226 = load i32, i32* %6
%227 = load i8*, i8** %10
%228 = call i32 @read(i32 %226, i8* %227, i32 512)
store i32 %228, i32* %11
%229 = icmp ne i32 %228, 0
br i1 %229, label %230, label %253
230: ; preds = %225
%231 = load i32, i32* %11
%232 = icmp eq i32 %231, -1
br i1 %232, label %233, label %238
233: ; preds = %230
%234 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.17, i64 0, i64 0))
%235 = load i32, i32* @stderr
%236 = load i32, i32* @errno
%237 = call i32 (i32, i8*, ...) @fprintf(i32 %235, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.18, i64 0, i64 0), i32 %236)
store i32 2, i32* %3
br label %275
238: ; preds = %230
%239 = load i8*, i8** %10
%240 = load i32, i32* %11
%241 = sext i32 %240 to i64
%242 = getelementptr inbounds i8, i8* %239, i64 %241
store i8 0, i8* %242
%243 = load i32, i32* %19
%244 = icmp eq i32 %243, 1
br i1 %244, label %245, label %248
245: ; preds = %238
%246 = load i8*, i8** %10
%247 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.21, i64 0, i64 0), i8* %246)
br label %248
248: ; preds = %245, %238
%249 = load i32, i32* %11
%250 = icmp slt i32 %249, 512
br i1 %250, label %251, label %252
251: ; preds = %248
br label %253
252: ; preds = %248
br label %225
253: ; preds = %251, %225
%254 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.22, i64 0, i64 0))
br label %255
255: ; preds = %253, %217
%256 = load i32, i32* %6
%257 = call i32 @close(i32 %256)
%258 = icmp eq i32 %257, -1
br i1 %258, label %259, label %264
259: ; preds = %255
%260 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.23, i64 0, i64 0))
%261 = load i32, i32* @stderr
%262 = load i32, i32* @errno
%263 = call i32 (i32, i8*, ...) @fprintf(i32 %261, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.24, i64 0, i64 0), i32 %262)
store i32 2, i32* %3
br label %275
264: ; preds = %255
br label %265
265: ; preds = %264
%266 = load i32, i32* %14
%267 = add nsw i32 %266, 1
store i32 %267, i32* %14
br label %133
268: ; preds = %133
%269 = load i32, i32* %14
%270 = icmp sgt i32 %269, 1
br i1 %270, label %271, label %274
271: ; preds = %268
%272 = load i32, i32* %14
%273 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.25, i64 0, i64 0), i32 %272)
br label %274
274: ; preds = %271, %268
store i32 0, i32* %3
br label %275
275: ; preds = %274, %259, %233, %202, %161, %144, %84, %76, %58, %41, %23
%276 = load i32, i32* %3
ret i32 %276
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i8*, i32, i32, i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [53 x i8] c"usage: %s [-d] [-c COUNT] <host-name> <port> <file>\0A\00"
@.str.1 = [5 x i8] c"c:dv\00"
@optarg = external global i8*
@stderr = global i32 0
@.str.2 = [64 x i8] c"Error: Times to download (-c COUNT) must be a positive number.\0A\00"
@.str.3 = [38 x i8] c"Option -%c not recognized. Ignoring.\0A\00"
@optind = global i64 0
@.str.4 = [67 x i8] c"Error: Host name too large. Must be less than %d characters long.\0A\00"
@.str.5 = [38 x i8] c"Error: Invalid port number provided.\0A\00"
@.str.6 = [19 x i8] c"DNS Lookup Failure\00"
@.str.7 = [29 x i8] c"Error: No such hostname: %s\0A\00"
@.str.8 = [18 x i8] c"--Host: %s -> %s\0A\00"
@AF_INET = global i32 0
@.str.9 = [139 x i8] c"GET %s HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0AAccept: text/html, text/plain, */*\0D\0AUser-Agent: CS360Downloader/1.0\0D\0AReferer: http://%s/\0D\0A\0D\0A\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.10 = [25 x i8] c"Failure to create socket\00"
@.str.11 = [41 x i8] c"Error: Could not create connection (%d)\0A\00"
@errno = global i32 0
@.str.12 = [30 x i8] c"--Connecting to http://%s:%d\0A\00"
@.str.13 = [17 x i8] c"Connection error\00"
@.str.14 = [39 x i8] c"Error: Could not connect to host (%d)\0A\00"
@.str.15 = [13 x i8] c"Request:\0A%s\0A\00"
@.str.16 = [22 x i8] c"--Content Length: %d\0A\00"
@.str.17 = [28 x i8] c"Failure reading from socket\00"
@.str.18 = [43 x i8] c"Error: Problem reading response body (%d)\0A\00"
@.str.21 = [3 x i8] c"%s\00"
@.str.23 = [23 x i8] c"Failed to close socket\00"
@.str.24 = [40 x i8] c"Error: Could not close connection (%d)\0A\00"
@.str.25 = [22 x i8] c"Downloaded %d times.\0A\00"
@str = [36 x i8] c"--Reading to the end of the socket.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = alloca [255 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i64* %4 to i8*
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 4
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %185
13: ; preds = %30, %32
%14 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
switch i32 %14, label %32 [
i32 -1, label %34
i32 99, label %20
i32 100, label %15
i32 118, label %27
]
15: ; preds = %13, %2
%16 = phi i1 [ true, %2 ], [ false, %13 ]
%17 = phi i32 [ 0, %2 ], [ 1, %13 ]
%18 = phi i32 [ 0, %2 ], [ %28, %13 ]
%19 = phi i32 [ 1, %2 ], [ %31, %13 ]
br label %27
20: ; preds = %13
%21 = load i8*, i8** @optarg
%22 = tail call i32 @atoi(i8* %21)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
br label %185
27: ; preds = %13, %15
%28 = phi i32 [ %18, %15 ], [ 1, %13 ]
%29 = phi i32 [ %19, %15 ], [ %31, %13 ]
br label %30
30: ; preds = %27, %20
%31 = phi i32 [ %29, %27 ], [ %22, %20 ]
br label %13
32: ; preds = %13
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %13
34: ; preds = %13
%35 = load i64, i64* @optind
%36 = getelementptr inbounds i8*, i8** %1, i64 %35
%37 = load i8*, i8** %36
%38 = tail call i32 @strlen(i8* %37)
%39 = icmp sgt i32 %38, 254
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.4, i64 0, i64 0), i32 255)
br label %185
43: ; preds = %34
%44 = load i64, i64* @optind
%45 = getelementptr inbounds i8*, i8** %1, i64 %44
%46 = load i8*, i8** %45
%47 = call i32 @strcpy(i8* %8, i8* %46)
%48 = load i64, i64* @optind
%49 = add i64 %48, 1
%50 = getelementptr inbounds i8*, i8** %1, i64 %49
%51 = load i8*, i8** %50
%52 = call i32 @atoi(i8* %51)
%53 = icmp slt i32 %52, 1
br i1 %53, label %54, label %57
54: ; preds = %43
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %185
57: ; preds = %43
%58 = call %struct.hostent* @gethostbyname(i8* %8)
%59 = icmp eq %struct.hostent* %58, null
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %8)
br label %185
64: ; preds = %57
%65 = icmp eq i32 %28, 0
br i1 %65, label %77, label %66
66: ; preds = %64
%67 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i64 0, i32 0
%68 = load i8*, i8** %67
%69 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i64 0, i32 3
%70 = load i64*, i64** %69
%71 = load i64, i64* %70
%72 = inttoptr i64 %71 to %struct.in_addr*
%73 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %72, i64 0, i32 0
%74 = load i32, i32* %73
%75 = call i8* @inet_ntoa(i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %68, i8* %75)
br label %77
77: ; preds = %66, %64
%78 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i64 0, i32 2
%79 = load i32, i32* %78
%80 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i64 0, i32 1
%81 = load i32, i32* %80
%82 = call i32 @memcpy(i64* %4, i32 %79, i32 %81)
%83 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 16)
%84 = load i64, i64* %4
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i64 %84, i64* %85
%86 = call i32 @htons(i32 %52)
%87 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %86, i32* %87
%88 = load i32, i32* @AF_INET
%89 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %88, i32* %89
%90 = call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%91 = load i64, i64* @optind
%92 = add i64 %91, 2
%93 = getelementptr inbounds i8*, i8** %1, i64 %92
%94 = load i8*, i8** %93
%95 = call i32 @sprintf(i8* dereferenceable(1) %90, i8* dereferenceable(1) getelementptr inbounds ([139 x i8], [139 x i8]* @.str.9, i64 0, i64 0), i8* %94, i8* %8, i8* %8)
%96 = icmp sgt i32 %31, 0
br i1 %96, label %97, label %185
97: ; preds = %77
%98 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%99 = icmp eq i32 %31, 1
br label %100
100: ; preds = %97, %178
%101 = phi i32 [ 0, %97 ], [ %179, %178 ]
%102 = load i32, i32* @AF_INET
%103 = load i32, i32* @SOCK_STREAM
%104 = load i32, i32* @IPPROTO_TCP
%105 = call i32 @socket(i32 %102, i32 %103, i32 %104)
%106 = icmp eq i32 %105, -1
br i1 %106, label %107, label %112
107: ; preds = %100
%108 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
%109 = load i32, i32* @stderr
%110 = load i32, i32* @errno
%111 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.11, i64 0, i64 0), i32 %110)
br label %185
112: ; preds = %100
br i1 %65, label %115, label %113
113: ; preds = %112
%114 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0), i8* %8, i32 %52)
br label %115
115: ; preds = %113, %112
%116 = call i32 @connect(i32 %105, %struct.sockaddr* %98, i32 16)
%117 = icmp eq i32 %116, -1
br i1 %117, label %118, label %123
118: ; preds = %115
%119 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i64 0, i64 0))
%120 = load i32, i32* @stderr
%121 = load i32, i32* @errno
%122 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.14, i64 0, i64 0), i32 %121)
br label %185
123: ; preds = %115
%124 = call i32 @strlen(i8* %90)
%125 = call i32 @write(i32 %105, i8* %90, i32 %124)
br i1 %16, label %128, label %126
126: ; preds = %123
%127 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.15, i64 0, i64 0), i8* %90)
br label %128
128: ; preds = %126, %123
%129 = call i32 @readHeaders(i32 %105, i32 %17)
br i1 %65, label %130, label %132
130: ; preds = %128
%131 = icmp sgt i32 %129, -1
br i1 %131, label %135, label %152
132: ; preds = %128
%133 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.16, i64 0, i64 0), i32 %129)
%134 = icmp sgt i32 %129, -1
br i1 %134, label %135, label %150
135: ; preds = %132, %130
%136 = add nuw nsw i32 %129, 1
%137 = call i8* @malloc(i32 %136)
%138 = call i32 @read(i32 %105, i8* %137, i32 %129)
%139 = icmp eq i32 %138, -1
br i1 %139, label %140, label %145
140: ; preds = %135
%141 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.17, i64 0, i64 0))
%142 = load i32, i32* @stderr
%143 = load i32, i32* @errno
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %142, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.18, i64 0, i64 0), i32 %143)
br label %185
145: ; preds = %135
br i1 %99, label %146, label %170
146: ; preds = %145
%147 = sext i32 %138 to i64
%148 = getelementptr inbounds i8, i8* %137, i64 %147
store i8 0, i8* %148
%149 = call i32 @puts(i8* dereferenceable(1) %137)
br label %170
150: ; preds = %132
%151 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %152
152: ; preds = %130, %150
%153 = call dereferenceable_or_null(512) i8* @malloc(i32 512)
br label %154
154: ; preds = %166, %152
%155 = call i32 @read(i32 %105, i8* %153, i32 512)
switch i32 %155, label %161 [
i32 0, label %168
i32 -1, label %156
]
156: ; preds = %154
%157 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.17, i64 0, i64 0))
%158 = load i32, i32* @stderr
%159 = load i32, i32* @errno
%160 = call i32 (i32, i8*, ...) @fprintf(i32 %158, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.18, i64 0, i64 0), i32 %159)
br label %185
161: ; preds = %154
%162 = sext i32 %155 to i64
%163 = getelementptr inbounds i8, i8* %153, i64 %162
store i8 0, i8* %163
br i1 %99, label %164, label %166
164: ; preds = %161
%165 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.21, i64 0, i64 0), i8* %153)
br label %166
166: ; preds = %164, %161
%167 = icmp slt i32 %155, 512
br i1 %167, label %168, label %154
168: ; preds = %154, %166
%169 = call i32 @putchar(i32 10)
br label %170
170: ; preds = %145, %146, %168
%171 = call i32 @close(i32 %105)
%172 = icmp eq i32 %171, -1
br i1 %172, label %173, label %178
173: ; preds = %170
%174 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.23, i64 0, i64 0))
%175 = load i32, i32* @stderr
%176 = load i32, i32* @errno
%177 = call i32 (i32, i8*, ...) @fprintf(i32 %175, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.24, i64 0, i64 0), i32 %176)
br label %185
178: ; preds = %170
%179 = add nuw nsw i32 %101, 1
%180 = icmp eq i32 %179, %31
br i1 %180, label %181, label %100
181: ; preds = %178
%182 = icmp ugt i32 %31, 1
br i1 %182, label %183, label %185
183: ; preds = %181
%184 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.25, i64 0, i64 0), i32 %31)
br label %185
185: ; preds = %77, %107, %118, %140, %156, %173, %183, %181, %60, %54, %40, %24, %10
%186 = phi i32 [ 1, %10 ], [ 1, %24 ], [ 1, %40 ], [ 1, %54 ], [ 1, %60 ], [ 2, %107 ], [ 2, %118 ], [ 2, %140 ], [ 2, %173 ], [ 2, %156 ], [ 0, %183 ], [ 0, %181 ], [ 0, %77 ]
ret i32 %186
}