#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*/ memset(char *, char, int);
scalar_t__ read(int, char *, int);
char tolower(char);
int read_line(int _sock, char *_buf, int _buf_len) {
  char read_char, c;
  int line_len;
  line_len = 0;
  memset(_buf, '\0', _buf_len);
  while (read(_sock, &read_char, 1) > 0) {
    c = tolower(read_char);
    if (c == '\n') {
      _buf[line_len++] = '\0';
      return line_len;
    }
    _buf[line_len++] = c;
    if (line_len == _buf_len - 1) {
      _buf[line_len] = '\0';
      return 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    11f2 <read_line+0xc2>
movsbl -0x15(%rbp),%edi
call   1050 <tolower@plt>
mov    %al,-0x16(%rbp)
movsbl -0x16(%rbp),%eax
cmp    $0xa,%eax
jne    11ac <read_line+0x7c>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <read_line+0xc9>
mov    -0x16(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11ed <read_line+0xbd>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    11f9 <read_line+0xc9>
jmp    1157 <read_line+0x27>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r13d
xor    %r15d,%r15d
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
add    $0xffffffff,%ebx
lea    0x7(%rsp),%r12
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11a2 <read_line+0x72>
movsbl 0x7(%rsp),%edi
call   1050 <tolower@plt>
cmp    $0xa,%al
je     1195 <read_line+0x65>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1160 <read_line+0x30>
xor    %r15d,%r15d
jmp    119d <read_line+0x6d>
mov    %ebp,%ebx
add    $0x1,%ebp
mov    %ebp,%r15d
movb   $0x0,(%r14,%rbx,1)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @memset(i8* %11, i8 signext 0, i32 %12)
br label %14
14: ; preds = %47, %3
%15 = load i32, i32* %5
%16 = call i64 @read(i32 %15, i8* %8, i32 1)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %48
18: ; preds = %14
%19 = load i8, i8* %8
%20 = call signext i8 @tolower(i8 signext %19)
store i8 %20, i8* %9
%21 = load i8, i8* %9
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %31
24: ; preds = %18
%25 = load i8*, i8** %6
%26 = load i32, i32* %10
%27 = add nsw i32 %26, 1
store i32 %27, i32* %10
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* %10
store i32 %30, i32* %4
br label %49
31: ; preds = %18
%32 = load i8, i8* %9
%33 = load i8*, i8** %6
%34 = load i32, i32* %10
%35 = add nsw i32 %34, 1
store i32 %35, i32* %10
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 %32, i8* %37
%38 = load i32, i32* %10
%39 = load i32, i32* %7
%40 = sub nsw i32 %39, 1
%41 = icmp eq i32 %38, %40
br i1 %41, label %42, label %47
42: ; preds = %31
%43 = load i8*, i8** %6
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
store i32 0, i32* %4
br label %49
47: ; preds = %31
br label %14
48: ; preds = %14
store i32 0, i32* %4
br label %49
49: ; preds = %48, %42, %24
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
define i32 @read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = tail call i32 @memset(i8* %1, i8 signext 0, i32 %2)
%6 = add nsw i32 %2, -1
%7 = zext i32 %6 to i64
br label %8
8: ; preds = %20, %3
%9 = phi i64 [ %21, %20 ], [ 0, %3 ]
%10 = call i64 @read(i32 %0, i8* %4, i32 1)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load i8, i8* %4
%14 = tail call signext i8 @tolower(i8 signext %13)
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = trunc i64 %9 to i32
%18 = add nuw nsw i32 %17, 1
%19 = and i64 %9, 4294967295
br label %24
20: ; preds = %12
%21 = add nuw nsw i64 %9, 1
%22 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 %14, i8* %22
%23 = icmp eq i64 %21, %7
br i1 %23, label %24, label %8
24: ; preds = %20, %16
%25 = phi i64 [ %19, %16 ], [ %7, %20 ]
%26 = phi i32 [ %18, %16 ], [ 0, %20 ]
%27 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 0, i8* %27
br label %28
28: ; preds = %8, %24
%29 = phi i32 [ %26, %24 ], [ 0, %8 ]
ret i32 %29
}