#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*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int read_line(FILE *stream, char *buffer, const int buffer_len) {
  int c = 0;
  int pos = 0;
  while ((c = getc(stream)) != EOF) {
    if (c == '\n') {
      break;
    }
    if (pos >= (buffer_len - 1)) {
      fprintf(stderr, "%s: line too long (%d characters)\n", "advent7_abba",
              pos);
      return -1;
    }
    buffer[pos] = (char)c;
    pos++;
  }
  if (pos < buffer_len) {
    buffer[pos] = '\0';
  }
  if ((pos == 0) && (c != EOF)) {
    fprintf(stderr, "%s: warning: read zero characters from stream\n",
            "advent7_abba");
  }
  return pos;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getc@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%eax
je     11c7 <read_line+0xa7>
cmpl   $0xa,-0x20(%rbp)
jne    116b <read_line+0x4b>
jmp    11c7 <read_line+0xa7>
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     11a9 <read_line+0x89>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%ecx
lea    0xe71(%rip),%rsi
lea    0xe8d(%rip),%rdx
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <read_line+0xff>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1141 <read_line+0x21>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11df <read_line+0xbf>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x24(%rbp)
jne    1219 <read_line+0xf9>
mov    -0x20(%rbp),%eax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%eax
je     1219 <read_line+0xf9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
lea    0xe11(%rip),%rdx
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <getc@plt>
mov    0x2ea8(%rip),%rbp
mov    0x0(%rbp),%ecx
xor    %ebx,%ebx
cmp    $0xa,%eax
je     1181 <read_line+0x61>
cmp    %ecx,%eax
je     1181 <read_line+0x61>
lea    -0x1(%r15),%r13d
xor    %ebx,%ebx
test   %r13d,%r13d
cmovle %ebx,%r13d
nopl   0x0(%rax,%rax,1)
cmp    %rbx,%r13
je     11ba <read_line+0x9a>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
call   1040 <getc@plt>
mov    0x0(%rbp),%ecx
cmp    $0xa,%eax
je     1181 <read_line+0x61>
cmp    %ecx,%eax
jne    1160 <read_line+0x40>
cmp    %r15d,%ebx
jge    1190 <read_line+0x70>
mov    %ebx,%ecx
movb   $0x0,(%r14,%rcx,1)
mov    0x0(%rbp),%ecx
test   %ebx,%ebx
jne    11e0 <read_line+0xc0>
cmp    %ecx,%eax
je     11e0 <read_line+0xc0>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
lea    0xe74(%rip),%rdx
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11e0 <read_line+0xc0>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
lea    0xe52(%rip),%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"%s: line too long (%d characters)\0A\00"
@.str.1 = [13 x i8] c"advent7_abba\00"
@.str.2 = [47 x i8] c"%s: warning: read zero characters from stream\0A\00"
define i32 @read_line(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %28, %3
%11 = load i32*, i32** %5
%12 = call i32 @getc(i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %37
15: ; preds = %10
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, 10
br i1 %17, label %18, label %19
18: ; preds = %15
br label %37
19: ; preds = %15
%20 = load i32, i32* %9
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 1
%23 = icmp sge i32 %20, %22
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i32, i32* %9
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 -1, i32* %4
br label %58
28: ; preds = %19
%29 = load i32, i32* %8
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %6
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 %30, i8* %34
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
br label %10
37: ; preds = %18, %10
%38 = load i32, i32* %9
%39 = load i32, i32* %7
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i8*, i8** %6
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %41, %37
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %46
%50 = load i32, i32* %8
%51 = load i32, i32* @EOF
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %53, %49, %46
%57 = load i32, i32* %9
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %24
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@EOF = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"%s: line too long (%d characters)\0A\00"
@.str.1 = [13 x i8] c"advent7_abba\00"
@.str.2 = [47 x i8] c"%s: warning: read zero characters from stream\0A\00"
define i32 @read_line(i32* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = tail call i32 @getc(i32* %0)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
%7 = icmp eq i32 %4, 10
%8 = or i1 %7, %6
br i1 %8, label %31, label %9
9: ; preds = %3
%10 = add i32 %2, -1
%11 = call i32 @llvm.smax.i32(i32 %10, i32 0)
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %9, %20
%14 = phi i64 [ 0, %9 ], [ %23, %20 ]
%15 = phi i32 [ %4, %9 ], [ %24, %20 ]
%16 = icmp eq i64 %14, %12
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %48
20: ; preds = %13
%21 = trunc i32 %15 to i8
%22 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %21, i8* %22
%23 = add nuw nsw i64 %14, 1
%24 = tail call i32 @getc(i32* %0)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
%27 = icmp eq i32 %24, 10
%28 = or i1 %27, %26
br i1 %28, label %29, label %13
29: ; preds = %20
%30 = trunc i64 %23 to i32
br label %31
31: ; preds = %29, %3
%32 = phi i32 [ %5, %3 ], [ %25, %29 ]
%33 = phi i32 [ 0, %3 ], [ %30, %29 ]
%34 = phi i32 [ %4, %3 ], [ %24, %29 ]
%35 = icmp slt i32 %33, %2
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = zext i32 %33 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* @EOF
br label %40
40: ; preds = %36, %31
%41 = phi i32 [ %39, %36 ], [ %32, %31 ]
%42 = icmp ne i32 %33, 0
%43 = icmp eq i32 %34, %41
%44 = select i1 %42, i1 true, i1 %43
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %40, %45, %17
%49 = phi i32 [ -1, %17 ], [ 0, %45 ], [ %33, %40 ]
ret i32 %49
}