#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 */
typedef int /*<<< orphan*/ xbuffer;
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int readfile_init(char *);
int /*<<< orphan*/ readonly(int, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int client_echo(int cfd) {
  char buffer[2048];
  ssize_t rd;
  if ((rd = read(cfd, buffer, 128)) <= 0) {
    perror("read()");
    return (-1);
  }
  if (memcmp(buffer, "GET", 3) == 0) {
    char xbuffer[4096];
    read(cfd, xbuffer, sizeof(xbuffer));
    const char *risposta = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    int fd;
    fd = readfile_init("page_html");
    write(cfd, risposta, strlen(risposta));
    readonly(fd, cfd);
    close(fd);
  }
  return (0);
}


x86 assembly (clang, -O0):
<client_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1840,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x814(%rbp)
cmp    $0x0,%eax
jg     11c9 <client_echo+0x49>
lea    0xe48(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <client_echo+0xf6>
lea    -0x810(%rbp),%rdi
lea    0xe30(%rip),%rsi
mov    $0x3,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    126f <client_echo+0xef>
mov    -0x8(%rbp),%edi
lea    -0x1820(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
lea    0xe05(%rip),%rax
mov    %rax,-0x1828(%rbp)
lea    0xe24(%rip),%rdi
call   1050 <readfile_init@plt>
mov    %eax,-0x182c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x183c(%rbp)
mov    -0x1828(%rbp),%rax
mov    %rax,-0x1838(%rbp)
mov    -0x1828(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x183c(%rbp),%edi
mov    -0x1838(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x182c(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1090 <readonly@plt>
mov    -0x182c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_echo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1808,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    121b <client_echo+0x9b>
lea    0xe60(%rip),%rsi
mov    %rsp,%rdi
mov    $0x3,%edx
call   1080 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    120b <client_echo+0x8b>
lea    0x800(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
lea    0xe62(%rip),%rdi
call   1050 <readfile_init@plt>
mov    %eax,%r14d
lea    0xe26(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1090 <readonly@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x1808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    120b <client_echo+0x8b>

LLVM IR (-O0):
@.str = [7 x i8] c"read()\00"
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [45 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
@.str.3 = [10 x i8] c"page_html\00"
define i32 @client_echo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2048 x i8]
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 128)
store i32 %11, i32* %5
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
15: ; preds = %1
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%17 = call i64 @memcmp(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = load i32, i32* %3
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 4096)
store i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8** %7
%23 = call i32 @readfile_init(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 %23, i32* %8
%24 = load i32, i32* %3
%25 = load i8*, i8** %7
%26 = load i8*, i8** %7
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
%29 = load i32, i32* %8
%30 = load i32, i32* %3
%31 = call i32 @readonly(i32 %29, i32 %30)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %19, %15
store i32 0, i32* %2
br label %35
35: ; preds = %34, %13
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [7 x i8] c"read()\00"
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [45 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
@.str.3 = [10 x i8] c"page_html\00"
define i32 @client_echo(i32 %0) {
%2 = alloca [2048 x i8]
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 128)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
9: ; preds = %1
%10 = call i64 @memcmp(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%14 = call i32 @read(i32 %0, i8* %13, i32 4096)
%15 = call i32 @readfile_init(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @strlen(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @write(i32 %0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @readonly(i32 %15, i32 %0)
%19 = call i32 @close(i32 %15)
br label %20
20: ; preds = %9, %12, %7
%21 = phi i32 [ -1, %7 ], [ 0, %12 ], [ 0, %9 ]
ret i32 %21
}