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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
void echo(int connfd) {
  char buf[4096];
  int n = read(connfd, buf, sizeof(buf));
  if (n < 0) {
    perror("read");
    exit(EXIT_FAILURE);
  }
  if (n > 0) {
    printf("server received %d bytes\n", (int)n);
    int out = write(connfd, buf, n);
    if (out != n) {
      fprintf(stderr, "write error\n");
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    11af <echo+0x4f>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1014(%rbp)
jle    1223 <echo+0xc3>
mov    -0x1014(%rbp),%esi
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1018(%rbp)
mov    -0x1018(%rbp),%eax
cmp    -0x1014(%rbp),%eax
je     121e <echo+0xbe>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1223 <echo+0xc3>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11a8 <echo+0x48>
je     119e <echo+0x3e>
mov    %eax,%ebp
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    11b6 <echo+0x56>
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
jmp    11cb <echo+0x6b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"server received %d bytes\0A\00"
@stderr = global i32 0
@.str.2 = [13 x i8] c"write error\0A\00"
define void @echo(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 4096)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %2
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%23 = load i32, i32* %4
%24 = call i32 @write(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = load i32, i32* %4
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %18
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %18
br label %34
34: ; preds = %33, %15
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"server received %d bytes\0A\00"
@stderr = global i32 0
@.str.2 = [13 x i8] c"write error\0A\00"
define void @echo(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 4096)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = icmp eq i32 %4, 0
br i1 %11, label %21, label %12
12: ; preds = %10
%13 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%14 = call i32 @write(i32 %0, i8* %3, i32 %4)
%15 = icmp eq i32 %14, %4
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12, %10
ret void
}