#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 fgetc(int /*<<< orphan*/ *);
char *str_append(char *, char *);
int /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
size_t str_getline(char **str, FILE *fin, char *delimiters) {
  int ch = 0;
  char s[2] = {'\0', '\0'};
  size_t num = 0;
  while ((ch = fgetc(fin)) != EOF) {
    s[0] = (char)ch;
    if (strchr(delimiters, (char)ch) != NULL) {
      ungetc(ch, fin);
      return (num);
    }
    *str = str_append(*str, s);
    num++;
  }
  return (num);
}


x86 assembly (clang, -O0):
<str_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x26(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <memset@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%eax
je     1201 <str_getline+0xb1>
mov    -0x24(%rbp),%eax
mov    %al,-0x26(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
movsbl %al,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11d6 <str_getline+0x86>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <ungetc@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <str_getline+0xb9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x26(%rbp),%rsi
call   1070 <str_append@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1183 <str_getline+0x33>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_getline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
movw   $0x0,0x6(%rsp)
mov    %rsi,%rdi
call   1040 <fgetc@plt>
mov    0x2e7e(%rip),%r13
cmp    0x0(%r13),%eax
jne    1174 <str_getline+0x34>
xor    %ebx,%ebx
jmp    11c8 <str_getline+0x88>
mov    %eax,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %bpl,0x6(%rsp)
movsbl %bpl,%esi
mov    %r14,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
jne    11be <str_getline+0x7e>
mov    (%r12),%rdi
lea    0x6(%rsp),%rsi
call   1060 <str_append@plt>
mov    %rax,(%r12)
add    $0x1,%rbx
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
jne    1180 <str_getline+0x40>
jmp    11c8 <str_getline+0x88>
mov    %ebp,%edi
mov    %r15,%rsi
call   1050 <ungetc@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i64 @str_getline(i8** %0, i32* %1, i8* %2) {
%4 = alloca i64
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [2 x i8]
%10 = alloca i64
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i32 0, i32* %8
%11 = bitcast [2 x i8]* %9 to i8*
store i64 0, i64* %10
br label %12
12: ; preds = %31, %3
%13 = load i32*, i32** %6
%14 = call i32 @fgetc(i32* %13)
store i32 %14, i32* %8
%15 = load i32, i32* @EOF
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %39
17: ; preds = %12
%18 = load i32, i32* %8
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %19, i8* %20
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = trunc i32 %22 to i8
%24 = call i32* @strchr(i8* %21, i8 signext %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %31
26: ; preds = %17
%27 = load i32, i32* %8
%28 = load i32*, i32** %6
%29 = call i32 @ungetc(i32 %27, i32* %28)
%30 = load i64, i64* %10
store i64 %30, i64* %4
br label %41
31: ; preds = %17
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%35 = call i8* @str_append(i8* %33, i8* %34)
%36 = load i8**, i8*** %5
store i8* %35, i8** %36
%37 = load i64, i64* %10
%38 = add i64 %37, 1
store i64 %38, i64* %10
br label %12
39: ; preds = %12
%40 = load i64, i64* %10
store i64 %40, i64* %4
br label %41
41: ; preds = %39, %26
%42 = load i64, i64* %4
ret i64 %42
}

LLVM IR (-O2):
@EOF = global i32 0
define i64 @str_getline(i8** nocapture %0, i32* nocapture %1, i8* %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
store i16 0, i16* %4
%6 = call i32 @fgetc(i32* %1)
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %24, label %9
9: ; preds = %3, %17
%10 = phi i32 [ %21, %17 ], [ %6, %3 ]
%11 = phi i64 [ %20, %17 ], [ 0, %3 ]
%12 = trunc i32 %10 to i8
store i8 %12, i8* %5
%13 = call i32* @strchr(i8* %2, i8 signext %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = call i32 @ungetc(i32 %10, i32* %1)
br label %24
17: ; preds = %9
%18 = load i8*, i8** %0
%19 = call i8* @str_append(i8* %18, i8* %5)
store i8* %19, i8** %0
%20 = add i64 %11, 1
%21 = call i32 @fgetc(i32* %1)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %9
24: ; preds = %17, %3, %15
%25 = phi i64 [ %11, %15 ], [ 0, %3 ], [ %20, %17 ]
ret i64 %25
}