#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 read_socket(char *, int, int const);
int read_line(char *buf, const int sockfd) {
  int stat = 0;
  while (1) {
    stat = read_socket(buf, 1, sockfd);
    if (stat == 0)
      return stat;
    if (*buf == '\n') {
      *buf = 0;
      return stat;
    }
    ++buf;
  }
  return stat;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x1,%esi
call   1030 <read_socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    114f <read_line+0x3f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1182 <read_line+0x72>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1171 <read_line+0x61>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1182 <read_line+0x72>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1126 <read_line+0x16>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x1,%esi
mov    %ebp,%edx
call   1030 <read_socket@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     116c <read_line+0x5c>
cmpb   $0xa,(%rbx)
je     1166 <read_line+0x56>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1030 <read_socket@plt>
test   %eax,%eax
je     116c <read_line+0x5c>
lea    0x1(%rbx),%rcx
cmpb   $0xa,(%rbx)
mov    %rcx,%rbx
jne    1140 <read_line+0x30>
add    $0xffffffffffffffff,%rcx
mov    %rcx,%rbx
movb   $0x0,(%rbx)
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_line(i8* %0, i32 %1) {
%3 = tail call i32 @read_socket(i8* %0, i32 1, i32 %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %19, label %5
5: ; preds = %2
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 10
br i1 %7, label %11, label %14
8: ; preds = %14
%9 = load i8, i8* %16
%10 = icmp eq i8 %9, 10
br i1 %10, label %11, label %14
11: ; preds = %8, %5
%12 = phi i32 [ %3, %5 ], [ %17, %8 ]
%13 = phi i8* [ %0, %5 ], [ %16, %8 ]
store i8 0, i8* %13
br label %19
14: ; preds = %5, %8
%15 = phi i8* [ %16, %8 ], [ %0, %5 ]
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = tail call i32 @read_socket(i8* %16, i32 1, i32 %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %8
19: ; preds = %14, %2, %11
%20 = phi i32 [ %12, %11 ], [ 0, %2 ], [ 0, %14 ]
ret i32 %20
}