#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 */
int /*<<< orphan*/ err(int, char *, char const *);
int /*<<< orphan*/ errx(int, char *, char const *);
int read(int, char *, int);
int read_until_terminator(int fd, const char *name, char *buf,
                          char terminator) {
  int len = 0;
  while (1) {
    int res = read(fd, buf + len, 1);
    if (res != 1) {
      if (res == 0)
        errx(1, "read %s: EOF", name);
      else
        err(1, "read %s", name);
    }
    if (buf[len] == '\n') {
      buf[len] = '\0';
      return len;
    }
    len++;
  }
}


x86 assembly (clang, -O0):
<read_until_terminator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     11b2 <read_until_terminator+0x82>
cmpl   $0x0,-0x24(%rbp)
jne    1198 <read_until_terminator+0x68>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe72(%rip),%rsi
call   1030 <errx@plt>
jmp    11ad <read_until_terminator+0x7d>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe65(%rip),%rsi
call   1050 <err@plt>
jmp    11b2 <read_until_terminator+0x82>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11dc <read_until_terminator+0xac>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    114f <read_until_terminator+0x1f>

x86 assembly (clang, -O2):
<read_until_terminator>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r13d
mov    $0xffffffff,%ebx
lea    0xeb3(%rip),%r15
lea    0xeb9(%rip),%r12
jmp    1178 <read_until_terminator+0x48>
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rsi
mov    %r14,%rdx
call   1050 <err@plt>
add    $0x1,%ebx
lea    0x1(%rbp),%rdx
cmpb   $0xa,0x0(%rbp)
je     11a6 <read_until_terminator+0x76>
mov    %rdx,%rbp
mov    %r13d,%edi
mov    %rdx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     116b <read_until_terminator+0x3b>
mov    $0x1,%edi
test   %eax,%eax
jne    1160 <read_until_terminator+0x30>
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <errx@plt>
jmp    116b <read_until_terminator+0x3b>
movb   $0x0,-0x1(%rdx)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"read %s: EOF\00"
@.str.1 = [8 x i8] c"read %s\00"
define i32 @read_until_terminator(i32 %0, i8* %1, i8* %2, i8 signext %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8 %3, i8* %8
store i32 0, i32* %9
br label %11
11: ; preds = %4, %44
%12 = load i32, i32* %5
%13 = load i8*, i8** %7
%14 = load i32, i32* %9
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = call i32 @read(i32 %12, i8* %16, i32 1)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %30
20: ; preds = %11
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %6
%25 = call i32 @errx(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %29
26: ; preds = %20
%27 = load i8*, i8** %6
%28 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %23
br label %30
30: ; preds = %29, %11
%31 = load i8*, i8** %7
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %44
38: ; preds = %30
%39 = load i8*, i8** %7
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32, i32* %9
ret i32 %43
44: ; preds = %30
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %11
}

LLVM IR (-O2):
@.str = [13 x i8] c"read %s: EOF\00"
@.str.1 = [8 x i8] c"read %s\00"
define i32 @read_until_terminator(i32 %0, i8* %1, i8* nocapture %2, i8 signext %3) {
br label %5
5: ; preds = %13, %4
%6 = phi i64 [ %16, %13 ], [ 0, %4 ]
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = tail call i32 @read(i32 %0, i8* %7, i32 1)
switch i32 %8, label %11 [
i32 1, label %13
i32 0, label %9
]
9: ; preds = %5
%10 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %13
11: ; preds = %5
%12 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %13
13: ; preds = %5, %9, %11
%14 = load i8, i8* %7
%15 = icmp eq i8 %14, 10
%16 = add nuw i64 %6, 1
br i1 %15, label %17, label %5
17: ; preds = %13
%18 = trunc i64 %6 to i32
store i8 0, i8* %7
ret i32 %18
}