#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 server_t {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ process_line(struct server_t *, char *);
scalar_t__ read(int, char *, int);
void read_line(int fd, struct server_t *server) {
  char ch, buf[1024];
  int sp;
  sp = 0;
  while (read(fd, &ch, 1) != 0) {
    if (ch == '\n' || sp >= 1024) {
      buf[sp++] = '\0';
      process_line(server, buf);
      sp = 0;
    } else {
      buf[sp++] = ch;
    }
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
je     11d0 <read_line+0xb0>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
je     1174 <read_line+0x54>
cmpl   $0x400,-0x424(%rbp)
jl     11ae <read_line+0x8e>
mov    -0x424(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x424(%rbp)
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1040 <process_line@plt>
movl   $0x0,-0x424(%rbp)
jmp    11cb <read_line+0xab>
mov    -0x11(%rbp),%cl
mov    -0x424(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x424(%rbp)
cltq
mov    %cl,-0x420(%rbp,%rax,1)
jmp    113c <read_line+0x1c>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     11a3 <read_line+0x83>
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
lea    0xf(%rsp),%r12
jmp    1186 <read_line+0x66>
cs nopw 0x0(%rax,%rax,1)
movb   $0x0,0x10(%rsp,%rcx,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <process_line@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     11a3 <read_line+0x83>
movzbl 0xf(%rsp),%eax
movslq %ebx,%rcx
cmp    $0xa,%al
je     1160 <read_line+0x40>
cmp    $0x400,%ebx
jge    1160 <read_line+0x40>
add    $0x1,%ebx
mov    %al,0x10(%rsp,%rcx,1)
jmp    1172 <read_line+0x52>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.server_t = type { i32 }
define void @read_line(i32 %0, %struct.server_t* %1) {
%3 = alloca i32
%4 = alloca %struct.server_t*
%5 = alloca i8
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store %struct.server_t* %1, %struct.server_t** %4
store i32 0, i32* %7
br label %8
8: ; preds = %33, %2
%9 = load i32, i32* %3
%10 = call i64 @read(i32 %9, i8* %5, i32 1)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %34
12: ; preds = %8
%13 = load i8, i8* %5
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 10
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i32, i32* %7
%18 = icmp sge i32 %17, 1024
br i1 %18, label %19, label %27
19: ; preds = %16, %12
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
store i32 %21, i32* %7
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %22
store i8 0, i8* %23
%24 = load %struct.server_t*, %struct.server_t** %4
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%26 = call i32 @process_line(%struct.server_t* %24, i8* %25)
store i32 0, i32* %7
br label %33
27: ; preds = %16
%28 = load i8, i8* %5
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %27, %19
br label %8
34: ; preds = %8
ret void
}

LLVM IR (-O2):
%struct.server_t = type { i32 }
define void @read_line(i32 %0, %struct.server_t* %1) {
%3 = alloca i8
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i64 @read(i32 %0, i8* %3, i32 1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %2, %22
%9 = phi i32 [ %23, %22 ], [ 0, %2 ]
%10 = load i8, i8* %3
%11 = icmp eq i8 %10, 10
%12 = icmp sgt i32 %9, 1023
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = sext i32 %9 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i32 @process_line(%struct.server_t* %1, i8* %5)
br label %22
18: ; preds = %8
%19 = add nsw i32 %9, 1
%20 = sext i32 %9 to i64
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %20
store i8 %10, i8* %21
br label %22
22: ; preds = %18, %14
%23 = phi i32 [ 0, %14 ], [ %19, %18 ]
%24 = call i64 @read(i32 %0, i8* %3, i32 1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %8
26: ; preds = %22, %2
ret void
}