#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 */
long const find_last_in_filestream(int /*<<< orphan*/ *, char);
char *fs_first_line(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long const, int);
long ftell(int /*<<< orphan*/ *);
char *restrict fs_read_nth_line_from_end(FILE *file_stream, int nth) {
  const long int origin = ftell(file_stream);
  char *restrict return_string;
  int current_index = 0;
  fseek(file_stream, 0, 2);
  do {
    fseek(file_stream, find_last_in_filestream(file_stream, '\n'), 1);
  } while (current_index++ < nth);
  fseek(file_stream, 1, 1);
  return_string = fs_first_line(file_stream);
  fseek(file_stream, origin, 0);
  return return_string;
}


x86 assembly (clang, -O0):
<fs_read_nth_line_from_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x2,%edx
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1050 <find_last_in_filestream@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1060 <fseek@plt>
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cmp    -0xc(%rbp),%eax
jl     1175 <fs_read_nth_line_from_end+0x35>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fs_first_line@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_read_nth_line_from_end>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
call   1030 <ftell@plt>
mov    %rax,%r14
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1060 <fseek@plt>
test   %r15d,%r15d
cmovg  %r15d,%ebp
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <find_last_in_filestream@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1060 <fseek@plt>
add    $0xffffffff,%ebp
jne    1170 <fs_read_nth_line_from_end+0x30>
mov    $0x1,%esi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1040 <fs_first_line@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @fs_read_nth_line_from_end(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32*, i32** %3
%9 = call i64 @ftell(i32* %8)
store i64 %9, i64* %5
store i32 0, i32* %7
%10 = load i32*, i32** %3
%11 = call i32 @fseek(i32* %10, i64 0, i32 2)
br label %12
12: ; preds = %17, %2
%13 = load i32*, i32** %3
%14 = load i32*, i32** %3
%15 = call i64 @find_last_in_filestream(i32* %14, i8 signext 10)
%16 = call i32 @fseek(i32* %13, i64 %15, i32 1)
br label %17
17: ; preds = %12
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
store i32 %19, i32* %7
%20 = load i32, i32* %4
%21 = icmp slt i32 %18, %20
br i1 %21, label %12, label %22
22: ; preds = %17
%23 = load i32*, i32** %3
%24 = call i32 @fseek(i32* %23, i64 1, i32 1)
%25 = load i32*, i32** %3
%26 = call i8* @fs_first_line(i32* %25)
store i8* %26, i8** %6
%27 = load i32*, i32** %3
%28 = load i64, i64* %5
%29 = call i32 @fseek(i32* %27, i64 %28, i32 0)
%30 = load i8*, i8** %6
ret i8* %30
}

LLVM IR (-O2):
define i8* @fs_read_nth_line_from_end(i32* %0, i32 %1) {
%3 = tail call i64 @ftell(i32* %0)
%4 = tail call i32 @fseek(i32* %0, i64 0, i32 2)
%5 = call i32 @llvm.smax.i32(i32 %1, i32 0)
br label %6
6: ; preds = %6, %2
%7 = phi i32 [ 0, %2 ], [ %10, %6 ]
%8 = tail call i64 @find_last_in_filestream(i32* %0, i8 signext 10)
%9 = tail call i32 @fseek(i32* %0, i64 %8, i32 1)
%10 = add nuw i32 %7, 1
%11 = icmp eq i32 %7, %5
br i1 %11, label %12, label %6
12: ; preds = %6
%13 = tail call i32 @fseek(i32* %0, i64 1, i32 1)
%14 = tail call i8* @fs_first_line(i32* %0)
%15 = tail call i32 @fseek(i32* %0, i64 %3, i32 0)
ret i8* %14
}