#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
int strlen(char *);
char *readline(char *s, int size, FILE *stream) {
  char *rv = fgets(s, size, stream);
  if (strlen(s) == (size - 1) && s[size - 1] != '\n') {
    char buf[1024];
    fgets(buf, 1024, stream);
    while (strlen(buf) == (1024 - 1) && buf[1024 - 1] != '\n') {
      fgets(buf, 1024, stream);
    }
  }
  return rv;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11f4 <readline+0xd4>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11f4 <readline+0xd4>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x3ff,%ecx
mov    %al,-0x421(%rbp)
jne    11c2 <readline+0xa2>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x421(%rbp)
mov    -0x421(%rbp),%al
test   $0x1,%al
jne    11d5 <readline+0xb5>
jmp    11ef <readline+0xcf>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
jmp    1190 <readline+0x70>
jmp    11f4 <readline+0xd4>
mov    -0x20(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%ebp
cmp    %ebp,%eax
jne    11b9 <readline+0x99>
cltq
cmpb   $0xa,(%rbx,%rax,1)
je     11b9 <readline+0x99>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x3ff,%eax
jne    11b9 <readline+0x99>
cmpb   $0xa,0x3ff(%rsp)
0a
je     11b9 <readline+0x99>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x3ff,%eax
jne    11b9 <readline+0x99>
cmpb   $0xa,0x3ff(%rsp)
0a
jne    1190 <readline+0x70>
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @readline(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [1024 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = load i32*, i32** %6
%12 = call i8* @fgets(i8* %9, i32 %10, i32* %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = icmp eq i32 %14, %16
br i1 %17, label %18, label %47
18: ; preds = %3
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br i1 %26, label %27, label %47
27: ; preds = %18
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i8* @fgets(i8* %28, i32 1024, i32* %29)
br label %31
31: ; preds = %42, %27
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = icmp eq i32 %33, 1023
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 1023
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 10
br label %40
40: ; preds = %35, %31
%41 = phi i1 [ false, %31 ], [ %39, %35 ]
br i1 %41, label %42, label %46
42: ; preds = %40
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = load i32*, i32** %6
%45 = call i8* @fgets(i8* %43, i32 1024, i32* %44)
br label %31
46: ; preds = %40
br label %47
47: ; preds = %46, %18, %3
%48 = load i8*, i8** %7
ret i8* %48
}

LLVM IR (-O2):
define i8* @readline(i8* %0, i32 %1, i32* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = tail call i8* @fgets(i8* %0, i32 %1, i32* %2)
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %1, -1
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %31
9: ; preds = %3
%10 = sext i32 %6 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %31, label %14
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = call i8* @fgets(i8* %15, i32 1024, i32* %2)
%17 = call i32 @strlen(i8* %15)
%18 = icmp eq i32 %17, 1023
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1023
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 10
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %23, label %30
23: ; preds = %14, %23
%24 = call i8* @fgets(i8* %15, i32 1024, i32* %2)
%25 = call i32 @strlen(i8* %15)
%26 = icmp eq i32 %25, 1023
%27 = load i8, i8* %19
%28 = icmp ne i8 %27, 10
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %23, label %30
30: ; preds = %23, %14
br label %31
31: ; preds = %30, %9, %3
ret i8* %5
}