#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serverAddress;
typedef int /*<<< orphan*/ readBuffer;
typedef int /*<<< orphan*/ message;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ write(int, char *, int);
int main(int argc, char *argv[]) {
  int socketfd, portNumber, length;
  char readBuffer[2000], message[255];
  struct sockaddr_in serverAddress;
  struct hostent *server;
  sprintf(message, "GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
          argv[1]);
  printf("Sending the message: %s", message);
  if (argc <= 1) {
    printf("Incorrect usage, use: ./webBrowser hostname\n");
    return 2;
  }
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    perror("Socket Client: error - unable to resolve host name.\n");
    return 1;
  }
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd < 0) {
    perror("Socket Client: error opening TCP IP-based socket.\n");
    return 1;
  }
  bzero((char *)&serverAddress, sizeof(serverAddress));
  portNumber = 80;
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_port = htons(portNumber);
  bcopy((char *)server->h_addr, (char *)&serverAddress.sin_addr.s_addr,
        server->h_length);
  if (connect(socketfd, (struct sockaddr *)&serverAddress,
              sizeof(serverAddress)) < 0) {
    perror("Socket Client: error connecting to the server.\n");
    return 1;
  }
  if (write(socketfd, message, sizeof(message)) < 0) {
    perror("Socket Client: error writing to socket");
    return 1;
  }
  if (read(socketfd, readBuffer, sizeof(readBuffer)) < 0) {
    perror("Socket Client: error reading from socket");
    return 1;
  }
  printf("**START**\n%s\n**END**\n", readBuffer);
  close(socketfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x910,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x8f0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe11(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x8f0(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jg     122d <main+0x6d>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13c0 <main+0x200>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x908(%rbp)
cmpq   $0x0,-0x908(%rbp)
jne    1267 <main+0xa7>
lea    0xe1f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c0 <main+0x200>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a5 <main+0xe5>
lea    0xe16(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c0 <main+0x200>
lea    -0x900(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
movl   $0x50,-0x18(%rbp)
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8f8(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x8fc(%rbp)
mov    -0x908(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x900(%rbp),%rsi
mov    -0x908(%rbp),%rax
mov    (%rax),%edx
call   1090 <bcopy@plt>
mov    -0x14(%rbp),%edi
lea    -0x900(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    1330 <main+0x170>
lea    0xdbe(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c0 <main+0x200>
mov    -0x14(%rbp),%edi
lea    -0x8f0(%rbp),%rsi
mov    $0xff,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1366 <main+0x1a6>
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c0 <main+0x200>
mov    -0x14(%rbp),%edi
lea    -0x7f0(%rbp),%rsi
mov    $0x7d0,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    139c <main+0x1dc>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c0 <main+0x200>
lea    -0x7f0(%rbp),%rsi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x910,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x8e8,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdx
lea    0xe13(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   10d0 <sprintf@plt>
lea    0xe2f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x1,%ebp
jg     1226 <main+0x56>
lea    0xf2f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
jmp    132b <main+0x15b>
mov    0x8(%rbx),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     12f6 <main+0x126>
mov    %rax,%rbx
mov    0x2da6(%rip),%r15
mov    (%r15),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12ff <main+0x12f>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbx),%rsi
mov    (%rbx),%edx
mov    %r14,%rdi
call   10a0 <memmove@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     1308 <main+0x138>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xff,%edx
call   1040 <write@plt>
test   %rax,%rax
js     1311 <main+0x141>
lea    0x110(%rsp),%rsi
mov    %ebp,%edi
mov    $0x7d0,%edx
call   1080 <read@plt>
test   %rax,%rax
js     131a <main+0x14a>
lea    0xe54(%rip),%rdi
xor    %ebx,%ebx
lea    0x110(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    132b <main+0x15b>
lea    0xd4b(%rip),%rdi
jmp    1321 <main+0x151>
lea    0xd77(%rip),%rdi
jmp    1321 <main+0x151>
lea    0xda1(%rip),%rdi
jmp    1321 <main+0x151>
lea    0xdc8(%rip),%rdi
jmp    1321 <main+0x151>
lea    0xde6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [48 x i8] c"GET / HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.1 = [24 x i8] c"Sending the message: %s\00"
@.str.2 = [45 x i8] c"Incorrect usage, use: ./webBrowser hostname\0A\00"
@.str.3 = [53 x i8] c"Socket Client: error - unable to resolve host name.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [51 x i8] c"Socket Client: error opening TCP IP-based socket.\0A\00"
@.str.5 = [48 x i8] c"Socket Client: error connecting to the server.\0A\00"
@.str.6 = [39 x i8] c"Socket Client: error writing to socket\00"
@.str.7 = [41 x i8] c"Socket Client: error reading from socket\00"
@.str.8 = [22 x i8] c"**START**\0A%s\0A**END**\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 i32
%9 = alloca [2000 x i8]
%10 = alloca [255 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %4
%21 = icmp sle i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %85
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call %struct.hostent* @gethostbyname(i8* %27)
store %struct.hostent* %28, %struct.hostent** %12
%29 = load %struct.hostent*, %struct.hostent** %12
%30 = icmp eq %struct.hostent* %29, null
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %85
33: ; preds = %24
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %85
41: ; preds = %33
%42 = bitcast %struct.sockaddr_in* %11 to i8*
%43 = call i32 @bzero(i8* %42, i32 12)
store i32 80, i32* %7
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %44, i32* %45
%46 = load i32, i32* %7
%47 = call i32 @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %47, i32* %48
%49 = load %struct.hostent*, %struct.hostent** %12
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = inttoptr i64 %51 to i8*
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
%55 = bitcast i32* %54 to i8*
%56 = load %struct.hostent*, %struct.hostent** %12
%57 = getelementptr inbounds %struct.hostent, %struct.hostent* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @bcopy(i8* %52, i8* %55, i32 %58)
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%62 = call i64 @connect(i32 %60, %struct.sockaddr* %61, i32 12)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %41
%65 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %85
66: ; preds = %41
%67 = load i32, i32* %6
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%69 = call i64 @write(i32 %67, i8* %68, i32 255)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %85
73: ; preds = %66
%74 = load i32, i32* %6
%75 = getelementptr inbounds [2000 x i8], [2000 x i8]* %9, i64 0, i64 0
%76 = call i64 @read(i32 %74, i8* %75, i32 2000)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %73
%79 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %85
80: ; preds = %73
%81 = getelementptr inbounds [2000 x i8], [2000 x i8]* %9, i64 0, i64 0
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i8* %81)
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
store i32 0, i32* %3
br label %85
85: ; preds = %80, %78, %71, %64, %39, %31, %22
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [48 x i8] c"GET / HTTP/1.1\0D\0AHost: %s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.1 = [24 x i8] c"Sending the message: %s\00"
@.str.3 = [53 x i8] c"Socket Client: error - unable to resolve host name.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [51 x i8] c"Socket Client: error opening TCP IP-based socket.\0A\00"
@.str.5 = [48 x i8] c"Socket Client: error connecting to the server.\0A\00"
@.str.6 = [39 x i8] c"Socket Client: error writing to socket\00"
@.str.7 = [41 x i8] c"Socket Client: error reading from socket\00"
@.str.8 = [22 x i8] c"**START**\0A%s\0A**END**\0A\00"
@str = [44 x i8] c"Incorrect usage, use: ./webBrowser hostname\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2000 x i8]
%4 = alloca [255 x i8]
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%13 = icmp slt i32 %0, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %58
16: ; preds = %2
%17 = load i8*, i8** %9
%18 = call %struct.hostent* @gethostbyname(i8* %17)
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %58
22: ; preds = %16
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0))
br label %58
29: ; preds = %22
%30 = call i32 @bzero(i8* %8, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @htons(i32 80)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 1
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to i8*
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 0
%39 = load i32, i32* %38
%40 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%41 = call i64 @connect(i32 %25, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %29
%44 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %58
45: ; preds = %29
%46 = call i64 @write(i32 %25, i8* %7, i32 255)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %58
50: ; preds = %45
%51 = call i64 @read(i32 %25, i8* %6, i32 2000)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
br label %58
55: ; preds = %50
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i8* %6)
%57 = call i32 @close(i32 %25)
br label %58
58: ; preds = %55, %53, %48, %43, %27, %20, %14
%59 = phi i32 [ 2, %14 ], [ 1, %20 ], [ 1, %27 ], [ 1, %43 ], [ 1, %48 ], [ 1, %53 ], [ 0, %55 ]
ret i32 %59
}