#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, char *, int);
int PMI2U_readline(int fd, char *buf, int maxlen) {
  static char readbuf[1024];
  static char *nextChar = 0, *lastChar = 0;
  int curlen, n;
  char *p, ch;
  p = buf;
  curlen = 1;
  while (curlen < maxlen) {
    if (nextChar == lastChar) {
      do {
        n = read(fd, readbuf, sizeof(readbuf) - 1);
      } while (n == -1 && errno == EINTR);
      if (n == 0) {
        break;
      } else if (n < 0) {
        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):
<PMI2U_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1255 <PMI2U_readline+0x145>
mov    0x32fc(%rip),%rax
cmp    0x32fd(%rip),%rax
jne    1207 <PMI2U_readline+0xf7>
jmp    1156 <PMI2U_readline+0x46>
mov    -0x4(%rbp),%edi
lea    0x2ee0(%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    1196 <PMI2U_readline+0x86>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    1156 <PMI2U_readline+0x46>
cmpl   $0x0,-0x1c(%rbp)
jne    11b0 <PMI2U_readline+0xa0>
jmp    1255 <PMI2U_readline+0x145>
cmpl   $0x0,-0x1c(%rbp)
jge    11d0 <PMI2U_readline+0xc0>
cmpl   $0x1,-0x18(%rbp)
jne    11cb <PMI2U_readline+0xbb>
movl   $0x0,-0x18(%rbp)
jmp    1255 <PMI2U_readline+0x145>
jmp    11d5 <PMI2U_readline+0xc5>
lea    0x2e64(%rip),%rax
mov    %rax,0x325d(%rip)
movslq -0x1c(%rbp),%rcx
lea    0x2e52(%rip),%rax
add    %rcx,%rax
mov    %rax,0x3250(%rip)
movslq -0x1c(%rbp),%rcx
lea    0x2e3d(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    0x3232(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x3224(%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    1250 <PMI2U_readline+0x140>
jmp    1255 <PMI2U_readline+0x145>
jmp    1131 <PMI2U_readline+0x21>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PMI2U_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edx
mov    $0x1,%r12d
cmovl  %r12d,%edx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x2f0c(%rip),%rbx
mov    0x2ea5(%rip),%r13
mov    0x2e96(%rip),%r15
mov    %edx,0x4(%rsp)
jmp    116c <PMI2U_readline+0x5c>
nopl   0x0(%rax,%rax,1)
lea    0x1(%rax),%rcx
mov    %rcx,0x32e5(%rip)
mov    (%rax),%al
mov    %al,(%r14)
add    $0x1,%r14
add    $0x1,%r12d
cmp    $0xa,%al
je     11dc <PMI2U_readline+0xcc>
cmp    %edx,%r12d
je     11cd <PMI2U_readline+0xbd>
mov    0x32c8(%rip),%rax
cmp    0x32c9(%rip),%rax
jne    1150 <PMI2U_readline+0x40>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    11ad <PMI2U_readline+0x9d>
mov    (%r15),%rcx
cmp    %rcx,0x0(%r13)
je     1190 <PMI2U_readline+0x80>
test   %eax,%eax
je     11dc <PMI2U_readline+0xcc>
js     11d2 <PMI2U_readline+0xc2>
mov    %eax,%eax
lea    (%rbx,%rax,1),%rcx
mov    %rcx,0x3288(%rip)
movb   $0x0,(%rax,%rbx,1)
mov    %rbx,%rax
mov    0x4(%rsp),%edx
jmp    1150 <PMI2U_readline+0x40>
mov    %edx,%r12d
jmp    11dc <PMI2U_readline+0xcc>
xor    %eax,%eax
cmp    $0x1,%r12d
cmove  %eax,%r12d
movb   $0x0,(%r14)
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):
@PMI2U_readline.readbuf = internal global [1024 x i8] zeroinitializer
@PMI2U_readline.nextChar = internal global i8* null
@PMI2U_readline.lastChar = internal global i8* null
@errno = global i64 0
@EINTR = global i64 0
define i32 @PMI2U_readline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
store i8* %11, i8** %9
store i32 1, i32* %7
br label %12
12: ; preds = %66, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %67
16: ; preds = %12
%17 = load i8*, i8** @PMI2U_readline.nextChar
%18 = load i8*, i8** @PMI2U_readline.lastChar
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %53
20: ; preds = %16
br label %21
21: ; preds = %31, %20
%22 = load i32, i32* %4
%23 = call i32 @read(i32 %22, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 0), i32 1023)
store i32 %23, i32* %8
br label %24
24: ; preds = %21
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br label %31
31: ; preds = %27, %24
%32 = phi i1 [ false, %24 ], [ %30, %27 ]
br i1 %32, label %21, label %33
33: ; preds = %31
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
br label %67
37: ; preds = %33
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 0, i32* %7
br label %44
44: ; preds = %43, %40
br label %67
45: ; preds = %37
br label %46
46: ; preds = %45
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 0), i8** @PMI2U_readline.nextChar
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 0), i64 %48
store i8* %49, i8** @PMI2U_readline.lastChar
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %46, %16
%54 = load i8*, i8** @PMI2U_readline.nextChar
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** @PMI2U_readline.nextChar
%56 = load i8, i8* %54
store i8 %56, i8* %10
%57 = load i8, i8* %10
%58 = load i8*, i8** %9
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %9
store i8 %57, i8* %58
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
%62 = load i8, i8* %10
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %66
65: ; preds = %53
br label %67
66: ; preds = %53
br label %12
67: ; preds = %65, %44, %36, %12
%68 = load i8*, i8** %9
store i8 0, i8* %68
%69 = load i32, i32* %7
%70 = sub nsw i32 %69, 1
ret i32 %70
}

LLVM IR (-O2):
@PMI2U_readline.readbuf = internal global [1024 x i8] zeroinitializer
@PMI2U_readline.nextChar = internal unnamed_addr global i8* null
@PMI2U_readline.lastChar = internal unnamed_addr global i8* null
@errno = global i64 0
@EINTR = global i64 0
define i32 @PMI2U_readline(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = call i32 @llvm.smax.i32(i32 %2, i32 1)
br label %5
5: ; preds = %30, %3
%6 = phi i8* [ %1, %3 ], [ %34, %30 ]
%7 = phi i32 [ 1, %3 ], [ %35, %30 ]
%8 = icmp eq i32 %7, %4
br i1 %8, label %37, label %9
9: ; preds = %5
%10 = load i8*, i8** @PMI2U_readline.nextChar
%11 = load i8*, i8** @PMI2U_readline.lastChar
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %30
13: ; preds = %9, %13
%14 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 0), i32 1023)
%15 = icmp eq i32 %14, -1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %13, label %20
20: ; preds = %13
%21 = icmp eq i32 %14, 0
br i1 %21, label %37, label %22
22: ; preds = %20
%23 = icmp slt i32 %14, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = icmp eq i32 %7, 1
%26 = select i1 %25, i32 0, i32 %7
br label %37
27: ; preds = %22
%28 = zext i32 %14 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 %28
store i8* %29, i8** @PMI2U_readline.lastChar
store i8 0, i8* %29
br label %30
30: ; preds = %27, %9
%31 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @PMI2U_readline.readbuf, i64 0, i64 0), %27 ], [ %10, %9 ]
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8* %32, i8** @PMI2U_readline.nextChar
%33 = load i8, i8* %31
%34 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %33, i8* %6
%35 = add nuw i32 %7, 1
%36 = icmp eq i8 %33, 10
br i1 %36, label %37, label %5
37: ; preds = %30, %20, %5, %24
%38 = phi i8* [ %6, %24 ], [ %6, %5 ], [ %34, %30 ], [ %6, %20 ]
%39 = phi i32 [ %26, %24 ], [ %4, %5 ], [ %35, %30 ], [ %7, %20 ]
store i8 0, i8* %38
%40 = add nsw i32 %39, -1
ret i32 %40
}