#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 EOF;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int read_file_backwards(int /*<<< orphan*/ *);
void set_file_position(long tail_length, FILE *input) {
  int character;
  (void)fseek(input, 0L, SEEK_END);
  while ((character = read_file_backwards(input)) != EOF && tail_length > 0L)
    if (character == '\n')
      --tail_length;
  (void)fseek(input, 1L, SEEK_CUR);
}


x86 assembly (clang, -O0):
<set_file_position>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ead(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1040 <read_file_backwards@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
mov    0x2e7b(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x15(%rbp)
je     1173 <set_file_position+0x53>
cmpq   $0x0,-0x8(%rbp)
setg   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1183 <set_file_position+0x63>
jmp    119e <set_file_position+0x7e>
cmpl   $0xa,-0x14(%rbp)
jne    1199 <set_file_position+0x79>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1146 <set_file_position+0x26>
mov    -0x10(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1030 <fseek@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_file_position>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2eb3(%rip),%rax
mov    (%rax),%edx
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <fseek@plt>
mov    %r14,%rdi
call   1040 <read_file_backwards@plt>
mov    0x2e88(%rip),%r15
cmp    (%r15),%eax
je     117e <set_file_position+0x5e>
test   %r12,%r12
jle    117e <set_file_position+0x5e>
nopw   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cmp    $0xa,%eax
sete   %bpl
mov    %r14,%rdi
call   1040 <read_file_backwards@plt>
cmp    (%r15),%eax
je     117e <set_file_position+0x5e>
mov    %bpl,%bl
sub    %rbx,%r12
jne    1160 <set_file_position+0x40>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fseek@plt>

LLVM IR (-O0):
@SEEK_END = global i32 0
@EOF = global i32 0
@SEEK_CUR = global i32 0
define void @set_file_position(i64 %0, i32* %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
store i64 %0, i64* %3
store i32* %1, i32** %4
%6 = load i32*, i32** %4
%7 = load i32, i32* @SEEK_END
%8 = call i32 @fseek(i32* %6, i64 0, i32 %7)
br label %9
9: ; preds = %25, %2
%10 = load i32*, i32** %4
%11 = call i32 @read_file_backwards(i32* %10)
store i32 %11, i32* %5
%12 = load i32, i32* @EOF
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i64, i64* %3
%16 = icmp sgt i64 %15, 0
br label %17
17: ; preds = %14, %9
%18 = phi i1 [ false, %9 ], [ %16, %14 ]
br i1 %18, label %19, label %26
19: ; preds = %17
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i64, i64* %3
%24 = add nsw i64 %23, -1
store i64 %24, i64* %3
br label %25
25: ; preds = %22, %19
br label %9
26: ; preds = %17
%27 = load i32*, i32** %4
%28 = load i32, i32* @SEEK_CUR
%29 = call i32 @fseek(i32* %27, i64 1, i32 %28)
ret void
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@EOF = global i32 0
@SEEK_CUR = global i32 0
define void @set_file_position(i64 %0, i32* %1) {
%3 = load i32, i32* @SEEK_END
%4 = tail call i32 @fseek(i32* %1, i64 0, i32 %3)
%5 = tail call i32 @read_file_backwards(i32* %1)
%6 = load i32, i32* @EOF
%7 = icmp ne i32 %5, %6
%8 = icmp sgt i64 %0, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %21
10: ; preds = %2, %10
%11 = phi i32 [ %16, %10 ], [ %5, %2 ]
%12 = phi i64 [ %15, %10 ], [ %0, %2 ]
%13 = icmp eq i32 %11, 10
%14 = sext i1 %13 to i64
%15 = add nsw i64 %12, %14
%16 = tail call i32 @read_file_backwards(i32* %1)
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
%19 = icmp ne i64 %15, 0
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %10, label %21
21: ; preds = %10, %2
%22 = load i32, i32* @SEEK_CUR
%23 = tail call i32 @fseek(i32* %1, i64 1, i32 %22)
ret void
}