void _readloop(int sockfd) {
  int n;
  char recvBuf[2024];
  while ((n = read(sockfd, recvBuf, 2024)) > 0) {
    printf("%.*s\n", n, recvBuf);
  }
  if (n < 0) {
    printf("Read error\n");
  } else {
    return;
  }
}


x86 assembly (clang, -O0):
<_readloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7f0,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x7f0(%rbp),%rsi
mov    $0x7e8,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    116d <_readloop+0x4d>
mov    -0x8(%rbp),%esi
lea    -0x7f0(%rbp),%rdx
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    112e <_readloop+0xe>
cmpl   $0x0,-0x8(%rbp)
jge    118a <_readloop+0x6a>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    118f <_readloop+0x6f>
jmp    118f <_readloop+0x6f>
add    $0x7f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_readloop>:
push   %rbp
push   %r14
push   %rbx
sub    $0x7f0,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x7e8,%edx
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
jle    1184 <_readloop+0x54>
lea    0xea9(%rip),%r14
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7e8,%edx
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
jg     1160 <_readloop+0x30>
test   %eax,%eax
jns    1194 <_readloop+0x64>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
add    $0x7f0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.*s\0A\00"
@.str.1 = [12 x i8] c"Read error\0A\00"
define void @_readloop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2024 x i8]
store i32 %0, i32* %2
br label %5
5: ; preds = %10, %1
%6 = load i32, i32* %2
%7 = getelementptr inbounds [2024 x i8], [2024 x i8]* %4, i64 0, i64 0
%8 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %6, i8* %7, i32 2024)
store i32 %8, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i32, i32* %3
%12 = getelementptr inbounds [2024 x i8], [2024 x i8]* %4, i64 0, i64 0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %12)
br label %5
14: ; preds = %5
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %20
19: ; preds = %14
br label %20
20: ; preds = %19, %17
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.*s\0A\00"
@str = [11 x i8] c"Read error\00"
define void @_readloop(i32 %0) {
%2 = alloca [2024 x i8]
%3 = getelementptr inbounds [2024 x i8], [2024 x i8]* %2, i64 0, i64 0
%4 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %3, i32 2024)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1, %6
%7 = phi i32 [ %9, %6 ], [ %4, %1 ]
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %3)
%9 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %3, i32 2024)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %6, label %11
11: ; preds = %6, %1
%12 = phi i32 [ %4, %1 ], [ %9, %6 ]
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %11, %14
ret void
}