#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
char *xfgets(FILE *file) {
  size_t linecap;
  ssize_t l;
  char *s;
  clearerr(file);
  linecap = 0;
  s = NULL;
  if ((l = getline(&s, &linecap, file)) == -1) {
    if (ferror(file))
      err(2, "error reading file");
    return (NULL);
  }
  if (s[l - 1] == '\n')
    s[l - 1] = '\0';
  return (s);
}


x86 assembly (clang, -O0):
<xfgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <clearerr@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11b3 <xfgets+0x73>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a6 <xfgets+0x66>
mov    $0x2,%edi
lea    0xe5f(%rip),%rsi
call   1050 <err@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e6 <xfgets+0xa6>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11de <xfgets+0x9e>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfgets>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <clearerr@plt>
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
je     118e <xfgets+0x4e>
mov    %eax,%ecx
mov    (%rsp),%rax
movslq %ecx,%rcx
cmpb   $0xa,-0x1(%rcx,%rax,1)
jne    11ae <xfgets+0x6e>
movb   $0x0,-0x1(%rax,%rcx,1)
mov    (%rsp),%rax
jmp    11ae <xfgets+0x6e>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11ac <xfgets+0x6c>
lea    0xe5e(%rip),%rsi
mov    $0x2,%edi
call   1050 <err@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [19 x i8] c"error reading file\00"
define i8* @xfgets(i32* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = bitcast i64* %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = tail call i32 @clearerr(i32* %0)
store i64 0, i64* %2
store i8* null, i8** %3
%7 = call i32 @getline(i8** %3, i64* %2, i32* %0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = call i64 @ferror(i32* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = call i32 @err(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %23
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = add nsw i32 %7, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %23
21: ; preds = %14
store i8 0, i8* %18
%22 = load i8*, i8** %3
br label %23
23: ; preds = %14, %21, %9, %12
%24 = phi i8* [ null, %12 ], [ null, %9 ], [ %22, %21 ], [ %15, %14 ]
ret i8* %24
}