#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*/ chop_newline(char *);
int /*<<< orphan*/ debug(char *, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int read_last_line(char *filename, char *buf, int size) {
  FILE *file;
  file = fopen(filename, "r");
  if (file == NULL) {
    debug("error opening '%s': %s", filename, strerror(errno));
    return 0;
  }
  buf[0] = '\0';
  while (fgets(buf, size, file))
    ;
  fclose(file);
  if (!buf[0]) {
    debug("empty line read from '%s'", filename);
    return 0;
  }
  chop_newline(buf);
  return 1;
}


x86 assembly (clang, -O0):
<read_last_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <read_last_line+0x68>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    1232 <read_last_line+0xd2>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11ee <read_last_line+0x8e>
jmp    11cf <read_last_line+0x6f>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1222 <read_last_line+0xc2>
mov    -0x10(%rbp),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    1232 <read_last_line+0xd2>
mov    -0x18(%rbp),%rdi
call   1060 <chop_newline@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_last_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bf <read_last_line+0x5f>
mov    %rax,%rbx
movb   $0x0,(%r15)
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <read_last_line+0x30>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmpb   $0x0,(%r15)
je     11e4 <read_last_line+0x84>
mov    %r15,%rdi
call   1060 <chop_newline@plt>
mov    $0x1,%ebx
jmp    11f7 <read_last_line+0x97>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2e(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <debug@plt>
jmp    11f7 <read_last_line+0x97>
lea    0xe2e(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <debug@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"error opening '%s': %s\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"empty line read from '%s'\00"
define i32 @read_last_line(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %16)
store i32 0, i32* %4
br label %41
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 0, i8* %20
br label %21
21: ; preds = %27, %18
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = load i32*, i32** %8
%25 = call i64 @fgets(i8* %22, i32 %23, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %21
28: ; preds = %21
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %28
%36 = load i8*, i8** %5
%37 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 0, i32* %4
br label %41
38: ; preds = %28
%39 = load i8*, i8** %6
%40 = call i32 @chop_newline(i8* %39)
store i32 1, i32* %4
br label %41
41: ; preds = %38, %35, %13
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"error opening '%s': %s\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"empty line read from '%s'\00"
define i32 @read_last_line(i8* %0, i8* %1, i32 %2) {
%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 %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %22
10: ; preds = %3
store i8 0, i8* %1
br label %11
11: ; preds = %11, %10
%12 = tail call i64 @fgets(i8* %1, i32 %2, i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %11
14: ; preds = %11
%15 = tail call i32 @fclose(i32* %4)
%16 = load i8, i8* %1
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %22
20: ; preds = %14
%21 = tail call i32 @chop_newline(i8* %1)
br label %22
22: ; preds = %20, %18, %6
%23 = phi i32 [ 0, %6 ], [ 1, %20 ], [ 0, %18 ]
ret i32 %23
}