#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 *);
int /*<<< orphan*/ errx(int, char *);
int read(int /*<<< orphan*/, char *, int);
void read_line(char *buf, size_t buf_sz) {
  while (1) {
    if (!buf_sz) {
      errx(1, "newline expected");
    }
    if (read(0, buf, 1) != 1) {
      err(1, "read");
    }
    if (*buf == '\n') {
      *buf = 0;
      return;
    }
    buf++;
    buf_sz--;
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115c <read_line+0x2c>
mov    $0x1,%edi
lea    0xea9(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1186 <read_line+0x56>
mov    $0x1,%edi
lea    0xe90(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11a3 <read_line+0x73>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x10,%rsp
pop    %rbp
ret
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1140 <read_line+0x10>

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xebb(%rip),%r15
lea    0xec5(%rip),%r12
sub    $0x1,%r14
jae    116d <read_line+0x3d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %r15,%rsi
call   1030 <errx@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     118e <read_line+0x5e>
mov    $0x1,%edi
mov    %r12,%rsi
call   1050 <err@plt>
cmpb   $0xa,(%rbx)
je     119f <read_line+0x6f>
add    $0x1,%rbx
sub    $0x1,%r14
jb     1160 <read_line+0x30>
jmp    116d <read_line+0x3d>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"newline expected\00"
@.str.1 = [5 x i8] c"read\00"
define void @read_line(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
br label %5
5: ; preds = %2, %23
%6 = load i64, i64* %4
%7 = icmp ne i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = call i32 @errx(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = load i8*, i8** %3
%12 = call i32 @read(i32 0, i8* %11, i32 1)
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 10
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i8*, i8** %3
store i8 0, i8* %22
ret void
23: ; preds = %16
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %3
%26 = load i64, i64* %4
%27 = add i64 %26, -1
store i64 %27, i64* %4
br label %5
}

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