#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(int, char *, int);
int get_line(int fd, char *buf, size_t buf_size) {
  size_t pos;
  char ch = 'a';
  (void)buf_size;
  for (pos = 0; ch != '\n';) {
    if (read(fd, &ch, 1) == 1) {
      buf[pos++] = (char)(ch == '\n' ? '\0' : ch);
    } else {
      buf[pos] = '\0';
      break;
    }
  }
  return pos;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x61,-0x21(%rbp)
movq   $0x0,-0x20(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     11aa <get_line+0x9a>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1194 <get_line+0x84>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    116d <get_line+0x5d>
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
jmp    1174 <get_line+0x64>
movsbl -0x21(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    11a5 <get_line+0x95>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11aa <get_line+0x9a>
jmp    112f <get_line+0x1f>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
movb   $0x61,0xf(%rsp)
xor    %r15d,%r15d
lea    0xf(%rsp),%r14
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    115f <get_line+0x4f>
movzbl 0xf(%rsp),%ecx
lea    0x1(%rbx),%rax
cmp    $0xa,%cl
cmove  %r15d,%ecx
mov    %cl,(%r12,%rbx,1)
mov    %rax,%rbx
jne    1130 <get_line+0x20>
jmp    1167 <get_line+0x57>
movb   $0x0,(%r12,%rbx,1)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @get_line(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
store i8 97, i8* %4
br label %5
5: ; preds = %3, %9
%6 = phi i64 [ 0, %3 ], [ %13, %9 ]
%7 = call i32 @read(i32 %0, i8* %4, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = load i8, i8* %4
%11 = icmp eq i8 %10, 10
%12 = select i1 %11, i8 0, i8 %10
%13 = add i64 %6, 1
%14 = getelementptr inbounds i8, i8* %1, i64 %6
store i8 %12, i8* %14
br i1 %11, label %17, label %5
15: ; preds = %5
%16 = getelementptr inbounds i8, i8* %1, i64 %6
store i8 0, i8* %16
br label %17
17: ; preds = %9, %15
%18 = phi i64 [ %6, %15 ], [ %13, %9 ]
%19 = trunc i64 %18 to i32
ret i32 %19
}