#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 EOF;
char getchar();
char read(char *string) {
  char c;
  int i = 0;
  while (1) {
    c = getchar();
    if (c == '\n' || c == EOF) {
      string[i] = '\0';
      return c;
    }
    string[i] = c;
    i++;
  }
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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     1150 <read+0x40>
movsbl -0x9(%rbp),%eax
mov    0x2ea3(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1166 <read+0x56>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movsbl -0x9(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
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)
jmp    1123 <read+0x13>

x86 assembly (clang, -O2):
<read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     115b <read+0x4b>
mov    0x2eb8(%rip),%r14
cmp    (%r14),%al
je     115b <read+0x4b>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     1158 <read+0x48>
cmp    (%r14),%al
jne    1140 <read+0x30>
mov    %ebx,%r15d
movb   $0x0,(%r12,%r15,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i8 0
define signext i8 @read(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 = %1, %22
%6 = call signext i8 (...) @getchar()
store i8 %6, i8* %3
%7 = load i8, i8* %3
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 10
br i1 %9, label %16, label %10
10: ; preds = %5
%11 = load i8, i8* %3
%12 = sext i8 %11 to i32
%13 = load i8, i8* @EOF
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %12, %14
br i1 %15, label %16, label %22
16: ; preds = %10, %5
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 0, i8* %20
%21 = load i8, i8* %3
ret i8 %21
22: ; preds = %10
%23 = load i8, i8* %3
%24 = load i8*, i8** %2
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %5
}

LLVM IR (-O2):
@EOF = global i8 0
define signext i8 @read(i8* nocapture writeonly %0) {
%2 = tail call signext i8 (...) @getchar()
%3 = icmp eq i8 %2, 10
%4 = load i8, i8* @EOF
%5 = icmp eq i8 %2, %4
%6 = select i1 %3, i1 true, i1 %5
br i1 %6, label %9, label %13
7: ; preds = %13
%8 = and i64 %17, 4294967295
br label %9
9: ; preds = %7, %1
%10 = phi i64 [ 0, %1 ], [ %8, %7 ]
%11 = phi i8 [ %2, %1 ], [ %18, %7 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %12
ret i8 %11
13: ; preds = %1, %13
%14 = phi i64 [ %17, %13 ], [ 0, %1 ]
%15 = phi i8 [ %18, %13 ], [ %2, %1 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 %15, i8* %16
%17 = add nuw i64 %14, 1
%18 = tail call signext i8 (...) @getchar()
%19 = icmp eq i8 %18, 10
%20 = load i8, i8* @EOF
%21 = icmp eq i8 %18, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %7, label %13
}