#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*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *, int);
int strlen(char *);
int checked_fgets(char *buf, int maxlen, FILE *f, FILE *errlog,
                  char const *path, int *p_lineno) {
  int len;
  if (!fgets(buf, maxlen, f))
    return 0;
  (*p_lineno)++;
  len = strlen(buf);
  if (len == maxlen - 1 && buf[len - 1] != '\n') {
    fprintf(errlog, "%s: %d: input line is too long\n", path, *p_lineno);
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<checked_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    1174 <checked_fgets+0x44>
movl   $0x0,-0x4(%rbp)
jmp    11e2 <checked_fgets+0xb2>
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11dc <checked_fgets+0xac>
mov    -0x10(%rbp),%rax
mov    -0x3c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11dc <checked_fgets+0xac>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <checked_fgets+0xb2>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_fgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9,%r12
mov    %r8,%r14
mov    %rcx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <fgets@plt>
test   %eax,%eax
je     118a <checked_fgets+0x5a>
addl   $0x1,(%r12)
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%ebp
cmp    %ebp,%eax
jne    118c <checked_fgets+0x5c>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rcx,%rbx,1)
je     118c <checked_fgets+0x5c>
mov    (%r12),%ecx
lea    0xe88(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    118c <checked_fgets+0x5c>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [32 x i8] c"%s: %d: input line is too long\0A\00"
define i32 @checked_fgets(i8* %0, i32 %1, i32* nocapture %2, i32* nocapture %3, i8* %4, i32* nocapture %5) {
%7 = tail call i32 @fgets(i8* %0, i32 %1, i32* %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %24, label %9
9: ; preds = %6
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
store i32 %11, i32* %5
%12 = tail call i32 @strlen(i8* %0)
%13 = add nsw i32 %1, -1
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %24
15: ; preds = %9
%16 = add nsw i32 %12, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = load i32, i32* %5
%23 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %22)
br label %24
24: ; preds = %9, %15, %6, %21
%25 = phi i32 [ -1, %21 ], [ 0, %6 ], [ %12, %15 ], [ %12, %9 ]
ret i32 %25
}