#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ buffer_date;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
size_t strlen(char *);
size_t write(int, char *, size_t);
int send_request(int port, const char *path) {
  int ret = -1;
  int sockfd;
  struct sockaddr_in server_addr;
  struct hostent *server;
  int res;
  FILE *fhd = NULL;
  char buffer_date[2048];
  ssize_t nb;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  assert(sockfd >= 0);
  server = gethostbyname("localhost");
  assert(server != NULL);
  server_addr.sin_family = AF_INET;
  memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
  server_addr.sin_port = htons(port);
  res = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  assert(res == 0);
  fhd = fdopen(sockfd, "r+");
  assert(fhd != NULL);
  snprintf(
      buffer_date, sizeof(buffer_date),
      "GET %s HTTP/1.1\r\nHost: localhost\r\nUser-Agent: home-made\r\n\r\n",
      path);
  nb = write(sockfd, buffer_date, strlen(buffer_date));
  assert(nb == strlen(buffer_date));
  while (1) {
    nb = read(sockfd, buffer_date, sizeof(buffer_date));
    assert(nb >= 0);
    if (nb == 0)
      break;
    buffer_date[nb] = 0;
    printf("%s", buffer_date);
  }
  fclose(fhd);
  close(sockfd);
  ret = 0;
  printf("======================================\n");
  return ret;
}


x86 assembly (clang, -O0):
<send_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
lea    0xdcb(%rip),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10d0 <memcpy@plt>
mov    -0x4(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
mov    -0x18(%rbp),%edi
lea    0xd5a(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
lea    -0x840(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x800,%esi
lea    0xd2b(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x85c(%rbp)
lea    -0x840(%rbp),%rax
mov    %rax,-0x858(%rbp)
lea    -0x840(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x85c(%rbp),%edi
mov    -0x858(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x848(%rbp)
mov    -0x848(%rbp),%rax
mov    %rax,-0x850(%rbp)
lea    -0x840(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x850(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
mov    -0x18(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    $0x800,%edx
call   10a0 <read@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
cmpq   $0x0,-0x848(%rbp)
jne    139d <send_request+0x1bd>
jmp    13c6 <send_request+0x1e6>
mov    -0x848(%rbp),%rax
movb   $0x0,-0x840(%rbp,%rax,1)
lea    -0x840(%rbp),%rsi
lea    0xc8f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    135a <send_request+0x17a>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x14(%rbp)
lea    0xc67(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_request>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x810,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2ddd(%rip),%r14
mov    (%r14),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%r12d
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   10d0 <assert@plt>
lea    0xdcc(%rip),%rdi
call   10c0 <gethostbyname@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   10e0 <memcpy@plt>
mov    %ebp,%edi
call   1070 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   1100 <connect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   10d0 <assert@plt>
lea    0xd7c(%rip),%rsi
mov    %r12d,%edi
call   10f0 <fdopen@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
lea    0xd5f(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x800,%esi
mov    %r15,%rcx
call   1090 <snprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
xor    %edi,%edi
cmp    %rax,%rbx
sete   %dil
call   10d0 <assert@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x800,%edx
call   10b0 <read@plt>
mov    %rax,%rbp
mov    $0x1,%edi
call   10d0 <assert@plt>
test   %rbp,%rbp
je     1354 <send_request+0x164>
lea    0xd2e(%rip),%r15
lea    0x10(%rsp),%rbx
movb   $0x0,0x10(%rsp,%rbp,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   10b0 <read@plt>
mov    %rax,%rbp
mov    $0x1,%edi
call   10d0 <assert@plt>
test   %rbp,%rbp
jne    1320 <send_request+0x130>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
lea    0xce1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x810,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [60 x i8] c"GET %s HTTP/1.1\0D\0AHost: localhost\0D\0AUser-Agent: home-made\0D\0A\0D\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [40 x i8] c"======================================\0A\00"
define i32 @send_request(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca [2048 x i8]
%12 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 -1, i32* %5
store i32* null, i32** %10
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %20, %struct.hostent** %8
%21 = load %struct.hostent*, %struct.hostent** %8
%22 = icmp ne %struct.hostent* %21, null
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load %struct.hostent*, %struct.hostent** %8
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load %struct.hostent*, %struct.hostent** %8
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i32* %28, i32 %31, i32 %34)
%36 = load i32, i32* %3
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%41 = call i32 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 0
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i32, i32* %6
%47 = call i32* @fdopen(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %47, i32** %10
%48 = load i32*, i32** %10
%49 = icmp ne i32* %48, null
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%53 = load i8*, i8** %4
%54 = call i32 @snprintf(i8* %52, i32 2048, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %6
%56 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%57 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%58 = call i64 @strlen(i8* %57)
%59 = call i64 @write(i32 %55, i8* %56, i64 %58)
store i64 %59, i64* %12
%60 = load i64, i64* %12
%61 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%62 = call i64 @strlen(i8* %61)
%63 = icmp eq i64 %60, %62
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
br label %66
66: ; preds = %2, %77
%67 = load i32, i32* %6
%68 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%69 = call i64 @read(i32 %67, i8* %68, i32 2048)
store i64 %69, i64* %12
%70 = load i64, i64* %12
%71 = icmp uge i64 %70, 0
%72 = zext i1 %71 to i32
%73 = call i32 @assert(i32 %72)
%74 = load i64, i64* %12
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %66
br label %82
77: ; preds = %66
%78 = load i64, i64* %12
%79 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 %78
store i8 0, i8* %79
%80 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %80)
br label %66
82: ; preds = %76
%83 = load i32*, i32** %10
%84 = call i32 @fclose(i32* %83)
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
store i32 0, i32* %5
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [60 x i8] c"GET %s HTTP/1.1\0D\0AHost: localhost\0D\0AUser-Agent: home-made\0D\0A\0D\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [39 x i8] c"======================================\00"
define i32 @send_request(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [2048 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = xor i32 %9, -1
%11 = lshr i32 %10, 31
%12 = tail call i32 @assert(i32 %11)
%13 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne %struct.hostent* %13, null
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i32* %19, i32 %21, i32 %23)
%25 = call i32 @htons(i32 %0)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @connect(i32 %9, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp ne i32* %32, null
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = call i32 @snprintf(i8* %6, i32 2048, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%37 = call i64 @strlen(i8* %6)
%38 = call i64 @write(i32 %9, i8* %6, i64 %37)
%39 = call i64 @strlen(i8* %6)
%40 = icmp eq i64 %38, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = call i64 @read(i32 %9, i8* %6, i32 2048)
%44 = call i32 @assert(i32 1)
%45 = icmp eq i64 %43, 0
br i1 %45, label %53, label %46
46: ; preds = %2, %46
%47 = phi i64 [ %50, %46 ], [ %43, %2 ]
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%50 = call i64 @read(i32 %9, i8* %6, i32 2048)
%51 = call i32 @assert(i32 1)
%52 = icmp eq i64 %50, 0
br i1 %52, label %53, label %46
53: ; preds = %46, %2
%54 = call i32 @fclose(i32* %32)
%55 = call i32 @close(i32 %9)
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
ret i32 0
}