#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 size_t uint32_t;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
int read(int /*<<< orphan*/, char *, int);
uint32_t read_until(int32_t fd, char term, char *out, uint32_t max) {
  uint32_t count = 0;
  char input;
  while (read(fd, &input, 1) == 1 && input != term && count < max) {
    out[count] = input;
    count++;
  }
  out[count] = '\0';
  return count;
}


x86 assembly (clang, -O0):
<read_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x22(%rbp)
jne    1175 <read_until+0x65>
movsbl -0x21(%rbp),%ecx
movsbl -0x5(%rbp),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x22(%rbp)
je     1175 <read_until+0x65>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    1185 <read_until+0x75>
jmp    11a4 <read_until+0x94>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1131 <read_until+0x21>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_until>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%r12d
mov    %edi,%ebx
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
jne    117b <read_until+0x6b>
lea    0x7(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x7(%rsp),%eax
cmp    %r12b,%al
je     117b <read_until+0x6b>
cmp    %r15,%rbp
jae    117b <read_until+0x6b>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1150 <read_until+0x40>
movb   $0x0,(%r14,%rbp,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @read_until(i32 %0, i8 signext %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i64 0, i64* %9
br label %11
11: ; preds = %27, %4
%12 = load i32, i32* %5
%13 = call i32 @read(i32 %12, i8* %10, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i8, i8* %10
%17 = sext i8 %16 to i32
%18 = load i8, i8* %6
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %17, %19
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i64, i64* %9
%23 = load i64, i64* %8
%24 = icmp ult i64 %22, %23
br label %25
25: ; preds = %21, %15, %11
%26 = phi i1 [ false, %15 ], [ false, %11 ], [ %24, %21 ]
br i1 %26, label %27, label %34
27: ; preds = %25
%28 = load i8, i8* %10
%29 = load i8*, i8** %7
%30 = load i64, i64* %9
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 %28, i8* %31
%32 = load i64, i64* %9
%33 = add i64 %32, 1
store i64 %33, i64* %9
br label %11
34: ; preds = %25
%35 = load i8*, i8** %7
%36 = load i64, i64* %9
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i64, i64* %9
ret i64 %38
}

LLVM IR (-O2):
define i64 @read_until(i32 %0, i8 signext %1, i8* nocapture writeonly %2, i64 %3) {
%5 = alloca i8
%6 = call i32 @read(i32 %0, i8* %5, i32 1)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %19
8: ; preds = %4, %14
%9 = phi i64 [ %16, %14 ], [ 0, %4 ]
%10 = load i8, i8* %5
%11 = icmp ne i8 %10, %1
%12 = icmp ult i64 %9, %3
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = getelementptr inbounds i8, i8* %2, i64 %9
store i8 %10, i8* %15
%16 = add nuw i64 %9, 1
%17 = call i32 @read(i32 %0, i8* %5, i32 1)
%18 = icmp eq i32 %17, 1
br i1 %18, label %8, label %19
19: ; preds = %8, %14, %4
%20 = phi i64 [ 0, %4 ], [ %16, %14 ], [ %9, %8 ]
%21 = getelementptr inbounds i8, i8* %2, i64 %20
store i8 0, i8* %21
ret i64 %20
}