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

/* Variables and functions */
int /*<<< orphan*/ error(char *);
int read(int, char *, int);
void read_text_data(int sock, char *buffer, int buffsize) {
  int num_bytes;
  *buffer = '\0';
  for (num_bytes = 1; num_bytes < buffsize - 1; num_bytes++) {
    char ch;
    int num_bytes_read = read(sock, &ch, 1);
    if (num_bytes_read == 1) {
      if (ch == '\n') {
        break;
      }
      *buffer++ = ch;
    } else if (num_bytes_read == 0) {
      break;
    } else {
      error("read_text_data failed");
    }
  }
  *buffer++ = '\0';
}


x86 assembly (clang, -O0):
<read_text_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11cd <read_text_data+0xad>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    119a <read_text_data+0x7a>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1181 <read_text_data+0x61>
jmp    11cd <read_text_data+0xad>
mov    -0x19(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11ba <read_text_data+0x9a>
cmpl   $0x0,-0x20(%rbp)
jne    11a9 <read_text_data+0x89>
jmp    11cd <read_text_data+0xad>
lea    0xe50(%rip),%rdi
call   1040 <error@plt>
jmp    11ba <read_text_data+0x9a>
jmp    11bf <read_text_data+0x9f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1140 <read_text_data+0x20>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movb   $0x0,(%rsi)
cmp    $0x3,%edx
jl     1191 <read_text_data+0x71>
mov    %edx,%ebp
mov    %edi,%r14d
add    $0xfffffffe,%ebp
lea    0xf(%rsp),%r15
lea    0xeb5(%rip),%r12
jmp    1155 <read_text_data+0x35>
nopl   (%rax)
add    $0xffffffff,%ebp
je     1191 <read_text_data+0x71>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1180 <read_text_data+0x60>
test   %eax,%eax
je     1191 <read_text_data+0x71>
mov    %r12,%rdi
call   1040 <error@plt>
jmp    1150 <read_text_data+0x30>
nopl   0x0(%rax,%rax,1)
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1191 <read_text_data+0x71>
mov    %al,(%rbx)
add    $0x1,%rbx
jmp    1150 <read_text_data+0x30>
movb   $0x0,(%rbx)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"read_text_data failed\00"
define void @read_text_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
store i8 0, i8* %10
store i32 1, i32* %7
br label %11
11: ; preds = %38, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = sub nsw i32 %13, 1
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %41
16: ; preds = %11
%17 = load i32, i32* %4
%18 = call i32 @read(i32 %17, i8* %8, i32 1)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %30
21: ; preds = %16
%22 = load i8, i8* %8
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %21
br label %41
26: ; preds = %21
%27 = load i8, i8* %8
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
store i8 %27, i8* %28
br label %37
30: ; preds = %16
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %41
34: ; preds = %30
%35 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34
br label %37
37: ; preds = %36, %26
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %11
41: ; preds = %33, %25, %11
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %5
store i8 0, i8* %42
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"read_text_data failed\00"
define void @read_text_data(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
store i8 0, i8* %1
%5 = add i32 %2, -1
%6 = icmp sgt i32 %2, 2
br i1 %6, label %7, label %23
7: ; preds = %3, %19
%8 = phi i8* [ %20, %19 ], [ %1, %3 ]
%9 = phi i32 [ %21, %19 ], [ 1, %3 ]
%10 = call i32 @read(i32 %0, i8* %4, i32 1)
switch i32 %10, label %16 [
i32 1, label %11
i32 0, label %18
]
11: ; preds = %7
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 10
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %12, i8* %8
br label %19
16: ; preds = %7
%17 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %19
18: ; preds = %7, %11
br label %23
19: ; preds = %16, %14
%20 = phi i8* [ %8, %16 ], [ %15, %14 ]
%21 = add nuw nsw i32 %9, 1
%22 = icmp eq i32 %21, %5
br i1 %22, label %23, label %7
23: ; preds = %19, %3, %18
%24 = phi i8* [ %8, %18 ], [ %1, %3 ], [ %20, %19 ]
store i8 0, i8* %24
ret void
}