#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 const, char *, int);
int get_line(const int fd, char *line) {
  static int nb = 4096;
  static int k = 0;
  static char buff[4096];
  if (k == 0 || k >= nb) {
    k = 0;
    if ((nb = read(fd, buff, 4096)) < 0)
      return (0);
  }
  if (nb == 0) {
    line[0] = '\0';
    k = -1;
  } else if (buff[k] == '\n')
    line[0] = '\0';
  else
    line[0] = buff[k];
  k = k + 1;
  return (nb);
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,0x2f1a(%rip)
je     113e <get_line+0x2e>
mov    0x2f0e(%rip),%eax
cmp    0x2ef0(%rip),%eax
jl     117c <get_line+0x6c>
movl   $0x0,0x2ef8(%rip)
mov    -0x8(%rbp),%edi
lea    0x2efe(%rip),%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
mov    %eax,0x2ec6(%rip)
cmp    $0x0,%eax
jge    1177 <get_line+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11fa <get_line+0xea>
jmp    117c <get_line+0x6c>
cmpl   $0x0,0x2ea5(%rip)
jne    119f <get_line+0x8f>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,0x2ea6(%rip)
ff ff ff
jmp    11e2 <get_line+0xd2>
movslq 0x2e9a(%rip),%rcx
lea    0x2ea3(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11c6 <get_line+0xb6>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11dd <get_line+0xcd>
movslq 0x2e73(%rip),%rcx
lea    0x2e7c(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
jmp    11e2 <get_line+0xd2>
mov    0x2e58(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e4f(%rip)
mov    0x2e31(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbx
mov    %rsi,%rbx
mov    0x2f26(%rip),%ecx
test   %ecx,%ecx
je     1128 <get_line+0x18>
mov    0x2f04(%rip),%eax
cmp    %eax,%ecx
jl     114d <get_line+0x3d>
movl   $0x0,0x2f0e(%rip)
lea    0x2f17(%rip),%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
mov    %eax,0x2edf(%rip)
test   %eax,%eax
js     117f <get_line+0x6f>
test   %eax,%eax
je     1171 <get_line+0x61>
movslq 0x2ee8(%rip),%rdx
lea    0x2ef1(%rip),%rcx
movzbl (%rdx,%rcx,1),%esi
xor    %ecx,%ecx
cmp    $0xa,%sil
cmovne %esi,%ecx
add    $0x1,%edx
jmp    1175 <get_line+0x65>
xor    %ecx,%ecx
xor    %edx,%edx
mov    %cl,(%rbx)
mov    %edx,0x2ec3(%rip)
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@get_line.nb = internal global i32 4096
@get_line.k = internal global i32 0
@get_line.buff = internal global [4096 x i8] zeroinitializer
define i32 @get_line(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%6 = load i32, i32* @get_line.k
%7 = icmp eq i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @get_line.k
%10 = load i32, i32* @get_line.nb
%11 = icmp sge i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %8, %2
store i32 0, i32* @get_line.k
%13 = load i32, i32* %4
%14 = call i32 @read(i32 %13, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_line.buff, i64 0, i64 0), i32 4096)
store i32 %14, i32* @get_line.nb
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 0, i32* %3
br label %46
17: ; preds = %12
br label %18
18: ; preds = %17, %8
%19 = load i32, i32* @get_line.nb
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
store i32 -1, i32* @get_line.k
br label %42
24: ; preds = %18
%25 = load i32, i32* @get_line.k
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_line.buff, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 0
store i8 0, i8* %33
br label %41
34: ; preds = %24
%35 = load i32, i32* @get_line.k
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_line.buff, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 0
store i8 %38, i8* %40
br label %41
41: ; preds = %34, %31
br label %42
42: ; preds = %41, %21
%43 = load i32, i32* @get_line.k
%44 = add nsw i32 %43, 1
store i32 %44, i32* @get_line.k
%45 = load i32, i32* @get_line.nb
store i32 %45, i32* %3
br label %46
46: ; preds = %42, %16
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@get_line.nb = internal unnamed_addr global i32 4096
@get_line.k = internal unnamed_addr global i32 0
@get_line.buff = internal global [4096 x i8] zeroinitializer
define i32 @get_line(i32 %0, i8* nocapture writeonly %1) {
%3 = load i32, i32* @get_line.k
%4 = icmp ne i32 %3, 0
%5 = load i32, i32* @get_line.nb
%6 = icmp slt i32 %3, %5
%7 = select i1 %4, i1 %6, i1 false
br i1 %7, label %11, label %8
8: ; preds = %2
store i32 0, i32* @get_line.k
%9 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_line.buff, i64 0, i64 0), i32 4096)
store i32 %9, i32* @get_line.nb
%10 = icmp slt i32 %9, 0
br i1 %10, label %25, label %11
11: ; preds = %2, %8
%12 = phi i32 [ %5, %2 ], [ %9, %8 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = load i32, i32* @get_line.k
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_line.buff, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
%20 = select i1 %19, i8 0, i8 %18
%21 = add i32 %15, 1
br label %22
22: ; preds = %14, %11
%23 = phi i8 [ 0, %11 ], [ %20, %14 ]
%24 = phi i32 [ 0, %11 ], [ %21, %14 ]
store i8 %23, i8* %1
store i32 %24, i32* @get_line.k
br label %25
25: ; preds = %8, %22
%26 = phi i32 [ %12, %22 ], [ 0, %8 ]
ret i32 %26
}