#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 *fgets_until(char *line, int n, FILE *f, int stop) {
  int i = 0;
  while (1) {
    if (i >= n - 1)
      break;
    int c = fgetc(f);
    if (c == EOF)
      return NULL;
    line[i] = c;
    if (c == stop)
      break;
    i += 1;
  }
  line[i + 1] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<fgets_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1143 <fgets_until+0x33>
jmp    119d <fgets_until+0x8d>
mov    -0x20(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e8f(%rip),%rcx
cmp    (%rcx),%eax
jne    116e <fgets_until+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    11b6 <fgets_until+0xa6>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    118f <fgets_until+0x7f>
jmp    119d <fgets_until+0x8d>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    112d <fgets_until+0x1d>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_until>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%r14
add    $0xffffffff,%ebp
xor    %ebx,%ebx
test   %ebp,%ebp
cmovle %ebx,%ebp
mov    0x2eb1(%rip),%r13
nopw   0x0(%rax,%rax,1)
cmp    %rbx,%rbp
je     1165 <fgets_until+0x55>
mov    %r12,%rdi
call   1030 <fgetc@plt>
cmp    0x0(%r13),%eax
je     116f <fgets_until+0x5f>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %r15d,%eax
jne    1140 <fgets_until+0x30>
add    $0xffffffff,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
movb   $0x0,0x1(%rax,%r14,1)
jmp    1172 <fgets_until+0x62>
xor    %r14d,%r14d
mov    %r14,%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 i8* @fgets_until(i8* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %4, %36
%13 = load i32, i32* %10
%14 = load i32, i32* %7
%15 = sub nsw i32 %14, 1
%16 = icmp sge i32 %13, %15
br i1 %16, label %17, label %18
17: ; preds = %12
br label %39
18: ; preds = %12
%19 = load i32*, i32** %8
%20 = call i32 @fgetc(i32* %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %18
store i8* null, i8** %5
br label %46
25: ; preds = %18
%26 = load i32, i32* %11
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %6
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %27, i8* %31
%32 = load i32, i32* %11
%33 = load i32, i32* %9
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %25
br label %39
36: ; preds = %25
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
br label %12
39: ; preds = %35, %17
%40 = load i8*, i8** %6
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %6
store i8* %45, i8** %5
br label %46
46: ; preds = %39, %24
%47 = load i8*, i8** %5
ret i8* %47
}

LLVM IR (-O2):
@EOF = global i32 0
define i8* @fgets_until(i8* writeonly %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = add i32 %1, -1
%6 = call i32 @llvm.smax.i32(i32 %5, i32 0)
%7 = zext i32 %6 to i64
br label %8
8: ; preds = %15, %4
%9 = phi i64 [ %19, %15 ], [ 0, %4 ]
%10 = icmp eq i64 %9, %7
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = tail call i32 @fgetc(i32* %2)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %27, label %15
15: ; preds = %11
%16 = trunc i32 %12 to i8
%17 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 %16, i8* %17
%18 = icmp eq i32 %12, %3
%19 = add nuw nsw i64 %9, 1
br i1 %18, label %20, label %8
20: ; preds = %15
%21 = trunc i64 %9 to i32
br label %22
22: ; preds = %8, %20
%23 = phi i32 [ %21, %20 ], [ %6, %8 ]
%24 = add nuw nsw i32 %23, 1
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %11, %22
%28 = phi i8* [ %0, %22 ], [ null, %11 ]
ret i8* %28
}