#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*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
char *realloc(char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ *strstr(char *, char *);
int readLine(int fd, char **buffer, int *size, int *max) {
  int recv_size, ret;
  recv_size = ret = 0;
  char tmp[32];
  *size = 0;
  memset(*buffer, 0, *max);
  memset(tmp, 0, 32);
  recv_size = read(fd, tmp, 1);
  strncat(*buffer, tmp, 1);
  if (recv_size == 0)
    return -1;
  while (strstr(*buffer, "\r\n") == NULL) {
    ++ret;
    if (++*size > *max) {
      *max = *max * 2;
      *buffer = realloc(*buffer, *max);
    }
    recv_size = read(fd, tmp, 1);
    strncat(*buffer, tmp, 1);
    if (recv_size < 0) {
      return -1;
    }
  }
  if (strcmp("\r\n", *buffer) == 0)
    return -2;
  return ret;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <memset@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
cmpl   $0x0,-0x24(%rbp)
jne    11f2 <readLine+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <readLine+0x172>
jmp    11f7 <readLine+0x97>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfb(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
jne    12a3 <readLine+0x143>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rcx
mov    (%rcx),%eax
add    $0x1,%eax
mov    %eax,(%rcx)
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jle    125f <readLine+0xff>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
cmpl   $0x0,-0x24(%rbp)
jge    129e <readLine+0x13e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <readLine+0x172>
jmp    11f7 <readLine+0x97>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd4f(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12cc <readLine+0x16c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12d2 <readLine+0x172>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x0,(%rdx)
mov    (%rsi),%rdi
mov    %rcx,0x8(%rsp)
mov    (%rcx),%edx
xor    %r12d,%r12d
xor    %esi,%esi
call   1030 <memset@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    %ebp,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%r13d
mov    (%r14),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
mov    $0xffffffff,%eax
test   %r13d,%r13d
jne    1210 <readLine+0xb0>
jmp    1276 <readLine+0x116>
nopl   0x0(%rax)
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    (%r14),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
add    $0x1,%r12d
test   %ebp,%ebp
js     1251 <readLine+0xf1>
mov    (%r14),%r13
mov    %r13,%rdi
lea    0xde3(%rip),%rsi
call   1080 <strstr@plt>
test   %rax,%rax
jne    1258 <readLine+0xf8>
mov    (%r15),%eax
lea    0x1(%rax),%ecx
mov    %ecx,(%r15)
mov    0x8(%rsp),%rcx
mov    (%rcx),%esi
cmp    %esi,%eax
jl     11e0 <readLine+0x80>
add    %esi,%esi
mov    0x8(%rsp),%rax
mov    %esi,(%rax)
mov    %r13,%rdi
call   1070 <realloc@plt>
mov    %rax,(%r14)
jmp    11e0 <readLine+0x80>
mov    $0xffffffff,%eax
jmp    1276 <readLine+0x116>
lea    0xda1(%rip),%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
mov    $0xfffffffe,%eax
cmove  %eax,%r12d
mov    %r12d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @readLine(i32 %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [32 x i8]
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 0, i32* %11
store i32 0, i32* %10
%13 = load i32*, i32** %8
store i32 0, i32* %13
%14 = load i8**, i8*** %7
%15 = load i8*, i8** %14
%16 = load i32*, i32** %9
%17 = load i32, i32* %16
%18 = call i32 @memset(i8* %15, i32 0, i32 %17)
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 32)
%21 = load i32, i32* %6
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 1)
store i32 %23, i32* %10
%24 = load i8**, i8*** %7
%25 = load i8*, i8** %24
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%27 = call i32 @strncat(i8* %25, i8* %26, i32 1)
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %4
store i32 -1, i32* %5
br label %77
31: ; preds = %4
br label %32
32: ; preds = %68, %31
%33 = load i8**, i8*** %7
%34 = load i8*, i8** %33
%35 = call i32* @strstr(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %69
37: ; preds = %32
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 1
store i32 %39, i32* %11
%40 = load i32*, i32** %8
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 1
store i32 %42, i32* %40
%43 = load i32*, i32** %9
%44 = load i32, i32* %43
%45 = icmp sgt i32 %42, %44
br i1 %45, label %46, label %57
46: ; preds = %37
%47 = load i32*, i32** %9
%48 = load i32, i32* %47
%49 = mul nsw i32 %48, 2
%50 = load i32*, i32** %9
store i32 %49, i32* %50
%51 = load i8**, i8*** %7
%52 = load i8*, i8** %51
%53 = load i32*, i32** %9
%54 = load i32, i32* %53
%55 = call i8* @realloc(i8* %52, i32 %54)
%56 = load i8**, i8*** %7
store i8* %55, i8** %56
br label %57
57: ; preds = %46, %37
%58 = load i32, i32* %6
%59 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 1)
store i32 %60, i32* %10
%61 = load i8**, i8*** %7
%62 = load i8*, i8** %61
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%64 = call i32 @strncat(i8* %62, i8* %63, i32 1)
%65 = load i32, i32* %10
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %57
store i32 -1, i32* %5
br label %77
68: ; preds = %57
br label %32
69: ; preds = %32
%70 = load i8**, i8*** %7
%71 = load i8*, i8** %70
%72 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %71)
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %69
store i32 -2, i32* %5
br label %77
75: ; preds = %69
%76 = load i32, i32* %11
store i32 %76, i32* %5
br label %77
77: ; preds = %75, %74, %67, %30
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @readLine(i32 %0, i8** nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
store i32 0, i32* %2
%7 = load i8*, i8** %1
%8 = load i32, i32* %3
%9 = tail call i32 @memset(i8* %7, i32 0, i32 %8)
%10 = call i32 @memset(i8* %6, i32 0, i32 32)
%11 = call i32 @read(i32 %0, i8* %6, i32 1)
%12 = load i8*, i8** %1
%13 = call i32 @strncat(i8* %12, i8* %6, i32 1)
%14 = icmp eq i32 %11, 0
br i1 %14, label %38, label %15
15: ; preds = %4, %29
%16 = phi i32 [ %21, %29 ], [ 0, %4 ]
%17 = load i8*, i8** %1
%18 = call i32* @strstr(i8* dereferenceable(1) %17, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %34
20: ; preds = %15
%21 = add nuw nsw i32 %16, 1
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
%24 = load i32, i32* %3
%25 = icmp slt i32 %22, %24
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = shl nsw i32 %24, 1
store i32 %27, i32* %3
%28 = call i8* @realloc(i8* %17, i32 %27)
store i8* %28, i8** %1
br label %29
29: ; preds = %26, %20
%30 = call i32 @read(i32 %0, i8* %6, i32 1)
%31 = load i8*, i8** %1
%32 = call i32 @strncat(i8* %31, i8* %6, i32 1)
%33 = icmp slt i32 %30, 0
br i1 %33, label %38, label %15
34: ; preds = %15
%35 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%36 = icmp eq i64 %35, 0
%37 = select i1 %36, i32 -2, i32 %16
br label %38
38: ; preds = %29, %34, %4
%39 = phi i32 [ -1, %4 ], [ %37, %34 ], [ -1, %29 ]
ret i32 %39
}