#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
char *strpbrk(char *, char *);
char *fgetline(char *s, int num, FILE *fp) {
  char *end;
  s = fgets(s, num, fp);
  if (NULL == s)
    return NULL;
  if (feof(fp))
    return s;
  end = strpbrk(s, "\n\r");
  if (NULL == end) {
    while ('\n' != fgetc(fp))
      ;
  } else {
    *end = '\0';
  }
  return s;
}


x86 assembly (clang, -O0):
<fgetline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1180 <fgetline+0x40>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <fgetline+0xb7>
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11a0 <fgetline+0x60>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <fgetline+0xb7>
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1060 <strpbrk@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11e8 <fgetline+0xa8>
jmp    11c5 <fgetline+0x85>
mov    -0x20(%rbp),%rdi
call   1030 <fgetc@plt>
movsbl %al,%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
je     11e3 <fgetline+0xa3>
jmp    11c5 <fgetline+0x85>
jmp    11ef <fgetline+0xaf>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetline>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
call   1040 <fgets@plt>
mov    %rax,%r14
test   %rax,%rax
je     118c <fgetline+0x4c>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    118c <fgetline+0x4c>
lea    0xe98(%rip),%rsi
mov    %r14,%rdi
call   1060 <strpbrk@plt>
test   %rax,%rax
je     1180 <fgetline+0x40>
movb   $0x0,(%rax)
jmp    118c <fgetline+0x4c>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%al
jne    1180 <fgetline+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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