#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;
};

/* 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, unsigned char *);
int /*<<< orphan*/ send_string(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strncasecmp(unsigned char *, char *, int);
int main(int argc, char *argv[]) {
  int sockfd;
  struct hostent *host_info;
  struct sockaddr_in target_addr;
  unsigned 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);
  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),%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    12a5 <main+0xf5>
lea    0xd94(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x14(%rbp),%edi
lea    0xda1(%rip),%rsi
call   1030 <send_string@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
call   1090 <recv_line@plt>
cmp    $0x0,%rax
je     131c <main+0x16c>
lea    -0x1030(%rbp),%rdi
lea    0xd89(%rip),%rsi
mov    $0x7,%edx
call   1080 <strncasecmp@plt>
cmp    $0x0,%rax
jne    1317 <main+0x167>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x1030(%rbp),%rdx
add    $0x8,%rdx
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12b4 <main+0x104>
lea    0xd66(%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),%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    1291 <main+0xd1>
lea    0xda8(%rip),%rdi
call   10d0 <fatal@plt>
lea    0xdb8(%rip),%rsi
mov    %r15d,%edi
call   1030 <send_string@plt>
lea    0x10(%rsp),%rbx
lea    0xdb8(%rip),%rbp
nopl   0x0(%rax)
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 = inttoptr i64 %41 to %struct.in_addr*
%43 = bitcast %struct.in_addr* %38 to i8*
%44 = bitcast %struct.in_addr* %42 to i8*
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%46 = call i32 @memset(i32* %45, i8 signext 0, i32 8)
%47 = load i32, i32* %6
%48 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 4)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %33
%52 = call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %33
%54 = load i32, i32* %6
%55 = call i32 @send_string(i32 %54, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %73, %53
%57 = load i32, i32* %6
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%59 = call i64 @recv_line(i32 %57, i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %56
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%63 = call i64 @strncasecmp(i8* %62, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 7)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %73
65: ; preds = %61
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 1
%68 = load i8*, i8** %67
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%70 = getelementptr inbounds i8, i8* %69, i64 8
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %68, i8* %70)
%72 = call i32 @exit(i32 0)
unreachable
73: ; preds = %61
br label %56
74: ; preds = %56
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%76 = 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 = inttoptr i64 %32 to i32*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%35 = load i32, i32* %33
store i32 %35, i32* %34
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%37 = call i32 @memset(i32* %36, i8 signext 0, i32 8)
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i32 @connect(i32 %22, %struct.sockaddr* %38, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %26
%42 = call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %26
%44 = call i32 @send_string(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %48, %43
%46 = call i64 @recv_line(i32 %22, i8* %6)
%47 = icmp eq i64 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %45
%49 = call i64 @strncasecmp(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 7)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %45
51: ; preds = %48
%52 = load i8*, i8** %13
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 8
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %52, i8* %53)
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %45
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
}