#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 */

/* Variables and functions */
scalar_t__ read(int, char *, int);
int read_until_delim(int fd, char *buf, unsigned int size, char endchar) {
  char ch;
  unsigned int total = 0;
  while (1) {
    if (read(fd, &ch, 1) <= 0) {
      return -1;
    }
    if (ch == endchar)
      break;
    if (total >= size)
      return -1;
    buf[total++] = ch;
  }
  return (int)total;
}


x86 assembly (clang, -O0):
<read_until_delim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %al,-0x15(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jg     1155 <read_until_delim+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <read_until_delim+0x94>
movsbl -0x16(%rbp),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
jne    116a <read_until_delim+0x5a>
jmp    119e <read_until_delim+0x8e>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1182 <read_until_delim+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <read_until_delim+0x94>
mov    -0x16(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
jmp    112e <read_until_delim+0x1e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_until_delim>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
jle    1184 <read_until_delim+0x74>
mov    %r12d,%r14d
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
nopl   0x0(%rax)
movzbl 0x7(%rsp),%eax
cmp    %r13b,%al
je     1182 <read_until_delim+0x72>
cmp    %rbx,%r14
je     117b <read_until_delim+0x6b>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jg     1150 <read_until_delim+0x40>
mov    $0xffffffff,%ecx
jmp    1184 <read_until_delim+0x74>
mov    %ebx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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