#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ recv_line(int, char *);
int /*<<< orphan*/ send_string(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strncasecmp(char *, char *, int);
int main(int argc, char *argv[]) {
  int sockfd;
  struct hostent *host_info;
  struct sockaddr_in target_addr;
  char buffer[4096];
  if (argc < 2) {
    printf("Usage: %s <hostname>\n", argv[0]);
    exit(1);
  }
  if ((host_info = gethostbyname(argv[1])) == NULL)
    fatal("looking up hostname");
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    fatal("in socket");
  target_addr.sin_family = AF_INET;
  target_addr.sin_port = htons(80);
  target_addr.sin_addr = *((struct in_addr *)host_info->h_addr_list[0]);
  memset(&(target_addr.sin_zero), '\0', 8);
  if (connect(sockfd, (struct sockaddr *)&target_addr,
              sizeof(struct sockaddr)) == -1)
    fatal("connecting to target server");
  send_string(sockfd, "HEAD / HTTP/1.0\r\n\r\n");
  while (recv_line(sockfd, buffer)) {
    if (strncasecmp(buffer, "Server:", 7) == 0) {
      printf("The web server for %s is %s\n", argv[1], buffer + 8);
      exit(0);
    }
  }
  printf("Server line not found\n");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11f2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1219 <main+0x69>
lea    0xe02(%rip),%rdi
call   10c0 <fatal@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124a <main+0x9a>
lea    0xde5(%rip),%rdi
call   10c0 <fatal@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12a8 <main+0xf8>
lea    0xd91(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x14(%rbp),%edi
lea    0xd9e(%rip),%rsi
call   1030 <send_string@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
call   1090 <recv_line@plt>
cmp    $0x0,%rax
je     131f <main+0x16f>
lea    -0x1030(%rbp),%rdi
lea    0xd86(%rip),%rsi
mov    $0x7,%edx
call   1080 <strncasecmp@plt>
cmp    $0x0,%rax
jne    131a <main+0x16a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x1030(%rbp),%rdx
add    $0x8,%rdx
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12b7 <main+0x107>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
cmp    $0x1,%edi
jg     11f0 <main+0x30>
mov    (%r14),%rsi
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    120d <main+0x4d>
lea    0xe0e(%rip),%rdi
call   10d0 <fatal@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    123a <main+0x7a>
lea    0xdf5(%rip),%rdi
call   10d0 <fatal@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1294 <main+0xd4>
lea    0xda5(%rip),%rdi
call   10d0 <fatal@plt>
lea    0xdb5(%rip),%rsi
mov    %r15d,%edi
call   1030 <send_string@plt>
lea    0x10(%rsp),%rbx
lea    0xdb5(%rip),%rbp
nop
mov    %r15d,%edi
mov    %rbx,%rsi
call   10a0 <recv_line@plt>
test   %rax,%rax
je     12f3 <main+0x133>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x7,%edx
call   1090 <strncasecmp@plt>
test   %rax,%rax
jne    12b0 <main+0xf0>
mov    0x8(%r14),%rsi
lea    0x18(%rsp),%rdx
lea    0xd87(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd8f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Usage: %s <hostname>\0A\00"
@.str.1 = [20 x i8] c"looking up hostname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [10 x i8] c"in socket\00"
@AF_INET = global i32 0
@.str.3 = [28 x i8] c"connecting to target server\00"
@.str.4 = [20 x i8] c"HEAD / HTTP/1.0\0D\0A\0D\0A\00"
@.str.5 = [8 x i8] c"Server:\00"
@.str.6 = [29 x i8] c"The web server for %s is %s\0A\00"
@.str.7 = [23 x i8] c"Server line not found\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 [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call %struct.hostent* @gethostbyname(i8* %21)
store %struct.hostent* %22, %struct.hostent** %7
%23 = icmp eq %struct.hostent* %22, null
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* @PF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 3
store i32 %34, i32* %35
%36 = call i32 @htons(i32 80)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%39 = load %struct.hostent*, %struct.hostent** %7
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = getelementptr inbounds i64, i64* %41, i64 0
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to %struct.in_addr*
%45 = bitcast %struct.in_addr* %38 to i8*
%46 = bitcast %struct.in_addr* %44 to i8*
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%48 = call i32 @memset(i32* %47, i8 signext 0, i32 8)
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%51 = call i32 @connect(i32 %49, %struct.sockaddr* %50, i32 4)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %33
%54 = call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %33
%56 = load i32, i32* %6
%57 = call i32 @send_string(i32 %56, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %75, %55
%59 = load i32, i32* %6
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%61 = call i64 @recv_line(i32 %59, i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %76
63: ; preds = %58
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%65 = call i64 @strncasecmp(i8* %64, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 7)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %63
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%72 = getelementptr inbounds i8, i8* %71, i64 8
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %70, i8* %72)
%74 = call i32 @exit(i32 0)
unreachable
75: ; preds = %63
br label %58
76: ; preds = %58
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%78 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Usage: %s <hostname>\0A\00"
@.str.1 = [20 x i8] c"looking up hostname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [10 x i8] c"in socket\00"
@AF_INET = global i32 0
@.str.3 = [28 x i8] c"connecting to target server\00"
@.str.4 = [20 x i8] c"HEAD / HTTP/1.0\0D\0A\0D\0A\00"
@.str.5 = [8 x i8] c"Server:\00"
@.str.6 = [29 x i8] c"The web server for %s is %s\0A\00"
@str = [22 x i8] c"Server line not found\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call %struct.hostent* @gethostbyname(i8* %14)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = tail call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %27, i32* %28
%29 = tail call i32 @htons(i32 80)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%32 = load i64*, i64** %31
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i32*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%36 = load i32, i32* %34
store i32 %36, i32* %35
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%38 = call i32 @memset(i32* %37, i8 signext 0, i32 8)
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @connect(i32 %22, %struct.sockaddr* %39, i32 4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %26
%43 = call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %26
%45 = call i32 @send_string(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %49, %44
%47 = call i64 @recv_line(i32 %22, i8* %6)
%48 = icmp eq i64 %47, 0
br i1 %48, label %57, label %49
49: ; preds = %46
%50 = call i64 @strncasecmp(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 7)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %46
52: ; preds = %49
%53 = load i8*, i8** %13
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 8
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %53, i8* %54)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %46
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
}