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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, size_t);
void echo(int sockfd) {
  char buf[80];
  ssize_t n;
  while ((n = read(sockfd, buf, sizeof(buf))) > 0) {
    buf[n] = '\0';
    printf("server: %s", buf);
    fflush(stdout);
    write(sockfd, buf, n);
  }
}


x86 assembly (clang, -O0):
<echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x50,%edx
call   1050 <read@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jbe    11a6 <echo+0x66>
mov    -0x68(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rsi
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x68(%rbp),%rdx
call   1030 <write@plt>
jmp    114b <echo+0xb>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x50,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11bc <echo+0x7c>
mov    %rax,%rbx
lea    0xe96(%rip),%r14
mov    %rsp,%r15
mov    0x2e6c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,(%rsp,%rbx,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    (%r12),%edi
call   1060 <fflush@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x50,%edx
call   1050 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1180 <echo+0x40>
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"server: %s\00"
@stdout = global i32 0
define void @echo(i32 %0) {
%2 = alloca i32
%3 = alloca [80 x i8]
%4 = alloca i64
store i32 %0, i32* %2
br label %5
5: ; preds = %10, %1
%6 = load i32, i32* %2
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%8 = call i64 @read(i32 %6, i8* %7, i32 80)
store i64 %8, i64* %4
%9 = icmp ugt i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %5
%11 = load i64, i64* %4
%12 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %11
store i8 0, i8* %12
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%14 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = load i32, i32* %2
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%19 = load i64, i64* %4
%20 = call i32 @write(i32 %17, i8* %18, i64 %19)
br label %5
21: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"server: %s\00"
@stdout = global i32 0
define void @echo(i32 %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 80)
%5 = icmp eq i64 %4, 0
br i1 %5, label %15, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %13, %6 ], [ %4, %1 ]
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %8
%9 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %3)
%10 = load i32, i32* @stdout
%11 = call i32 @fflush(i32 %10)
%12 = call i32 @write(i32 %0, i8* %3, i64 %7)
%13 = call i64 @read(i32 %0, i8* %3, i32 80)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %6
15: ; preds = %6, %1
ret void
}