#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ kclose(int);
int /*<<< orphan*/ kgetpeername(int, struct sockaddr_in *, int *);
int kread(int, char *, int);
int /*<<< orphan*/ kwrite(int, char *, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
void serve(int sd, int fd) {
  int len, file_len, total;
  char buf[8192];
  struct sockaddr_in name;
  int name_len = sizeof(struct sockaddr_in);
  kgetpeername(sd, &name, &name_len);
  fprintf(stderr, "start to serve connection from %s:%d\n",
          inet_ntoa(name.sin_addr), ntohs(name.sin_port));
  total = 0;
  kread(sd, buf, sizeof(buf));
  file_len = atoi(buf);
  while ((len = kread(sd, buf, sizeof(buf))) > 0) {
    total += len;
    fprintf(stderr, "%d/%d bytes received.\n", total, file_len);
    write(fd, buf, len);
    if (total >= file_len)
      break;
  }
  kwrite(sd, "OK", 3);
  fprintf(stderr, "complete!\n");
  kclose(sd);
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x8,-0x202c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x2028(%rbp),%rsi
lea    -0x202c(%rbp),%rdx
call   1080 <kgetpeername@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x203c(%rbp)
mov    -0x2024(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x2038(%rbp)
mov    -0x2028(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x203c(%rbp),%edi
mov    -0x2038(%rbp),%rdx
mov    %eax,%ecx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   10a0 <kread@plt>
lea    -0x2020(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   10a0 <kread@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    12a2 <serve+0x112>
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     129d <serve+0x10d>
jmp    12a2 <serve+0x112>
jmp    1234 <serve+0xa4>
mov    -0x4(%rbp),%edi
lea    0xd91(%rip),%rsi
mov    $0x3,%edx
call   1070 <kwrite@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1050 <kclose@plt>
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1080 <kgetpeername@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%ebx
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%edi
call   1090 <ntohs@plt>
lea    0xe1d(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0x10(%rsp),%r13
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x2000,%edx
call   10a0 <kread@plt>
mov    %r13,%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x2000,%edx
call   10a0 <kread@plt>
test   %eax,%eax
jle    1266 <serve+0xd6>
mov    %eax,%r12d
add    %eax,%ebp
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
cmp    %ebx,%ebp
jl     1220 <serve+0x90>
lea    0xdd0(%rip),%rsi
mov    %r15d,%edi
mov    $0x3,%edx
call   1070 <kwrite@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15d,%edi
call   1050 <kclose@plt>
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@stderr = global i32 0
@.str = [38 x i8] c"start to serve connection from %s:%d\0A\00"
@.str.1 = [23 x i8] c"%d/%d bytes received.\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [11 x i8] c"complete!\0A\00"
define void @serve(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8192 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 8, i32* %10
%11 = load i32, i32* %3
%12 = call i32 @kgetpeername(i32 %11, %struct.sockaddr_in* %9, i32* %10)
%13 = load i32, i32* @stderr
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i8* @inet_ntoa(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @ntohs(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %19)
store i32 0, i32* %7
%21 = load i32, i32* %3
%22 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%23 = call i32 @kread(i32 %21, i8* %22, i32 8192)
%24 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
br label %26
26: ; preds = %47, %2
%27 = load i32, i32* %3
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%29 = call i32 @kread(i32 %27, i8* %28, i32 8192)
store i32 %29, i32* %5
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %48
31: ; preds = %26
%32 = load i32, i32* %5
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
%35 = load i32, i32* @stderr
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %37)
%39 = load i32, i32* %4
%40 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* %5
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
%43 = load i32, i32* %7
%44 = load i32, i32* %6
%45 = icmp sge i32 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %31
br label %48
47: ; preds = %31
br label %26
48: ; preds = %46, %26
%49 = load i32, i32* %3
%50 = call i32 @kwrite(i32 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 3)
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* %3
%54 = call i32 @kclose(i32 %53)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@stderr = global i32 0
@.str = [38 x i8] c"start to serve connection from %s:%d\0A\00"
@.str.1 = [23 x i8] c"%d/%d bytes received.\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [11 x i8] c"complete!\0A\00"
define void @serve(i32 %0, i32 %1) {
%3 = alloca [8192 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%9 = call i32 @kgetpeername(i32 %0, %struct.sockaddr_in* %4, i32* %5)
%10 = load i32, i32* @stderr
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%12 = load i32, i32* %11
%13 = call i8* @inet_ntoa(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @ntohs(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %16)
%18 = call i32 @kread(i32 %0, i8* %6, i32 8192)
%19 = call i32 @atoi(i8* %6)
br label %20
20: ; preds = %24, %2
%21 = phi i32 [ 0, %2 ], [ %25, %24 ]
%22 = call i32 @kread(i32 %0, i8* %6, i32 8192)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = add nsw i32 %22, %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %19)
%28 = call i32 @write(i32 %1, i8* %6, i32 %22)
%29 = icmp slt i32 %25, %19
br i1 %29, label %20, label %30
30: ; preds = %24, %20
%31 = call i32 @kwrite(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 3)
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @kclose(i32 %0)
ret void
}