#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 /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int readline(char *buffer, FILE *fd, int len, char delim) {
  int i;
  for (i = 0; i < len - 1; i++) {
    fscanf(fd, "%c", &buffer[i]);
    if (buffer[i] == delim)
      break;
  }
  buffer[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %al,-0x15(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    118b <readline+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
movslq -0x1c(%rbp),%rax
add    %rax,%rdx
lea    0xeaa(%rip),%rsi
call   1030 <fscanf@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
jne    1178 <readline+0x68>
jmp    118b <readline+0x7b>
jmp    117d <readline+0x6d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112f <readline+0x1f>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
cmp    $0x2,%edx
jl     1164 <readline+0x54>
mov    %ecx,%r15d
mov    %rsi,%r12
add    $0xffffffff,%edx
mov    %edx,0x4(%rsp)
mov    %edx,%r14d
xor    %ebp,%ebp
lea    0xec2(%rip),%r13
xchg   %ax,%ax
lea    (%rbx,%rbp,1),%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <fscanf@plt>
cmp    %r15b,(%rbx,%rbp,1)
je     1164 <readline+0x54>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1140 <readline+0x30>
mov    0x4(%rsp),%eax
mov    %eax,%ebp
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
define i32 @readline(i8* %0, i32* %1, i32 %2, i8 signext %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i8 %3, i8* %8
store i32 0, i32* %9
br label %10
10: ; preds = %33, %4
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %36
15: ; preds = %10
%16 = load i32*, i32** %6
%17 = load i8*, i8** %5
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %5
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = load i8, i8* %8
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %27, %29
br i1 %30, label %31, label %32
31: ; preds = %15
br label %36
32: ; preds = %15
br label %33
33: ; preds = %32
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %10
36: ; preds = %31, %10
%37 = load i8*, i8** %5
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %9
ret i32 %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
define i32 @readline(i8* %0, i32* nocapture %1, i32 %2, i8 signext %3) {
%5 = add i32 %2, -1
%6 = icmp sgt i32 %2, 1
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = zext i32 %5 to i64
br label %9
9: ; preds = %7, %15
%10 = phi i64 [ 0, %7 ], [ %16, %15 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i32 @fscanf(i32* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i8, i8* %11
%14 = icmp eq i8 %13, %3
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = add nuw nsw i64 %10, 1
%17 = icmp eq i64 %16, %8
br i1 %17, label %20, label %9
18: ; preds = %9
%19 = trunc i64 %10 to i32
br label %20
20: ; preds = %15, %18, %4
%21 = phi i32 [ 0, %4 ], [ %19, %18 ], [ %5, %15 ]
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 0, i8* %23
ret i32 %21
}