#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 getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ luaL_addchar(int);
int /*<<< orphan*/ luaL_buffer();
int /*<<< orphan*/ lua_pushlstring(int /*<<< orphan*/, int);
void read_until(FILE *f, int lim) {
  int l = 0;
  int c;
  for (c = getc(f); c != EOF && c != lim; c = getc(f)) {
    luaL_addchar(c);
    l++;
  }
  if (l > 0 || c == lim)
    lua_pushlstring(luaL_buffer(), l);
}


x86 assembly (clang, -O0):
<read_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
mov    0x2e7a(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x15(%rbp)
je     1185 <read_until+0x45>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1195 <read_until+0x55>
jmp    11b7 <read_until+0x77>
mov    -0x14(%rbp),%edi
call   1040 <luaL_addchar@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x14(%rbp)
jmp    1162 <read_until+0x22>
cmpl   $0x0,-0x10(%rbp)
jg     11cd <read_until+0x8d>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11de <read_until+0x9e>
mov    $0x0,%al
call   1030 <luaL_buffer@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%esi
call   1050 <lua_pushlstring@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_until>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1060 <getc@plt>
cmp    %r14d,%eax
je     118e <read_until+0x4e>
mov    0x2e8a(%rip),%r15
cmp    (%r15),%eax
je     118e <read_until+0x4e>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%edi
call   1040 <luaL_addchar@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <getc@plt>
cmp    %r14d,%eax
je     1195 <read_until+0x55>
cmp    (%r15),%eax
jne    1170 <read_until+0x30>
jmp    1195 <read_until+0x55>
xor    %ebp,%ebp
cmp    %r14d,%eax
jne    11af <read_until+0x6f>
xor    %eax,%eax
call   1030 <luaL_buffer@plt>
mov    %eax,%edi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <lua_pushlstring@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define void @read_until(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = load i32*, i32** %3
%8 = call i32 @getc(i32* %7)
store i32 %8, i32* %6
br label %9
9: ; preds = %24, %2
%10 = load i32, i32* %6
%11 = load i32, i32* @EOF
%12 = icmp ne i32 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %6
%15 = load i32, i32* %4
%16 = icmp ne i32 %14, %15
br label %17
17: ; preds = %13, %9
%18 = phi i1 [ false, %9 ], [ %16, %13 ]
br i1 %18, label %19, label %27
19: ; preds = %17
%20 = load i32, i32* %6
%21 = call i32 @luaL_addchar(i32 %20)
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
br label %24
24: ; preds = %19
%25 = load i32*, i32** %3
%26 = call i32 @getc(i32* %25)
store i32 %26, i32* %6
br label %9
27: ; preds = %17
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = load i32, i32* %6
%32 = load i32, i32* %4
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30, %27
%35 = call i32 (...) @luaL_buffer()
%36 = load i32, i32* %5
%37 = call i32 @lua_pushlstring(i32 %35, i32 %36)
br label %38
38: ; preds = %34, %30
ret void
}

LLVM IR (-O2):
@EOF = global i32 0
define void @read_until(i32* nocapture %0, i32 %1) {
%3 = tail call i32 @getc(i32* %0)
%4 = load i32, i32* @EOF
%5 = icmp ne i32 %3, %4
%6 = icmp ne i32 %3, %1
%7 = and i1 %6, %5
br i1 %7, label %8, label %18
8: ; preds = %2, %8
%9 = phi i32 [ %13, %8 ], [ %3, %2 ]
%10 = phi i32 [ %12, %8 ], [ 0, %2 ]
%11 = tail call i32 @luaL_addchar(i32 %9)
%12 = add nuw nsw i32 %10, 1
%13 = tail call i32 @getc(i32* %0)
%14 = load i32, i32* @EOF
%15 = icmp ne i32 %13, %14
%16 = icmp ne i32 %13, %1
%17 = and i1 %16, %15
br i1 %17, label %8, label %20
18: ; preds = %2
%19 = icmp eq i32 %3, %1
br i1 %19, label %20, label %24
20: ; preds = %8, %18
%21 = phi i32 [ 0, %18 ], [ %12, %8 ]
%22 = tail call i32 (...) @luaL_buffer()
%23 = tail call i32 @lua_pushlstring(i32 %22, i32 %21)
br label %24
24: ; preds = %18, %20
ret void
}