#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 ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ releaselock(int);
int /*<<< orphan*/ rolloverfile(char *, int);
int takewritelock(int);
int write(int, char const *, size_t);
int writelocked(int fd, const char *buffer, size_t size) {
  ssize_t wres = -1;
  off_t sres;
  int fres;
  fres = takewritelock(fd);
  if (fres < 0)
    goto RETURN;
  sres = lseek(fd, 0, SEEK_END);
  if (sres > 1000) {
    rolloverfile("hallo", fd);
    sres = lseek(fd, 0, SEEK_END);
    if (sres > 1000) {
      goto RETURN;
    }
  }
  if (sres < 0)
    goto RETURN;
  wres = write(fd, buffer, size);
  releaselock(fd);
RETURN:
  return wres;
}


x86 assembly (clang, -O0):
<writelocked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <takewritelock@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1184 <writelocked+0x34>
jmp    120d <writelocked+0xbd>
mov    -0x4(%rbp),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x3e8,-0x20(%rbp)
jle    11e3 <writelocked+0x93>
mov    -0x4(%rbp),%esi
lea    0xe4f(%rip),%rdi
call   1070 <rolloverfile@plt>
mov    -0x4(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x3e8,-0x20(%rbp)
jle    11de <writelocked+0x8e>
jmp    120d <writelocked+0xbd>
jmp    11e3 <writelocked+0x93>
cmpl   $0x0,-0x20(%rbp)
jge    11f2 <writelocked+0xa2>
jmp    120d <writelocked+0xbd>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <releaselock@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writelocked>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
call   1030 <takewritelock@plt>
test   %eax,%eax
js     11a3 <writelocked+0x53>
mov    0x2e79(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x3e9,%eax
jl     11aa <writelocked+0x5a>
lea    0xe77(%rip),%rdi
mov    %ebx,%esi
call   1070 <rolloverfile@plt>
mov    0x0(%rbp),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x3e8,%eax
jbe    11ae <writelocked+0x5e>
mov    $0xffffffff,%eax
jmp    11c6 <writelocked+0x76>
test   %eax,%eax
js     11a3 <writelocked+0x53>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <releaselock@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@.str = [6 x i8] c"hallo\00"
define i32 @writelocked(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 -1, i32* %7
%10 = load i32, i32* %4
%11 = call i32 @takewritelock(i32 %10)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
br label %42
15: ; preds = %3
%16 = load i32, i32* %4
%17 = load i32, i32* @SEEK_END
%18 = call i32 @lseek(i32 %16, i32 0, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 1000
br i1 %20, label %21, label %31
21: ; preds = %15
%22 = load i32, i32* %4
%23 = call i32 @rolloverfile(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %4
%25 = load i32, i32* @SEEK_END
%26 = call i32 @lseek(i32 %24, i32 0, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 1000
br i1 %28, label %29, label %30
29: ; preds = %21
br label %42
30: ; preds = %21
br label %31
31: ; preds = %30, %15
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
br label %42
35: ; preds = %31
%36 = load i32, i32* %4
%37 = load i8*, i8** %5
%38 = load i64, i64* %6
%39 = call i32 @write(i32 %36, i8* %37, i64 %38)
store i32 %39, i32* %7
%40 = load i32, i32* %4
%41 = call i32 @releaselock(i32 %40)
br label %42
42: ; preds = %35, %34, %29, %14
%43 = load i32, i32* %7
ret i32 %43
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@.str = [6 x i8] c"hallo\00"
define i32 @writelocked(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @takewritelock(i32 %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @lseek(i32 %0, i32 0, i32 %7)
%9 = icmp sgt i32 %8, 1000
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = tail call i32 @rolloverfile(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @lseek(i32 %0, i32 0, i32 %12)
%14 = icmp ugt i32 %13, 1000
br i1 %14, label %20, label %17
15: ; preds = %6
%16 = icmp slt i32 %8, 0
br i1 %16, label %20, label %17
17: ; preds = %10, %15
%18 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%19 = tail call i32 @releaselock(i32 %0)
br label %20
20: ; preds = %15, %10, %3, %17
%21 = phi i32 [ -1, %3 ], [ -1, %10 ], [ %18, %17 ], [ -1, %15 ]
ret i32 %21
}