#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\n\r\n", argv[1]);
  printf("Starting EBB Web Browser C Example\n");
  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);
  printf("End of EBB Socket Example\n");
  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    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8f0(%rbp),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jg     123b <main+0x7b>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13dc <main+0x21c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x908(%rbp)
cmpq   $0x0,-0x908(%rbp)
jne    1275 <main+0xb5>
lea    0xe22(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dc <main+0x21c>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12b3 <main+0xf3>
lea    0xe19(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dc <main+0x21c>
lea    -0x900(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
movl   $0x50,-0x18(%rbp)
mov    0x2d16(%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    133e <main+0x17e>
lea    0xdc1(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dc <main+0x21c>
mov    -0x14(%rbp),%edi
lea    -0x8f0(%rbp),%rsi
mov    $0xff,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1374 <main+0x1b4>
lea    0xdbb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dc <main+0x21c>
mov    -0x14(%rbp),%edi
lea    -0x7f0(%rbp),%rsi
mov    $0x7d0,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    13aa <main+0x1ea>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dc <main+0x21c>
lea    -0x7f0(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@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    0xf32(%rip),%rdi
call   1030 <puts@plt>
lea    0xe10(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x1,%ebp
jg     1232 <main+0x62>
lea    0xf4d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
jmp    1343 <main+0x173>
mov    0x8(%rbx),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     130e <main+0x13e>
mov    %rax,%rbx
mov    0x2d9a(%rip),%r15
mov    (%r15),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1317 <main+0x147>
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     1320 <main+0x150>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xff,%edx
call   1040 <write@plt>
test   %rax,%rax
js     1329 <main+0x159>
lea    0x110(%rsp),%rsi
mov    %ebp,%edi
mov    $0x7d0,%edx
call   1080 <read@plt>
test   %rax,%rax
js     1332 <main+0x162>
lea    0xe35(%rip),%rdi
xor    %ebx,%ebx
lea    0x110(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
jmp    1343 <main+0x173>
lea    0xd20(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xd4c(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xd76(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xd9d(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xdbb(%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 = [29 x i8] c"GET / HTTP/1.1\0D\0AHost: %s\0D\0A\0D\0A\00"
@.str.1 = [36 x i8] c"Starting EBB Web Browser C Example\0A\00"
@.str.2 = [24 x i8] c"Sending the message: %s\00"
@.str.3 = [45 x i8] c"Incorrect usage, use: ./webBrowser hostname\0A\00"
@.str.4 = [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.5 = [51 x i8] c"Socket Client: error opening TCP IP-based socket.\0A\00"
@.str.6 = [48 x i8] c"Socket Client: error connecting to the server.\0A\00"
@.str.7 = [39 x i8] c"Socket Client: error writing to socket\00"
@.str.8 = [41 x i8] c"Socket Client: error reading from socket\00"
@.str.9 = [22 x i8] c"**START**\0A%s\0A**END**\0A\00"
@.str.10 = [27 x i8] c"End of EBB Socket Example\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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %4
%22 = icmp sle i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
br label %87
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %12
%30 = load %struct.hostent*, %struct.hostent** %12
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %87
34: ; preds = %25
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %87
42: ; preds = %34
%43 = bitcast %struct.sockaddr_in* %11 to i8*
%44 = call i32 @bzero(i8* %43, i32 12)
store i32 80, i32* %7
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* %7
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %48, i32* %49
%50 = load %struct.hostent*, %struct.hostent** %12
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = inttoptr i64 %52 to i8*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
%56 = bitcast i32* %55 to i8*
%57 = load %struct.hostent*, %struct.hostent** %12
%58 = getelementptr inbounds %struct.hostent, %struct.hostent* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @bcopy(i8* %53, i8* %56, i32 %59)
%61 = load i32, i32* %6
%62 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%63 = call i64 @connect(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %42
%66 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %87
67: ; preds = %42
%68 = load i32, i32* %6
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%70 = call i64 @write(i32 %68, i8* %69, i32 255)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %87
74: ; preds = %67
%75 = load i32, i32* %6
%76 = getelementptr inbounds [2000 x i8], [2000 x i8]* %9, i64 0, i64 0
%77 = call i64 @read(i32 %75, i8* %76, i32 2000)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %74
%80 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %87
81: ; preds = %74
%82 = getelementptr inbounds [2000 x i8], [2000 x i8]* %9, i64 0, i64 0
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i8* %82)
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %3
br label %87
87: ; preds = %81, %79, %72, %65, %40, %32, %23
%88 = load i32, i32* %3
ret i32 %88
}

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 = [29 x i8] c"GET / HTTP/1.1\0D\0AHost: %s\0D\0A\0D\0A\00"
@.str.2 = [24 x i8] c"Sending the message: %s\00"
@.str.4 = [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.5 = [51 x i8] c"Socket Client: error opening TCP IP-based socket.\0A\00"
@.str.6 = [48 x i8] c"Socket Client: error connecting to the server.\0A\00"
@.str.7 = [39 x i8] c"Socket Client: error writing to socket\00"
@.str.8 = [41 x i8] c"Socket Client: error reading from socket\00"
@.str.9 = [22 x i8] c"**START**\0A%s\0A**END**\0A\00"
@str = [35 x i8] c"Starting EBB Web Browser C Example\00"
@str.11 = [26 x i8] c"End of EBB Socket Example\00"
@str.12 = [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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%14 = icmp slt i32 %0, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.12, i64 0, i64 0))
br label %60
17: ; preds = %2
%18 = load i8*, i8** %9
%19 = call %struct.hostent* @gethostbyname(i8* %18)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
br label %60
23: ; preds = %17
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0))
br label %60
30: ; preds = %23
%31 = call i32 @bzero(i8* %8, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @htons(i32 80)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 1
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to i8*
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%40 = load i32, i32* %39
%41 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%42 = call i64 @connect(i32 %26, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %30
%45 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
br label %60
46: ; preds = %30
%47 = call i64 @write(i32 %26, i8* %7, i32 255)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
br label %60
51: ; preds = %46
%52 = call i64 @read(i32 %26, i8* %6, i32 2000)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0))
br label %60
56: ; preds = %51
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i8* %6)
%58 = call i32 @close(i32 %26)
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.11, i64 0, i64 0))
br label %60
60: ; preds = %56, %54, %49, %44, %28, %21, %15
%61 = phi i32 [ 2, %15 ], [ 1, %21 ], [ 1, %28 ], [ 1, %44 ], [ 1, %49 ], [ 1, %54 ], [ 0, %56 ]
ret i32 %61
}