#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 */
char getchar();
void read_string(char *s) {
  char c;
  int i = 0;
  do {
    c = getchar();
    if (c != '\n') {
      s[i] = c;
    }
    i++;
  } while (c != '\n' && c != '\0');
  s[--i] = '\0';
  return;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     1148 <read_string+0x38>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movsbl -0x9(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x11(%rbp)
je     1170 <read_string+0x60>
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1123 <read_string+0x13>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ecx,%ecx
nopl   0x0(%rax)
mov    %rcx,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     113a <read_string+0x2a>
mov    %al,(%r14,%rbx,1)
lea    0x1(%rbx),%rcx
test   %al,%al
jne    1120 <read_string+0x10>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define void @read_string(i8* nocapture writeonly %0) {
br label %2
2: ; preds = %6, %1
%3 = phi i64 [ %8, %6 ], [ 0, %1 ]
%4 = tail call signext i8 (...) @getchar()
%5 = icmp eq i8 %4, 10
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 %3
store i8 %4, i8* %7
%8 = add nuw i64 %3, 1
%9 = icmp eq i8 %4, 0
br i1 %9, label %10, label %2
10: ; preds = %6, %2
%11 = and i64 %3, 4294967295
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
ret void
}