#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 */

/* Variables and functions */
scalar_t__ EINVAL;
int create_socket();
scalar_t__ errno;
scalar_t__ print_tcp_response(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ send_tcp_message(int, char *);
int /*<<< orphan*/ sprintf(char *, char const *, char *);
int const strlen(char const *);
int strtol(char *, int /*<<< orphan*/ *, int);
scalar_t__ tcp_connect(int, char *, int);
int main(int argc, char **ARGV) {
  int sockfd = 0;
  int port = 0;
  char *hostname = NULL;
  char *path = NULL;
  const int HTTP_COMMAND_LENGTH = 1204;
  char http_command[HTTP_COMMAND_LENGTH];
  const char *HTTP_COMMAND_TEMPLATE = "GET %s HTTP/1.0\n\n";
  const int MAX_PATH_SIZE =
      HTTP_COMMAND_LENGTH - strlen(HTTP_COMMAND_TEMPLATE) - 2;
  if (argc < 4) {
    printf("Usage:\nhttp_client <hostname|ip_address> <port> <path>");
    return 1;
  }
  hostname = ARGV[1];
  port = strtol(ARGV[2], NULL, 10);
  if (port == 0 && errno == EINVAL) {
    printf("Error: invalid port specified");
    return 1;
  }
  path = ARGV[3];
  if (strlen(path) > MAX_PATH_SIZE) {
    printf("Error: path too long");
    return 1;
  }
  if ((sockfd = create_socket()) < 0) {
    printf("Error: Could not create socket\n");
    return 1;
  }
  if (tcp_connect(sockfd, hostname, port) < 0) {
    printf("Error: Connect Failed\n");
    return 1;
  }
  sprintf(http_command, HTTP_COMMAND_TEMPLATE, path);
  if (send_tcp_message(sockfd, http_command) < 0) {
    printf("Error: Message Send Failed\n");
  }
  if (print_tcp_response(sockfd) < 0) {
    printf("Error: Message Read Failed\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x500,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x4b4,-0x2c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
lea    0xe34(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    $0x4b4,%eax
sub    %ecx,%eax
sub    $0x2,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1211 <main+0x91>
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1385 <main+0x205>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1279 <main+0xf9>
mov    0x2d99(%rip),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    1279 <main+0xf9>
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1385 <main+0x205>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x44(%rbp),%eax
jle    12b6 <main+0x136>
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1385 <main+0x205>
mov    $0x0,%al
call   1070 <create_socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12e8 <main+0x168>
lea    0xdac(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1385 <main+0x205>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <tcp_connect@plt>
cmp    $0x0,%rax
jge    1320 <main+0x1a0>
lea    0xd94(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1385 <main+0x205>
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x500(%rbp),%rdi
call   1080 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x500(%rbp),%rsi
call   10a0 <send_tcp_message@plt>
cmp    $0x0,%rax
jge    1359 <main+0x1d9>
lea    0xd5f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <print_tcp_response@plt>
cmp    $0x0,%rax
jge    1377 <main+0x1f7>
lea    0xd5d(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x500,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4c0,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xe55(%rip),%rdi
call   1050 <strlen@plt>
cmp    $0x3,%ebx
jg     11be <main+0x2e>
lea    0xe56(%rip),%rdi
jmp    121a <main+0x8a>
mov    %eax,%r15d
mov    0x8(%rbp),%r14
mov    0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%r12d
test   %eax,%eax
jne    11fb <main+0x6b>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    11fb <main+0x6b>
lea    0xe50(%rip),%rdi
jmp    121a <main+0x8a>
mov    $0x4b2,%ebx
sub    %r15d,%ebx
mov    0x18(%rbp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    %ebx,%eax
jle    1236 <main+0xa6>
lea    0xe4d(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x4c0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1080 <create_socket@plt>
test   %eax,%eax
js     12a4 <main+0x114>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <tcp_connect@plt>
test   %rax,%rax
js     12ad <main+0x11d>
lea    0xda3(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1090 <sprintf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10b0 <send_tcp_message@plt>
test   %rax,%rax
jns    1287 <main+0xf7>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   10a0 <print_tcp_response@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    1224 <main+0x94>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
jmp    1224 <main+0x94>
lea    0xe1d(%rip),%rdi
jmp    12b4 <main+0x124>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
jmp    121f <main+0x8f>

LLVM IR (-O0):
@.str = [18 x i8] c"GET %s HTTP/1.0\0A\0A\00"
@.str.1 = [55 x i8] c"Usage:\0Ahttp_client <hostname|ip_address> <port> <path>\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [30 x i8] c"Error: invalid port specified\00"
@.str.3 = [21 x i8] c"Error: path too long\00"
@.str.4 = [32 x i8] c"Error: Could not create socket\0A\00"
@.str.5 = [23 x i8] c"Error: Connect Failed\0A\00"
@.str.6 = [28 x i8] c"Error: Message Send Failed\0A\00"
@.str.7 = [28 x i8] c"Error: Message Read Failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
store i8* null, i8** %9
store i32 1204, i32* %10
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %11
%16 = alloca i8, i64 1204
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %12
%17 = load i8*, i8** %12
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 1204, %18
%20 = sub nsw i32 %19, 2
store i32 %20, i32* %13
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 4
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %80
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %8
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @strtol(i8* %31, i32* null, i32 10)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %25
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINVAL
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %80
41: ; preds = %35, %25
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 3
%44 = load i8*, i8** %43
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = call i32 @strlen(i8* %45)
%47 = load i32, i32* %13
%48 = icmp sgt i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %80
51: ; preds = %41
%52 = call i32 (...) @create_socket()
store i32 %52, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %80
56: ; preds = %51
%57 = load i32, i32* %6
%58 = load i8*, i8** %8
%59 = load i32, i32* %7
%60 = call i64 @tcp_connect(i32 %57, i8* %58, i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %80
64: ; preds = %56
%65 = load i8*, i8** %12
%66 = load i8*, i8** %9
%67 = call i32 @sprintf(i8* %16, i8* %65, i8* %66)
%68 = load i32, i32* %6
%69 = call i64 @send_tcp_message(i32 %68, i8* %16)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %64
%72 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %73
73: ; preds = %71, %64
%74 = load i32, i32* %6
%75 = call i64 @print_tcp_response(i32 %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %79
79: ; preds = %77, %73
store i32 0, i32* %3
store i32 1, i32* %14
br label %80
80: ; preds = %79, %62, %54, %49, %39, %23
%81 = load i8*, i8** %11
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str = [18 x i8] c"GET %s HTTP/1.0\0A\0A\00"
@.str.1 = [55 x i8] c"Usage:\0Ahttp_client <hostname|ip_address> <port> <path>\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [30 x i8] c"Error: invalid port specified\00"
@.str.3 = [21 x i8] c"Error: path too long\00"
@str = [27 x i8] c"Error: Message Read Failed\00"
@str.8 = [27 x i8] c"Error: Message Send Failed\00"
@str.9 = [22 x i8] c"Error: Connect Failed\00"
@str.10 = [31 x i8] c"Error: Could not create socket\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1204 x i8]
%4 = getelementptr inbounds [1204 x i8], [1204 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = sub i32 1202, %5
%7 = icmp slt i32 %0, 4
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
br label %51
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @strtol(i8* nocapture %14, i32* null, i32 10)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINVAL
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %51
23: ; preds = %17, %10
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i32 @strlen(i8* %25)
%27 = icmp sgt i32 %26, %6
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %51
30: ; preds = %23
%31 = tail call i32 (...) @create_socket()
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
br label %51
35: ; preds = %30
%36 = tail call i64 @tcp_connect(i32 %31, i8* %12, i32 %15)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0))
br label %51
40: ; preds = %35
%41 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
%42 = call i64 @send_tcp_message(i32 %31, i8* %4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = call i64 @print_tcp_response(i32 %31)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %51
51: ; preds = %46, %49, %38, %33, %28, %21, %8
%52 = phi i32 [ 1, %8 ], [ 1, %21 ], [ 1, %28 ], [ 1, %33 ], [ 1, %38 ], [ 0, %49 ], [ 0, %46 ]
ret i32 %52
}