#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*/ scanf(char *, char *);
int read_line(char *buffer) {
  char c = 0;
  int counter = 0;
  while (1) {
    scanf("%c", &c);
    if (c == '\n')
      break;
    buffer[counter++] = c;
  }
  return counter;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x0,-0x9(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xed2(%rip),%rdi
lea    -0x9(%rbp),%rsi
call   1030 <scanf@plt>
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
jne    1149 <read_line+0x39>
jmp    1166 <read_line+0x56>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1127 <read_line+0x17>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
movb   $0x0,0x7(%rsp)
lea    0xed9(%rip),%rdi
lea    0x7(%rsp),%rsi
call   1030 <scanf@plt>
mov    0x7(%rsp),%al
xor    %ebx,%ebx
cmp    $0xa,%al
je     116c <read_line+0x5c>
xor    %ebx,%ebx
lea    0xebc(%rip),%r15
lea    0x7(%rsp),%r12
nopl   0x0(%rax)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <scanf@plt>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
jne    1150 <read_line+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
define i32 @read_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i32
store i8* %0, i8** %2
store i8 0, i8* %3
store i32 0, i32* %4
br label %5
5: ; preds = %1, %11
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
%7 = load i8, i8* %3
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 10
br i1 %9, label %10, label %11
10: ; preds = %5
br label %18
11: ; preds = %5
%12 = load i8, i8* %3
%13 = load i8*, i8** %2
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 %12, i8* %17
br label %5
18: ; preds = %10
%19 = load i32, i32* %4
ret i32 %19
}

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
define i32 @read_line(i8* nocapture writeonly %0) {
%2 = alloca i8
store i8 0, i8* %2
%3 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%4 = load i8, i8* %2
%5 = icmp eq i8 %4, 10
br i1 %5, label %16, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %9, %6 ], [ 0, %1 ]
%8 = phi i8 [ %12, %6 ], [ %4, %1 ]
%9 = add nuw i64 %7, 1
%10 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 %8, i8* %10
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%12 = load i8, i8* %2
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %6
14: ; preds = %6
%15 = trunc i64 %9 to i32
br label %16
16: ; preds = %14, %1
%17 = phi i32 [ 0, %1 ], [ %15, %14 ]
ret i32 %17
}