#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void handle_request(int msgsock) {
  char buffer[1024];
  int num_read = 0;
  num_read = read(msgsock, buffer, 1024 - 1);
  while (num_read > 0) {
    buffer[num_read] = 0;
    printf("echoing %d bytes: %s \n", num_read, buffer);
    write(msgsock, buffer, num_read);
    num_read = read(msgsock, buffer, 1024 - 1);
  }
  close(msgsock);
}


x86 assembly (clang, -O0):
<handle_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x414(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jle    11db <handle_request+0x9b>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
jmp    1172 <handle_request+0x32>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_request>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11a4 <handle_request+0x64>
mov    %eax,%ebp
lea    0xe97(%rip),%r14
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1170 <handle_request+0x30>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"echoing %d bytes: %s \0A\00"
define void @handle_request(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @read(i32 %5, i8* %6, i32 1023)
store i32 %7, i32* %4
br label %8
8: ; preds = %11, %1
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %8
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %13
store i8 0, i8* %14
%15 = load i32, i32* %4
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %16)
%18 = load i32, i32* %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
%22 = load i32, i32* %2
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 1023)
store i32 %24, i32* %4
br label %8
25: ; preds = %8
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"echoing %d bytes: %s \0A\00"
define void @handle_request(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 1023)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %1, %6
%7 = phi i32 [ %12, %6 ], [ %4, %1 ]
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %8
store i8 0, i8* %9
%10 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %3)
%11 = call i32 @write(i32 %0, i8* %3, i32 %7)
%12 = call i32 @read(i32 %0, i8* %3, i32 1023)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %6, label %14
14: ; preds = %6, %1
%15 = call i32 @close(i32 %0)
ret void
}