#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*/ readbuf;

/* Variables and functions */
int EINTR;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int PMIU_readline(int fd, char *buf, int maxlen) {
  static char readbuf[1024];
  static char *nextChar = 0, *lastChar = 0;
  static int lastErrno = 0;
  static int lastfd = -1;
  int curlen, n;
  char *p, ch;
  if (nextChar != lastChar && fd != lastfd) {
    fprintf(stderr, "Panic - buffer inconsistent\n");
    return -1;
  }
  p = buf;
  curlen = 1;
  while (curlen < maxlen) {
    if (nextChar == lastChar) {
      lastfd = fd;
      do {
        n = read(fd, readbuf, sizeof(readbuf) - 1);
      } while (n == -1 && errno == EINTR);
      if (n == 0) {
        break;
      } else if (n < 0) {
        lastErrno = errno;
        if (curlen == 1) {
          curlen = 0;
        }
        break;
      }
      nextChar = readbuf;
      lastChar = readbuf + n;
      readbuf[n] = 0;
    }
    ch = *nextChar++;
    *p++ = ch;
    curlen++;
    if (ch == '\n')
      break;
  }
  *p = 0;
  return curlen - 1;
}


x86 assembly (clang, -O0):
<PMIU_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x3317(%rip),%rax
cmp    0x3318(%rip),%rax
je     1176 <PMIU_readline+0x56>
mov    -0x8(%rbp),%eax
cmp    0x2ee1(%rip),%eax
je     1176 <PMIU_readline+0x56>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <PMIU_readline+0x1aa>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12ba <PMIU_readline+0x19a>
mov    0x32b8(%rip),%rax
cmp    0x32b9(%rip),%rax
jne    126c <PMIU_readline+0x14c>
mov    -0x8(%rbp),%eax
mov    %eax,0x2e82(%rip)
mov    -0x8(%rbp),%edi
lea    0x2e98(%rip),%rsi
mov    $0x3ff,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x2a(%rbp)
jne    11ec <PMIU_readline+0xcc>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    11ae <PMIU_readline+0x8e>
cmpl   $0x0,-0x1c(%rbp)
jne    1206 <PMIU_readline+0xe6>
jmp    12ba <PMIU_readline+0x19a>
cmpl   $0x0,-0x1c(%rbp)
jge    1235 <PMIU_readline+0x115>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x3241(%rip)
cmpl   $0x1,-0x18(%rbp)
jne    1230 <PMIU_readline+0x110>
movl   $0x0,-0x18(%rbp)
jmp    12ba <PMIU_readline+0x19a>
jmp    123a <PMIU_readline+0x11a>
lea    0x2e0f(%rip),%rax
mov    %rax,0x3208(%rip)
movslq -0x1c(%rbp),%rcx
lea    0x2dfd(%rip),%rax
add    %rcx,%rax
mov    %rax,0x31fb(%rip)
movslq -0x1c(%rbp),%rcx
lea    0x2de8(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    0x31dd(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x31cf(%rip)
mov    (%rax),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    12b5 <PMIU_readline+0x195>
jmp    12ba <PMIU_readline+0x19a>
jmp    1185 <PMIU_readline+0x65>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PMIU_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x3319(%rip),%rcx
mov    0x331a(%rip),%rsi
cmp    %rsi,%rcx
je     116b <PMIU_readline+0x4b>
cmp    %ebx,0x2ee7(%rip)
je     116b <PMIU_readline+0x4b>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
lea    0xea5(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%r12d
jmp    1224 <PMIU_readline+0x104>
cmp    $0x2,%edx
mov    $0x1,%r12d
cmovl  %r12d,%edx
lea    0x2ed1(%rip),%rbp
mov    0x2e52(%rip),%r13
mov    0x2e43(%rip),%r14
mov    %edx,0x4(%rsp)
jmp    11bf <PMIU_readline+0x9f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%rcx),%rdi
mov    %rdi,0x32a5(%rip)
mov    (%rcx),%al
mov    %al,(%r15)
add    $0x1,%r15
add    $0x1,%r12d
mov    %rdi,%rcx
cmp    $0xa,%al
je     121c <PMIU_readline+0xfc>
cmp    %edx,%r12d
je     120d <PMIU_readline+0xed>
cmp    %rsi,%rcx
jne    11a0 <PMIU_readline+0x80>
mov    %ebx,0x2e61(%rip)
nop
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    11ed <PMIU_readline+0xcd>
mov    (%r14),%ecx
cmp    %ecx,0x0(%r13)
je     11d0 <PMIU_readline+0xb0>
test   %eax,%eax
je     121c <PMIU_readline+0xfc>
js     1212 <PMIU_readline+0xf2>
mov    %eax,%ecx
lea    (%rcx,%rbp,1),%rsi
mov    %rsi,0x3258(%rip)
movb   $0x0,(%rcx,%rbp,1)
mov    %rbp,%rcx
mov    0x4(%rsp),%edx
jmp    11a0 <PMIU_readline+0x80>
mov    %edx,%r12d
jmp    121c <PMIU_readline+0xfc>
xor    %eax,%eax
cmp    $0x1,%r12d
cmove  %eax,%r12d
movb   $0x0,(%r15)
add    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PMIU_readline.readbuf = internal global [1024 x i8] zeroinitializer
@PMIU_readline.nextChar = internal global i8* null
@PMIU_readline.lastChar = internal global i8* null
@PMIU_readline.lastErrno = internal global i32 0
@PMIU_readline.lastfd = internal global i32 -1
@stderr = global i32 0
@.str = [29 x i8] c"Panic - buffer inconsistent\0A\00"
@errno = global i32 0
@EINTR = global i32 0
define i32 @PMIU_readline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** @PMIU_readline.nextChar
%13 = load i8*, i8** @PMIU_readline.lastChar
%14 = icmp ne i8* %12, %13
br i1 %14, label %15, label %22
15: ; preds = %3
%16 = load i32, i32* %5
%17 = load i32, i32* @PMIU_readline.lastfd
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %85
22: ; preds = %15, %3
%23 = load i8*, i8** %6
store i8* %23, i8** %10
store i32 1, i32* %8
br label %24
24: ; preds = %80, %22
%25 = load i32, i32* %8
%26 = load i32, i32* %7
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %81
28: ; preds = %24
%29 = load i8*, i8** @PMIU_readline.nextChar
%30 = load i8*, i8** @PMIU_readline.lastChar
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %67
32: ; preds = %28
%33 = load i32, i32* %5
store i32 %33, i32* @PMIU_readline.lastfd
br label %34
34: ; preds = %44, %32
%35 = load i32, i32* %5
%36 = call i32 @read(i32 %35, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 0), i32 1023)
store i32 %36, i32* %9
br label %37
37: ; preds = %34
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = load i32, i32* @EINTR
%43 = icmp eq i32 %41, %42
br label %44
44: ; preds = %40, %37
%45 = phi i1 [ false, %37 ], [ %43, %40 ]
br i1 %45, label %34, label %46
46: ; preds = %44
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %46
br label %81
50: ; preds = %46
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load i32, i32* @errno
store i32 %54, i32* @PMIU_readline.lastErrno
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, 1
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 0, i32* %8
br label %58
58: ; preds = %57, %53
br label %81
59: ; preds = %50
br label %60
60: ; preds = %59
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 0), i8** @PMIU_readline.nextChar
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 0), i64 %62
store i8* %63, i8** @PMIU_readline.lastChar
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 %65
store i8 0, i8* %66
br label %67
67: ; preds = %60, %28
%68 = load i8*, i8** @PMIU_readline.nextChar
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** @PMIU_readline.nextChar
%70 = load i8, i8* %68
store i8 %70, i8* %11
%71 = load i8, i8* %11
%72 = load i8*, i8** %10
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %10
store i8 %71, i8* %72
%74 = load i32, i32* %8
%75 = add nsw i32 %74, 1
store i32 %75, i32* %8
%76 = load i8, i8* %11
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 10
br i1 %78, label %79, label %80
79: ; preds = %67
br label %81
80: ; preds = %67
br label %24
81: ; preds = %79, %58, %49, %24
%82 = load i8*, i8** %10
store i8 0, i8* %82
%83 = load i32, i32* %8
%84 = sub nsw i32 %83, 1
store i32 %84, i32* %4
br label %85
85: ; preds = %81, %19
%86 = load i32, i32* %4
ret i32 %86
}

