#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 /*<<< orphan*/ ends_in_newline(char *);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
char *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int strlen(char *);
int readline(char *buf, size_t length) {
  printf("> ");
  char *result = fgets(buf, length, stdin);
  if (result != NULL && !ends_in_newline(buf) && !feof(stdin)) {
    do {
      result = fgets(buf, length, stdin);
    } while (result != NULL && !ends_in_newline(buf));
    printf("Line too long\n");
    return result == NULL ? -1 : 0;
  }
  if (result == NULL) {
    return -1;
  }
  char *newline = strchr(buf, '\n');
  if (newline != NULL) {
    *newline = '\0';
  }
  return strlen(result);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123e <readline+0xde>
mov    -0x10(%rbp),%rdi
call   1080 <ends_in_newline@plt>
cmp    $0x0,%eax
jne    123e <readline+0xde>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    123e <readline+0xde>
jmp    11cf <readline+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
je     120d <readline+0xad>
mov    -0x10(%rbp),%rdi
call   1080 <ends_in_newline@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11cf <readline+0x6f>
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1285 <readline+0x125>
cmpq   $0x0,-0x20(%rbp)
jne    1255 <readline+0xf5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <readline+0x125>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1279 <readline+0x119>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe7a(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e56(%rip),%r13
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <fgets@plt>
test   %rax,%rax
je     1227 <readline+0xb7>
mov    %rax,%r15
mov    %r12,%rdi
call   1090 <ends_in_newline@plt>
test   %eax,%eax
jne    11c6 <readline+0x56>
mov    0x0(%r13),%edi
call   1080 <feof@plt>
test   %eax,%eax
je     11fc <readline+0x8c>
mov    %r12,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11db <readline+0x6b>
movb   $0x0,(%rax)
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <strlen@plt>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1090 <ends_in_newline@plt>
test   %eax,%eax
jne    1213 <readline+0xa3>
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <fgets@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f0 <readline+0x80>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%rbx
sbb    %eax,%eax
jmp    122c <readline+0xbc>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.1 = [15 x i8] c"Line too long\0A\00"
define i32 @readline(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %4
%10 = load i64, i64* %5
%11 = load i32, i32* @stdin
%12 = call i8* @fgets(i8* %9, i64 %10, i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %45
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @ends_in_newline(i8* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %45, label %19
19: ; preds = %15
%20 = load i32, i32* @stdin
%21 = call i32 @feof(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %45, label %23
23: ; preds = %19
br label %24
24: ; preds = %37, %23
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = load i32, i32* @stdin
%28 = call i8* @fgets(i8* %25, i64 %26, i32 %27)
store i8* %28, i8** %6
br label %29
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8*, i8** %4
%34 = call i32 @ends_in_newline(i8* %33)
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
br label %37
37: ; preds = %32, %29
%38 = phi i1 [ false, %29 ], [ %36, %32 ]
br i1 %38, label %24, label %39
39: ; preds = %37
%40 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%41 = load i8*, i8** %6
%42 = icmp eq i8* %41, null
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 -1, i32 0
store i32 %44, i32* %3
br label %59
45: ; preds = %19, %15, %2
%46 = load i8*, i8** %6
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 -1, i32* %3
br label %59
49: ; preds = %45
%50 = load i8*, i8** %4
%51 = call i8* @strchr(i8* %50, i8 signext 10)
store i8* %51, i8** %7
%52 = load i8*, i8** %7
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = load i8*, i8** %7
store i8 0, i8* %55
br label %56
56: ; preds = %54, %49
%57 = load i8*, i8** %6
%58 = call i32 @strlen(i8* %57)
store i32 %58, i32* %3
br label %59
59: ; preds = %56, %48, %39
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [3 x i8] c"> \00"
@stdin = global i32 0
@str = [14 x i8] c"Line too long\00"
define i32 @readline(i8* %0, i64 %1) {
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @stdin
%5 = tail call i8* @fgets(i8* %0, i64 %1, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = tail call i32 @ends_in_newline(i8* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = load i32, i32* @stdin
%12 = tail call i32 @feof(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %10, %18
%15 = load i32, i32* @stdin
%16 = tail call i8* @fgets(i8* %0, i64 %1, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = tail call i32 @ends_in_newline(i8* %0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %14, label %21
21: ; preds = %14, %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%23 = sext i1 %17 to i32
br label %30
24: ; preds = %7, %10
%25 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 10)
%26 = icmp eq i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %24
store i8 0, i8* %25
br label %28
28: ; preds = %27, %24
%29 = tail call i32 @strlen(i8* %5)
br label %30
30: ; preds = %2, %28, %21
%31 = phi i32 [ %29, %28 ], [ %23, %21 ], [ -1, %2 ]
ret i32 %31
}