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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ ModelicaError(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ realloc(char *, size_t);
char *strchr(char *, char);
int readLine(char **buf, int *bufLen, FILE *fp) {
  char *offset;
  int oldBufLen;
  if (fgets(*buf, *bufLen, fp) == NULL) {
    return EOF;
  }
  do {
    char *p;
    char *tmp;
    if ((p = strchr(*buf, '\n')) != NULL) {
      *p = '\0';
      return 0;
    }
    oldBufLen = *bufLen;
    *bufLen *= 2;
    tmp = (char *)realloc(*buf, (size_t)*bufLen);
    if (NULL == tmp) {
      fclose(fp);
      free(*buf);
      ModelicaError("Memory allocation error\n");
      return 1;
    }
    *buf = tmp;
    offset = &((*buf)[oldBufLen - 1]);
  } while (fgets(offset, oldBufLen + 1, fp));
  return 0;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    11a5 <readLine+0x45>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <readLine+0x126>
jmp    11aa <readLine+0x4a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11dc <readLine+0x7c>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1286 <readLine+0x126>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmp    -0x40(%rbp),%rax
jne    1240 <readLine+0xe0>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
lea    0xdd1(%rip),%rdi
call   1060 <ModelicaError@plt>
movl   $0x1,-0x4(%rbp)
jmp    1286 <readLine+0x126>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
mov    -0x20(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    11aa <readLine+0x4a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    (%rdi),%rdi
mov    (%rsi),%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     11db <readLine+0x7b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%r13
mov    %r13,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
jne    11e6 <readLine+0x86>
movslq (%r12),%rbx
lea    (%rbx,%rbx,1),%rsi
mov    %esi,(%r12)
mov    %r13,%rdi
call   1080 <realloc@plt>
test   %rax,%rax
je     11ed <readLine+0x8d>
mov    %rax,(%r15)
lea    (%rbx,%rax,1),%rdi
add    $0xffffffffffffffff,%rdi
lea    0x1(%rbx),%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1190 <readLine+0x30>
jmp    11e9 <readLine+0x89>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
jmp    120e <readLine+0xae>
movb   $0x0,(%rax)
xor    %eax,%eax
jmp    120e <readLine+0xae>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
lea    0xdfc(%rip),%rdi
call   1060 <ModelicaError@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i32 0
@.str = [25 x i8] c"Memory allocation error\0A\00"
define i32 @readLine(i8** %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
%14 = load i32*, i32** %6
%15 = load i32, i32* %14
%16 = load i32*, i32** %7
%17 = call i32* @fgets(i8* %13, i32 %15, i32* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i32, i32* @EOF
store i32 %20, i32* %4
br label %68
21: ; preds = %3
br label %22
22: ; preds = %60, %21
%23 = load i8**, i8*** %5
%24 = load i8*, i8** %23
%25 = call i8* @strchr(i8* %24, i8 signext 10)
store i8* %25, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i8*, i8** %10
store i8 0, i8* %28
store i32 0, i32* %4
br label %68
29: ; preds = %22
%30 = load i32*, i32** %6
%31 = load i32, i32* %30
store i32 %31, i32* %9
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = mul nsw i32 %33, 2
store i32 %34, i32* %32
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i32*, i32** %6
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = call i64 @realloc(i8* %36, i64 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = icmp eq i8* null, %42
br i1 %43, label %44, label %51
44: ; preds = %29
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
%47 = load i8**, i8*** %5
%48 = load i8*, i8** %47
%49 = call i32 @free(i8* %48)
%50 = call i32 @ModelicaError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %68
51: ; preds = %29
%52 = load i8*, i8** %11
%53 = load i8**, i8*** %5
store i8* %52, i8** %53
%54 = load i8**, i8*** %5
%55 = load i8*, i8** %54
%56 = load i32, i32* %9
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8* %59, i8** %8
br label %60
60: ; preds = %51
%61 = load i8*, i8** %8
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
%64 = load i32*, i32** %7
%65 = call i32* @fgets(i8* %61, i32 %63, i32* %64)
%66 = icmp ne i32* %65, null
br i1 %66, label %22, label %67
67: ; preds = %60
store i32 0, i32* %4
br label %68
68: ; preds = %67, %44, %27, %19
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [25 x i8] c"Memory allocation error\0A\00"
define i32 @readLine(i8** nocapture %0, i32* nocapture %1, i32* nocapture %2) {
%4 = load i8*, i8** %0
%5 = load i32, i32* %1
%6 = tail call i32* @fgets(i8* %4, i32 %5, i32* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = load i32, i32* @EOF
br label %34
10: ; preds = %3, %27
%11 = load i8*, i8** %0
%12 = tail call i8* @strchr(i8* dereferenceable(1) %11, i8 signext 10)
%13 = icmp eq i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %10
store i8 0, i8* %12
br label %34
15: ; preds = %10
%16 = load i32, i32* %1
%17 = shl nsw i32 %16, 1
store i32 %17, i32* %1
%18 = sext i32 %17 to i64
%19 = tail call i64 @realloc(i8* %11, i64 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = tail call i32 @fclose(i32* %2)
%24 = load i8*, i8** %0
%25 = tail call i32 @free(i8* %24)
%26 = tail call i32 @ModelicaError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %34
27: ; preds = %15
store i8* %20, i8** %0
%28 = add nsw i32 %16, -1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %20, i64 %29
%31 = add nsw i32 %16, 1
%32 = tail call i32* @fgets(i8* %30, i32 %31, i32* %2)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %10
34: ; preds = %27, %22, %14, %8
%35 = phi i32 [ %9, %8 ], [ 1, %22 ], [ 0, %14 ], [ 0, %27 ]
ret i32 %35
}