LLVM IR (-O2):
@PMIU_readline.readbuf = internal global [1024 x i8] zeroinitializer
@PMIU_readline.nextChar = internal unnamed_addr global i8* null
@PMIU_readline.lastChar = internal unnamed_addr global i8* null
@PMIU_readline.lastfd = internal unnamed_addr global i32 -1
@stderr = global i32 0
@.str = [29 x i8] c"Panic - buffer inconsistent\0A\00"
@errno = global i32 0
@EINTR = global i32 0
define i32 @PMIU_readline(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = load i8*, i8** @PMIU_readline.nextChar
%5 = load i8*, i8** @PMIU_readline.lastChar
%6 = icmp eq i8* %4, %5
%7 = load i32, i32* @PMIU_readline.lastfd
%8 = icmp eq i32 %7, %0
%9 = select i1 %6, i1 true, i1 %8
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = call i32 @llvm.smax.i32(i32 %2, i32 1)
br label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %53
15: ; preds = %10, %41
%16 = phi i8* [ %43, %41 ], [ %5, %10 ]
%17 = phi i8* [ %44, %41 ], [ %4, %10 ]
%18 = phi i32 [ %47, %41 ], [ 1, %10 ]
%19 = phi i8* [ %46, %41 ], [ %1, %10 ]
%20 = icmp eq i32 %18, %11
br i1 %20, label %49, label %21
21: ; preds = %15
%22 = icmp eq i8* %17, %16
br i1 %22, label %23, label %41
23: ; preds = %21
store i32 %0, i32* @PMIU_readline.lastfd
br label %24
24: ; preds = %24, %23
%25 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 0), i32 1023)
%26 = icmp eq i32 %25, -1
%27 = load i32, i32* @errno
%28 = load i32, i32* @EINTR
%29 = icmp eq i32 %27, %28
%30 = select i1 %26, i1 %29, i1 false
br i1 %30, label %24, label %31
31: ; preds = %24
%32 = icmp eq i32 %25, 0
br i1 %32, label %49, label %33
33: ; preds = %31
%34 = icmp slt i32 %25, 0
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = icmp eq i32 %18, 1
%37 = select i1 %36, i32 0, i32 %18
br label %49
38: ; preds = %33
%39 = zext i32 %25 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 %39
store i8* %40, i8** @PMIU_readline.lastChar
store i8 0, i8* %40
br label %41
41: ; preds = %38, %21
%42 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMIU_readline.readbuf, i64 0, i64 0), %38 ], [ %17, %21 ]
%43 = phi i8* [ %40, %38 ], [ %16, %21 ]
%44 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %44, i8** @PMIU_readline.nextChar
%45 = load i8, i8* %42
%46 = getelementptr inbounds i8, i8* %19, i64 1
store i8 %45, i8* %19
%47 = add nuw i32 %18, 1
%48 = icmp eq i8 %45, 10
br i1 %48, label %49, label %15
49: ; preds = %41, %31, %15, %35
%50 = phi i32 [ %37, %35 ], [ %11, %15 ], [ %47, %41 ], [ %18, %31 ]
%51 = phi i8* [ %19, %35 ], [ %19, %15 ], [ %46, %41 ], [ %19, %31 ]
store i8 0, i8* %51
%52 = add nsw i32 %50, -1
br label %53
53: ; preds = %49, %12
%54 = phi i32 [ -1, %12 ], [ %52, %49 ]
ret i32 %54
}