#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 */
struct stat {
  scalar_t__ st_ino;
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;
typedef scalar_t__ ino_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stdout;
int print_logs(char *filename, ino_t *new_inode, off_t *new_offset) {
  FILE *f;
  struct stat fs;
  char buf[4096];
  ino_t inode;
  off_t offset;
  inode = *new_inode;
  offset = *new_offset;
  if (stat(filename, &fs) != 0)
    return errno;
  if (inode != fs.st_ino) {
    inode = fs.st_ino;
    offset = 0;
  }
  if (offset > fs.st_size) {
    offset = 0;
  }
  if ((f = fopen(filename, "r")) == NULL)
    return errno;
  fseek(f, offset, SEEK_SET);
  while ((fgets(buf, 4096, f)) != NULL)
    printf("%s", buf);
  fflush(stdout);
  fclose(f);
  if (ferror(f) != 0)
    return errno;
  *new_inode = fs.st_ino;
  *new_offset = fs.st_size;
  return 0;
}


x86 assembly (clang, -O0):
<print_logs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x1050(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11db <print_logs+0x5b>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <print_logs+0x175>
mov    -0x1048(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     1202 <print_logs+0x82>
mov    -0x38(%rbp),%rax
mov    %rax,-0x1048(%rbp)
movq   $0x0,-0x1050(%rbp)
mov    -0x1050(%rbp),%rax
cmp    -0x30(%rbp),%rax
jle    121e <print_logs+0x9e>
movq   $0x0,-0x1050(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    124d <print_logs+0xcd>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <print_logs+0x175>
mov    -0x28(%rbp),%rdi
mov    -0x1050(%rbp),%rsi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
lea    -0x1040(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     129d <print_logs+0x11d>
lea    -0x1040(%rbp),%rsi
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
jmp    1266 <print_logs+0xe6>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12d8 <print_logs+0x158>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <print_logs+0x175>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_logs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rsi),%rbp
mov    (%rdx),%r13
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11c8 <print_logs+0x48>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
cmp    (%rsp),%rbp
cmove  %r13,%r12
mov    0x8(%rsp),%rbp
lea    0xe21(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11ad <print_logs+0x2d>
mov    %rax,%r13
xor    %esi,%esi
cmp    %rbp,%r12
cmovle %r12,%rsi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
call   1090 <fseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1250 <print_logs+0xd0>
lea    0xddb(%rip),%r12
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1230 <print_logs+0xb0>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11ad <print_logs+0x2d>
mov    (%rsp),%rax
mov    %rax,(%r15)
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
xor    %eax,%eax
jmp    11b6 <print_logs+0x36>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@errno = global i32 0
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@stdout = global i32 0
define i32 @print_logs(i8* %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca %struct.stat
%10 = alloca [4096 x i8]
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64* %2, i64** %7
%13 = load i64*, i64** %6
%14 = load i64, i64* %13
store i64 %14, i64* %11
%15 = load i64*, i64** %7
%16 = load i64, i64* %15
store i64 %16, i64* %12
%17 = load i8*, i8** %5
%18 = call i64 @stat(i8* %17, %struct.stat* %9)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = load i32, i32* @errno
store i32 %21, i32* %4
br label %72
22: ; preds = %3
%23 = load i64, i64* %11
%24 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp ne i64 %23, %25
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%29 = load i64, i64* %28
store i64 %29, i64* %11
store i64 0, i64* %12
br label %30
30: ; preds = %27, %22
%31 = load i64, i64* %12
%32 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%33 = load i64, i64* %32
%34 = icmp sgt i64 %31, %33
br i1 %34, label %35, label %36
35: ; preds = %30
store i64 0, i64* %12
br label %36
36: ; preds = %35, %30
%37 = load i8*, i8** %5
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %38, i32** %8
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32, i32* @errno
store i32 %41, i32* %4
br label %72
42: ; preds = %36
%43 = load i32*, i32** %8
%44 = load i64, i64* %12
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %43, i64 %44, i32 %45)
br label %47
47: ; preds = %52, %42
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%49 = load i32*, i32** %8
%50 = call i32* @fgets(i8* %48, i32 4096, i32* %49)
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%54 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %53)
br label %47
55: ; preds = %47
%56 = load i32, i32* @stdout
%57 = call i32 @fflush(i32 %56)
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i32*, i32** %8
%61 = call i64 @ferror(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = load i32, i32* @errno
store i32 %64, i32* %4
br label %72
65: ; preds = %55
%66 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%67 = load i64, i64* %66
%68 = load i64*, i64** %6
store i64 %67, i64* %68
%69 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%70 = load i64, i64* %69
%71 = load i64*, i64** %7
store i64 %70, i64* %71
store i32 0, i32* %4
br label %72
72: ; preds = %65, %63, %40, %20
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@errno = global i32 0
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@stdout = global i32 0
define i32 @print_logs(i8* %0, i64* nocapture %1, i64* nocapture %2) {
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = load i64, i64* %1
%9 = load i64, i64* %2
%10 = call i64 @stat(i8* %0, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = load i32, i32* @errno
br label %47
14: ; preds = %3
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%16 = load i64, i64* %15
%17 = icmp eq i64 %8, %16
%18 = select i1 %17, i64 %9, i64 0
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%20 = load i64, i64* %19
%21 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = load i32, i32* @errno
br label %47
25: ; preds = %14
%26 = icmp sgt i64 %18, %20
%27 = select i1 %26, i64 0, i64 %18
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %21, i64 %27, i32 %28)
%30 = call i32* @fgets(i8* %7, i32 4096, i32* %21)
%31 = icmp eq i32* %30, null
br i1 %31, label %36, label %32
32: ; preds = %25, %32
%33 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%34 = call i32* @fgets(i8* %7, i32 4096, i32* %21)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %32
36: ; preds = %32, %25
%37 = load i32, i32* @stdout
%38 = call i32 @fflush(i32 %37)
%39 = call i32 @fclose(i32* %21)
%40 = call i64 @ferror(i32* %21)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = load i32, i32* @errno
br label %47
44: ; preds = %36
%45 = load i64, i64* %15
store i64 %45, i64* %1
%46 = load i64, i64* %19
store i64 %46, i64* %2
br label %47
47: ; preds = %44, %42, %23, %12
%48 = phi i32 [ %13, %12 ], [ %24, %23 ], [ %43, %42 ], [ 0, %44 ]
ret i32 %48
}