#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*/ close(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
void *serve(void *param) {
  int fd = (int)((long)param);
  char buf[1024];
  while (1) {
    int size = read(fd, buf, sizeof(buf) - 1, 0);
    if (size < 0) {
      perror("cannot read");
      break;
    } else if (size == 0) {
      break;
    }
    buf[size] = 0;
    size = send(fd, buf, size, 0);
    if (size <= 0) {
      perror("cannot send");
      break;
    } else {
    }
  }
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1190 <serve+0x50>
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
jmp    11fb <serve+0xbb>
cmpl   $0x0,-0x414(%rbp)
jne    11a2 <serve+0x62>
jmp    11fb <serve+0xbb>
jmp    11a7 <serve+0x67>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     11f1 <serve+0xb1>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
jmp    11fb <serve+0xbb>
jmp    11f6 <serve+0xb6>
jmp    1156 <serve+0x16>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    %rsp,%r15
lea    0xea5(%rip),%r14
lea    0xeaa(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1050 <read@plt>
test   %eax,%eax
js     11a2 <serve+0x62>
je     11aa <serve+0x6a>
mov    %eax,%ecx
movb   $0x0,(%rsp,%rcx,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
jg     1170 <serve+0x30>
mov    %r12,%r14
mov    %r14,%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"cannot read\00"
@.str.1 = [12 x i8] c"cannot send\00"
define i8* @serve(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = ptrtoint i8* %6 to i64
%8 = trunc i64 %7 to i32
store i32 %8, i32* %3
br label %9
9: ; preds = %1, %35
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 1023, i32 0)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %36
17: ; preds = %9
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
br label %36
21: ; preds = %17
br label %22
22: ; preds = %21
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %3
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* %5
%29 = call i32 @send(i32 %26, i8* %27, i32 %28, i32 0)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %36
34: ; preds = %22
br label %35
35: ; preds = %34
br label %9
36: ; preds = %32, %20, %15
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
ret i8* null
}

LLVM IR (-O2):
@.str = [12 x i8] c"cannot read\00"
@.str.1 = [12 x i8] c"cannot send\00"
define noalias i8* @serve(i8* %0) {
%2 = alloca [1024 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %6
6: ; preds = %11, %1
%7 = call i32 @read(i32 %4, i8* %5, i32 1023, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %6
%10 = icmp eq i32 %7, 0
br i1 %10, label %19, label %11
11: ; preds = %9
%12 = zext i32 %7 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @send(i32 %4, i8* %5, i32 %7, i32 0)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %6
16: ; preds = %11, %6
%17 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %11 ]
%18 = call i32 @perror(i8* %17)
br label %19
19: ; preds = %9, %16
%20 = call i32 @close(i32 %4)
ret i8* null
}