#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 /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
char *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *);
int strlen(char *);
char *read_line(char *buf, size_t length, FILE *f) {
  char *p = fgets(buf, length, f);
  if (p) {
    size_t last = strlen(buf) - 1;
    if (buf[last] == '\n') {
      buf[last] = '\0';
    } else {
      fscanf(f, "%*[^\n]");
      (void)fgetc(f);
    }
  }
  return p;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ca <read_line+0x8a>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11ac <read_line+0x6c>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11c5 <read_line+0x85>
mov    -0x18(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1040 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
jmp    11ca <read_line+0x8a>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rdi,%rbx
call   1060 <fgets@plt>
mov    %rax,%r15
test   %rax,%rax
je     1187 <read_line+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1170 <read_line+0x30>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    1187 <read_line+0x47>
lea    0xe89(%rip),%rsi
mov    %r14,%rdi
call   1040 <fscanf@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%*[^\0A]\00"
define i8* @read_line(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
%10 = load i64, i64* %5
%11 = load i32*, i32** %6
%12 = call i8* @fgets(i8* %9, i64 %10, i32* %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %36
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
store i64 %19, i64* %8
%20 = load i8*, i8** %4
%21 = load i64, i64* %8
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = load i8*, i8** %4
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
br label %35
30: ; preds = %15
%31 = load i32*, i32** %6
%32 = call i32 @fscanf(i32* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%33 = load i32*, i32** %6
%34 = call i32 @fgetc(i32* %33)
br label %35
35: ; preds = %30, %26
br label %36
36: ; preds = %35, %3
%37 = load i8*, i8** %7
ret i8* %37
}

LLVM IR (-O2):
@.str = [7 x i8] c"%*[^\0A]\00"
define i8* @read_line(i8* %0, i64 %1, i32* nocapture %2) {
%4 = tail call i8* @fgets(i8* %0, i64 %1, i32* %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %6
store i8 0, i8* %10
br label %17
14: ; preds = %6
%15 = tail call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @fgetc(i32* %2)
br label %17
17: ; preds = %13, %14, %3
ret i8* %4
}