#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char);
int readline(int fd, void *buffer, ssize_t max_length) {
  if (!buffer || max_length < 0) {
    perror("readline: bad buffer or length!");
    return -1;
  }
  char c;
  char *buffer_index = buffer;
  int read_bytes;
  int ret_count = 0;
  do {
    read_bytes = read(fd, &c, 1);
    if (read_bytes == 1) {
      sprintf(buffer_index, "%c", c);
      ret_count++;
      buffer_index++;
    } else if (read_bytes < 0) {
      perror("readline.read: bad read");
      return -1;
    } else if (read_bytes == 0) {
      perror("nothing to read");
      return 0;
    }
    if (ret_count >= max_length)
      break;
  } while (c != '\n');
  *buffer_index = '\0';
  return ret_count;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1157 <readline+0x27>
cmpl   $0x0,-0x14(%rbp)
jge    116f <readline+0x3f>
lea    0xea2(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <readline+0x118>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11ca <readline+0x9a>
mov    -0x20(%rbp),%rdi
lea    0xe79(%rip),%rsi
movsbl -0x15(%rbp),%edx
call   1050 <sprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1218 <readline+0xe8>
cmpl   $0x0,-0x24(%rbp)
jge    11ec <readline+0xbc>
lea    0xe48(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <readline+0x118>
cmpl   $0x0,-0x24(%rbp)
jne    120e <readline+0xde>
lea    0xe3e(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1248 <readline+0x118>
jmp    1213 <readline+0xe3>
jmp    1218 <readline+0xe8>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1229 <readline+0xf9>
jmp    123b <readline+0x10b>
jmp    122e <readline+0xfe>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    117e <readline+0x4e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     11a4 <readline+0x84>
mov    %edx,%r14d
test   %edx,%edx
js     11a4 <readline+0x84>
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %ebp,%ebp
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1180 <readline+0x60>
movzbl 0xf(%rsp),%eax
mov    %al,(%rbx)
movb   $0x0,0x1(%rbx)
add    $0x1,%rbx
add    $0x1,%ebp
jmp    1186 <readline+0x66>
nopl   0x0(%rax)
test   %eax,%eax
js     11ad <readline+0x8d>
je     11c0 <readline+0xa0>
cmp    %r14d,%ebp
jge    1192 <readline+0x72>
cmpb   $0xa,0xf(%rsp)
jne    1150 <readline+0x30>
movb   $0x0,(%rbx)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe55(%rip),%rdi
jmp    11b4 <readline+0x94>
lea    0xe6c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1195 <readline+0x75>
lea    0xe71(%rip),%rdi
call   1040 <perror@plt>
xor    %ebp,%ebp
jmp    1195 <readline+0x75>

LLVM IR (-O0):
@.str = [32 x i8] c"readline: bad buffer or length!\00"
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [24 x i8] c"readline.read: bad read\00"
@.str.3 = [16 x i8] c"nothing to read\00"
define i32 @readline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14, %3
%18 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
19: ; preds = %14
%20 = load i8*, i8** %6
store i8* %20, i8** %9
store i32 0, i32* %11
br label %21
21: ; preds = %52, %19
%22 = load i32, i32* %5
%23 = call i32 @read(i32 %22, i8* %8, i32 1)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = load i8*, i8** %9
%28 = load i8, i8* %8
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 signext %28)
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
store i32 %31, i32* %11
%32 = load i8*, i8** %9
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %9
br label %46
34: ; preds = %21
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
39: ; preds = %34
%40 = load i32, i32* %10
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %59
44: ; preds = %39
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45, %26
%47 = load i32, i32* %11
%48 = load i32, i32* %7
%49 = icmp sge i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %56
51: ; preds = %46
br label %52
52: ; preds = %51
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 10
br i1 %55, label %21, label %56
56: ; preds = %52, %50
%57 = load i8*, i8** %9
store i8 0, i8* %57
%58 = load i32, i32* %11
store i32 %58, i32* %4
br label %59
59: ; preds = %56, %42, %37, %17
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@.str = [32 x i8] c"readline: bad buffer or length!\00"
@.str.2 = [24 x i8] c"readline.read: bad read\00"
@.str.3 = [16 x i8] c"nothing to read\00"
define i32 @readline(i32 %0, i8* writeonly %1, i32 %2) {
%4 = alloca i8
%5 = icmp eq i8* %1, null
%6 = icmp slt i32 %2, 0
%7 = or i1 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %38
10: ; preds = %3
br label %11
11: ; preds = %28, %10
%12 = phi i8* [ %1, %10 ], [ %29, %28 ]
%13 = phi i32 [ 0, %10 ], [ %30, %28 ]
%14 = call i32 @read(i32 %0, i8* %4, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8, i8* %4
store i8 %17, i8* %12
%18 = getelementptr i8, i8* %12, i64 1
store i8 0, i8* %18
%19 = add nsw i32 %13, 1
br label %28
20: ; preds = %11
%21 = icmp slt i32 %14, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %36
24: ; preds = %20
%25 = icmp eq i32 %14, 0
br i1 %25, label %26, label %28
26: ; preds = %24
%27 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %36
28: ; preds = %24, %16
%29 = phi i8* [ %18, %16 ], [ %12, %24 ]
%30 = phi i32 [ %19, %16 ], [ %13, %24 ]
%31 = icmp slt i32 %30, %2
%32 = load i8, i8* %4
%33 = icmp ne i8 %32, 10
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %11, label %35
35: ; preds = %28
store i8 0, i8* %29
br label %36
36: ; preds = %35, %26, %22
%37 = phi i32 [ %30, %35 ], [ -1, %22 ], [ 0, %26 ]
br label %38
38: ; preds = %36, %8
%39 = phi i32 [ -1, %8 ], [ %37, %36 ]
ret i32 %39
}