#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;
};

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wrename(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int writelog(char *path, char *log) {
  if (!access(path, F_OK)) {
    struct stat buf;
    stat(path, &buf);
    if (buf.st_size > 102400) {
      wrename(path, strlen(path), 0);
    }
  }
  int fd =
      open(path, O_APPEND | O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
  if (fd >= 0) {
    write(fd, log, strlen(log));
  } else {
    perror("open cssh log");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<writelog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%eax
jne    11e8 <writelog+0x68>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmpl   $0x19000,-0x18(%rbp)
jle    11e3 <writelog+0x63>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1030 <wrename@plt>
jmp    11e8 <writelog+0x68>
mov    -0x8(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
mov    0x2dc8(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     125e <writelog+0xde>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    126a <writelog+0xea>
lea    0xd9b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writelog>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
test   %eax,%eax
jne    11c4 <writelog+0x44>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmpl   $0x19001,(%rsp)
jl     11c4 <writelog+0x44>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1030 <wrename@plt>
mov    0x2de5(%rip),%rax
mov    0x2de6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
or     (%rax),%esi
mov    0x2df2(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df0(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     122d <writelog+0xad>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdcc(%rip),%rdi
call   10a0 <perror@plt>
jmp    121c <writelog+0x9c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@F_OK = global i32 0
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str = [14 x i8] c"open cssh log\00"
define i32 @writelog(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @F_OK
%9 = call i32 @access(i8* %7, i32 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 @stat(i8* %12, %struct.stat* %5)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = icmp sgt i32 %15, 102400
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i8*, i8** %3
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @wrename(i8* %18, i32 %20, i32 0)
br label %22
22: ; preds = %17, %11
br label %23
23: ; preds = %22, %2
%24 = load i8*, i8** %3
%25 = load i32, i32* @O_APPEND
%26 = load i32, i32* @O_WRONLY
%27 = or i32 %25, %26
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRWXU
%31 = load i32, i32* @S_IRWXG
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRWXO
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %24, i32 %29, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %23
%39 = load i32, i32* %6
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* %41)
%43 = call i32 @write(i32 %39, i8* %40, i32 %42)
br label %46
44: ; preds = %23
%45 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %44, %38
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@F_OK = global i32 0
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str = [14 x i8] c"open cssh log\00"
define i32 @writelog(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @F_OK
%5 = tail call i32 @access(i8* %0, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = bitcast %struct.stat* %3 to i8*
%9 = call i32 @stat(i8* %0, %struct.stat* %3)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp sgt i32 %11, 102400
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @strlen(i8* %0)
%15 = tail call i32 @wrename(i8* %0, i32 %14, i32 0)
br label %16
16: ; preds = %13, %7
br label %17
17: ; preds = %16, %2
%18 = load i32, i32* @O_APPEND
%19 = load i32, i32* @O_WRONLY
%20 = or i32 %19, %18
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRWXU
%24 = load i32, i32* @S_IRWXG
%25 = or i32 %24, %23
%26 = load i32, i32* @S_IRWXO
%27 = or i32 %25, %26
%28 = tail call i32 @open(i8* %0, i32 %22, i32 %27)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = tail call i32 @strlen(i8* %1)
%32 = tail call i32 @write(i32 %28, i8* %1, i32 %31)
br label %35
33: ; preds = %17
%34 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = tail call i32 @close(i32 %28)
ret i32 0
}