#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*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long get_file_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
char *strdup(char *);
char *get_last_line(const char *filepath) {
  FILE *fp;
  char buff[4096 + 1];
  int size, i;
  long fsize;
  if (NULL == (fp = fopen(filepath, "r"))) {
    perror("file cannot open at lastline");
    return NULL;
  }
  fsize = -1L * get_file_size(fp);
  if (size =
          fseek(fp, (((fsize) >= (-4096L)) ? (fsize) : (-4096L)), SEEK_END)) {
    perror("cannot seek");
    exit(1);
  }
  size = fread(buff, sizeof(char), 4096, fp);
  fclose(fp);
  buff[size] = '\0';
  i = size - 1;
  if (buff[i] == '\n') {
    buff[i] = '\0';
  }
  while (i >= 0 && buff[i] != '\n')
    --i;
  ++i;
  return strdup(&buff[i]);
}


x86 assembly (clang, -O0):
<get_last_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ca <get_last_line+0x4a>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1354 <get_last_line+0x1d4>
mov    -0x18(%rbp),%rdi
call   1060 <get_file_size@plt>
imul   $0xffffffffffffffff,%rax,%rax
mov    %rax,-0x1030(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x1038(%rbp)
cmpq   $0xfffffffffffff000,-0x1030(%rbp)
jl     120d <get_last_line+0x8d>
mov    -0x1030(%rbp),%rax
mov    %rax,-0x1040(%rbp)
jmp    1220 <get_last_line+0xa0>
mov    $0xfffffffffffff000,%rax
mov    %rax,-0x1040(%rbp)
jmp    1220 <get_last_line+0xa0>
mov    -0x1038(%rbp),%rdi
mov    -0x1040(%rbp),%rsi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
je     1261 <get_last_line+0xe1>
lea    0xdcd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movslq -0x1024(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1024(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1028(%rbp)
movslq -0x1028(%rbp),%rax
movsbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    12cf <get_last_line+0x14f>
movslq -0x1028(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
jmp    12d4 <get_last_line+0x154>
xor    %eax,%eax
cmpl   $0x0,-0x1028(%rbp)
mov    %al,-0x1041(%rbp)
jl     1304 <get_last_line+0x184>
movslq -0x1028(%rbp),%rax
movsbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
setne  %al
mov    %al,-0x1041(%rbp)
mov    -0x1041(%rbp),%al
test   $0x1,%al
jne    1317 <get_last_line+0x197>
jmp    132b <get_last_line+0x1ab>
mov    -0x1028(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1028(%rbp)
jmp    12d4 <get_last_line+0x154>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
movslq -0x1028(%rbp),%rax
lea    -0x1020(%rbp),%rdi
add    %rax,%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_last_line>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
lea    0xe6d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1255 <get_last_line+0xd5>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <get_file_size@plt>
mov    %rax,%rcx
neg    %rcx
cmp    $0x1001,%rax
mov    $0xfffffffffffff000,%rsi
cmovl  %rcx,%rsi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   1050 <fseek@plt>
test   %eax,%eax
jne    1265 <get_last_line+0xe5>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
movslq %ebx,%rax
movb   $0x0,(%rsp,%rax,1)
movzbl -0x1(%rsp,%rax,1),%ecx
xor    %edx,%edx
cmp    $0xa,%cl
cmovne %ecx,%edx
mov    %dl,-0x1(%rsp,%rax,1)
test   %ebx,%ebx
jle    1235 <get_last_line+0xb5>
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rbx),%eax
cmpb   $0xa,(%rsp,%rax,1)
je     1239 <get_last_line+0xb9>
add    $0xffffffff,%ebx
jg     1220 <get_last_line+0xa0>
mov    $0xffffffff,%eax
jmp    1239 <get_last_line+0xb9>
add    $0xffffffffffffffff,%rax
cltq
lea    (%rax,%r14,1),%rdi
add    $0x1,%rdi
call   10a0 <strdup@plt>
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda6(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    1248 <get_last_line+0xc8>
lea    0xdb3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [29 x i8] c"file cannot open at lastline\00"
@SEEK_END = global i32 0
@.str.2 = [12 x i8] c"cannot seek\00"
define i8* @get_last_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [4097 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = icmp eq i32* null, %10
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %76
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = call i64 @get_file_size(i32* %15)
%17 = mul nsw i64 -1, %16
store i64 %17, i64* %8
%18 = load i32*, i32** %4
%19 = load i64, i64* %8
%20 = icmp sge i64 %19, -4096
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i64, i64* %8
br label %24
23: ; preds = %14
br label %24
24: ; preds = %23, %21
%25 = phi i64 [ %22, %21 ], [ -4096, %23 ]
%26 = load i32, i32* @SEEK_END
%27 = call i32 @fseek(i32* %18, i64 %25, i32 %26)
store i32 %27, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 0
%34 = load i32*, i32** %4
%35 = call i32 @fread(i8* %33, i32 1, i32 4096, i32* %34)
store i32 %35, i32* %6
%36 = load i32*, i32** %4
%37 = call i32 @fclose(i32* %36)
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, 1
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %53
49: ; preds = %32
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %49, %32
br label %54
54: ; preds = %66, %53
%55 = load i32, i32* %7
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 10
br label %64
64: ; preds = %57, %54
%65 = phi i1 [ false, %54 ], [ %63, %57 ]
br i1 %65, label %66, label %69
66: ; preds = %64
%67 = load i32, i32* %7
%68 = add nsw i32 %67, -1
store i32 %68, i32* %7
br label %54
69: ; preds = %64
%70 = load i32, i32* %7
%71 = add nsw i32 %70, 1
store i32 %71, i32* %7
%72 = load i32, i32* %7
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 %73
%75 = call i8* @strdup(i8* %74)
store i8* %75, i8** %2
br label %76
76: ; preds = %69, %12
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [29 x i8] c"file cannot open at lastline\00"
@SEEK_END = global i32 0
@.str.2 = [12 x i8] c"cannot seek\00"
define noalias i8* @get_last_line(i8* nocapture %0) {
%2 = alloca [4097 x i8]
%3 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %46
8: ; preds = %1
%9 = tail call i64 @get_file_size(i32* %4)
%10 = sub nsw i64 0, %9
%11 = icmp slt i64 %9, 4097
%12 = select i1 %11, i64 %10, i64 -4096
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %4, i64 %12, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %8
%20 = call i32 @fread(i8* %3, i32 1, i32 4096, i32* %4)
%21 = tail call i32 @fclose(i32* %4)
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %22
store i8 0, i8* %23
%24 = add nsw i32 %20, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 10
%29 = select i1 %28, i8 0, i8 %27
store i8 %29, i8* %26
%30 = icmp sgt i32 %20, 0
br i1 %30, label %31, label %40
31: ; preds = %19, %37
%32 = phi i32 [ %38, %37 ], [ %24, %19 ]
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 10
br i1 %36, label %40, label %37
37: ; preds = %31
%38 = add nsw i32 %32, -1
%39 = icmp sgt i32 %32, 0
br i1 %39, label %31, label %40
40: ; preds = %31, %37, %19
%41 = phi i32 [ %24, %19 ], [ -1, %37 ], [ %32, %31 ]
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %43
%45 = call i8* @strdup(i8* %44)
br label %46
46: ; preds = %40, %6
%47 = phi i8* [ null, %6 ], [ %45, %40 ]
ret i8* %47
}