#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 {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int LINE_MAX;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ warn(char *, char *);
void rotatelog(char *path) {
  struct stat st;
  FILE *fp;
  char *buf;
  size_t n, i, j;
  printf("Rotating: %s\n", path);
  if (stat(path, &st) == -1) {
    warn("Unable to stat: %s", path);
    return;
  }
  if (st.st_size <= 10000 * LINE_MAX) {
    printf("No need to rotate log...\n");
  }
  buf = malloc(10000 * LINE_MAX);
  if (buf == NULL)
    return;
  fp = fopen(path, "r");
  if (fp == NULL) {
    free(buf);
    return;
  }
  fseek(fp, st.st_size - 10000 * LINE_MAX, SEEK_SET);
  n = 0;
  while ((i = fread(buf + n, 1, 1024, fp)) > 0) {
    n += i;
  }
  fclose(fp);
  unlink(path);
  fp = fopen(path, "w");
  if (fp == NULL) {
    free(buf);
    warn("Losing data in %s", path);
    return;
  }
  i = 0;
  while (i < n) {
    j = fwrite(buf + i, 1, (1024) < (n) ? (1024) : (n), fp);
    i += j;
  }
  fclose(fp);
  free(buf);
}


x86 assembly (clang, -O0):
<rotatelog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    11f9 <rotatelog+0x49>
mov    -0x8(%rbp),%rsi
lea    0xe1f(%rip),%rdi
call   1050 <warn@plt>
jmp    13ad <rotatelog+0x1fd>
mov    -0x10(%rbp),%eax
mov    0x2de5(%rip),%rcx
imul   $0x2710,(%rcx),%ecx
cmp    %ecx,%eax
jg     121f <rotatelog+0x6f>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2dc2(%rip),%rax
imul   $0x2710,(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1245 <rotatelog+0x95>
jmp    13ad <rotatelog+0x1fd>
mov    -0x8(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1272 <rotatelog+0xc2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    13ad <rotatelog+0x1fd>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2d68(%rip),%rax
imul   $0x2710,(%rax),%eax
sub    %eax,%esi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
call   10b0 <fseek@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1060 <fread@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    12d8 <rotatelog+0x128>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    129e <rotatelog+0xee>
mov    -0x18(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x8(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x8(%rbp),%rdi
lea    0xd48(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1327 <rotatelog+0x177>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rsi
lea    0xd22(%rip),%rdi
call   1050 <warn@plt>
jmp    13ad <rotatelog+0x1fd>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    139b <rotatelog+0x1eb>
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x400,%eax
cmp    -0x28(%rbp),%rax
jae    1366 <rotatelog+0x1b6>
mov    $0x400,%eax
mov    %rax,-0x48(%rbp)
jmp    136e <rotatelog+0x1be>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    132f <rotatelog+0x17f>
mov    -0x18(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rotatelog>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe2a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
je     1318 <rotatelog+0x158>
mov    0x8(%rsp),%ebp
mov    0x2de7(%rip),%r12
imul   $0x2710,(%r12),%edi
cmp    %edi,%ebp
jg     1221 <rotatelog+0x61>
lea    0xe23(%rip),%rdi
call   1060 <puts@plt>
imul   $0x2710,(%r12),%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1321 <rotatelog+0x161>
mov    %rax,%r15
lea    0xde8(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1304 <rotatelog+0x144>
mov    %rax,%rbx
imul   $0xffffd8f0,(%r12),%esi
ff
add    %ebp,%esi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   10c0 <fseek@plt>
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
mov    %rcx,%rbp
lea    (%r15,%rcx,1),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1070 <fread@plt>
lea    (%rax,%rbp,1),%rcx
test   %rax,%rax
jne    1270 <rotatelog+0xb0>
mov    %rbx,%rdi
call   1080 <fclose@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
lea    0xd7a(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     132e <rotatelog+0x16e>
mov    %rax,%r12
test   %rbp,%rbp
je     12fc <rotatelog+0x13c>
cmp    $0x400,%rbp
mov    $0x400,%r14d
cmovb  %ebp,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10e0 <fwrite@plt>
add    %rax,%rbx
cmp    %rbp,%rbx
jb     12e0 <rotatelog+0x120>
mov    %r12,%rdi
call   1080 <fclose@plt>
mov    %r15,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xcef(%rip),%rdi
jmp    133d <rotatelog+0x17d>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xce8(%rip),%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <warn@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"Rotating: %s\0A\00"
@.str.1 = [19 x i8] c"Unable to stat: %s\00"
@LINE_MAX = global i32 0
@.str.2 = [26 x i8] c"No need to rotate log...\0A\00"
@.str.3 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [18 x i8] c"Losing data in %s\00"
define void @rotatelog(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %2
%12 = call i32 @stat(i8* %11, %struct.stat* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %101
17: ; preds = %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @LINE_MAX
%21 = mul nsw i32 10000, %20
%22 = icmp sle i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = load i32, i32* @LINE_MAX
%27 = mul nsw i32 10000, %26
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %25
br label %101
32: ; preds = %25
%33 = load i8*, i8** %2
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %4
%35 = load i32*, i32** %4
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
br label %101
40: ; preds = %32
%41 = load i32*, i32** %4
%42 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%43 = load i32, i32* %42
%44 = load i32, i32* @LINE_MAX
%45 = mul nsw i32 10000, %44
%46 = sub nsw i32 %43, %45
%47 = load i32, i32* @SEEK_SET
%48 = call i32 @fseek(i32* %41, i32 %46, i32 %47)
store i64 0, i64* %6
br label %49
49: ; preds = %56, %40
%50 = load i8*, i8** %5
%51 = load i64, i64* %6
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = load i32*, i32** %4
%54 = call i64 @fread(i8* %52, i32 1, i32 1024, i32* %53)
store i64 %54, i64* %7
%55 = icmp ugt i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %49
%57 = load i64, i64* %7
%58 = load i64, i64* %6
%59 = add i64 %58, %57
store i64 %59, i64* %6
br label %49
60: ; preds = %49
%61 = load i32*, i32** %4
%62 = call i32 @fclose(i32* %61)
%63 = load i8*, i8** %2
%64 = call i32 @unlink(i8* %63)
%65 = load i8*, i8** %2
%66 = call i32* @fopen(i8* %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %66, i32** %4
%67 = load i32*, i32** %4
%68 = icmp eq i32* %67, null
br i1 %68, label %69, label %74
69: ; preds = %60
%70 = load i8*, i8** %5
%71 = call i32 @free(i8* %70)
%72 = load i8*, i8** %2
%73 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %72)
br label %101
74: ; preds = %60
store i64 0, i64* %7
br label %75
75: ; preds = %88, %74
%76 = load i64, i64* %7
%77 = load i64, i64* %6
%78 = icmp ult i64 %76, %77
br i1 %78, label %79, label %96
79: ; preds = %75
%80 = load i8*, i8** %5
%81 = load i64, i64* %7
%82 = getelementptr inbounds i8, i8* %80, i64 %81
%83 = load i64, i64* %6
%84 = icmp ult i64 1024, %83
br i1 %84, label %85, label %86
85: ; preds = %79
br label %88
86: ; preds = %79
%87 = load i64, i64* %6
br label %88
88: ; preds = %86, %85
%89 = phi i64 [ 1024, %85 ], [ %87, %86 ]
%90 = trunc i64 %89 to i32
%91 = load i32*, i32** %4
%92 = call i64 @fwrite(i8* %82, i32 1, i32 %90, i32* %91)
store i64 %92, i64* %8
%93 = load i64, i64* %8
%94 = load i64, i64* %7
%95 = add i64 %94, %93
store i64 %95, i64* %7
br label %75
96: ; preds = %75
%97 = load i32*, i32** %4
%98 = call i32 @fclose(i32* %97)
%99 = load i8*, i8** %5
%100 = call i32 @free(i8* %99)
br label %101
101: ; preds = %96, %69, %37, %31, %14
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"Rotating: %s\0A\00"
@.str.1 = [19 x i8] c"Unable to stat: %s\00"
@LINE_MAX = global i32 0
@.str.3 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [18 x i8] c"Losing data in %s\00"
@str = [25 x i8] c"No need to rotate log...\00"
define void @rotatelog(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32 @stat(i8* %0, %struct.stat* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %63
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @LINE_MAX
%13 = mul nsw i32 %12, 10000
%14 = icmp sgt i32 %11, %13
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @LINE_MAX
%18 = mul nsw i32 %17, 10000
br label %19
19: ; preds = %15, %9
%20 = phi i32 [ %18, %15 ], [ %13, %9 ]
%21 = tail call i8* @malloc(i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %63, label %23
23: ; preds = %19
%24 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @free(i8* %21)
br label %63
28: ; preds = %23
%29 = load i32, i32* @LINE_MAX
%30 = mul i32 %29, -10000
%31 = add i32 %30, %11
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @fseek(i32* %24, i32 %31, i32 %32)
br label %34
34: ; preds = %34, %28
%35 = phi i64 [ 0, %28 ], [ %39, %34 ]
%36 = getelementptr inbounds i8, i8* %21, i64 %35
%37 = tail call i64 @fread(i8* %36, i32 1, i32 1024, i32* %24)
%38 = icmp eq i64 %37, 0
%39 = add i64 %37, %35
br i1 %38, label %40, label %34
40: ; preds = %34
%41 = tail call i32 @fclose(i32* %24)
%42 = tail call i32 @unlink(i8* %0)
%43 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp eq i32* %43, null
br i1 %44, label %51, label %45
45: ; preds = %40
%46 = icmp eq i64 %35, 0
br i1 %46, label %60, label %47
47: ; preds = %45
%48 = icmp ult i64 %35, 1024
%49 = select i1 %48, i64 %35, i64 1024
%50 = trunc i64 %49 to i32
br label %54
51: ; preds = %40
%52 = tail call i32 @free(i8* %21)
%53 = tail call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %63
54: ; preds = %47, %54
%55 = phi i64 [ 0, %47 ], [ %58, %54 ]
%56 = getelementptr inbounds i8, i8* %21, i64 %55
%57 = tail call i64 @fwrite(i8* %56, i32 1, i32 %50, i32* %43)
%58 = add i64 %57, %55
%59 = icmp ult i64 %58, %35
br i1 %59, label %54, label %60
60: ; preds = %54, %45
%61 = tail call i32 @fclose(i32* %43)
%62 = tail call i32 @free(i8* %21)
br label %63
63: ; preds = %19, %60, %51, %26, %7
ret void
}