#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 recv(int, char *, int, int /*<<< orphan*/);
void readline(int sockfd, char *buf) {
  int pos = 0;
  char c;
  for (;;) {
    int n = recv(sockfd, &c, 1, 0);
    if (n < 1)
      break;
    buf[pos++] = c;
    if (c == '\n')
      break;
  }
  buf[pos] = '\0';
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    114b <readline+0x3b>
jmp    117a <readline+0x6a>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1175 <readline+0x65>
jmp    117a <readline+0x6a>
jmp    1126 <readline+0x16>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0x7(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1156 <readline+0x46>
movzbl 0x7(%rsp),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1130 <readline+0x20>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @readline(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %3
%10 = call i32 @recv(i32 %9, i8* %6, i32 1, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %8
br label %26
14: ; preds = %8
%15 = load i8, i8* %6
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
store i8 %15, i8* %20
%21 = load i8, i8* %6
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %14
br label %26
25: ; preds = %14
br label %8
26: ; preds = %24, %13
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
ret void
}

LLVM IR (-O2):
define void @readline(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i8
br label %4
4: ; preds = %8, %2
%5 = phi i64 [ %10, %8 ], [ 0, %2 ]
%6 = call i32 @recv(i32 %0, i8* %3, i32 1, i32 0)
%7 = icmp slt i32 %6, 1
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = load i8, i8* %3
%10 = add nuw i64 %5, 1
%11 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 %9, i8* %11
%12 = icmp eq i8 %9, 10
br i1 %12, label %13, label %4
13: ; preds = %8, %4
%14 = phi i64 [ %5, %4 ], [ %10, %8 ]
%15 = shl i64 %14, 32
%16 = ashr exact i64 %15, 32
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %17
ret void
}