#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;
};
typedef scalar_t__ ino_t;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int fstat(int, struct stat *);
int open(char const *, int, ...);
int rename(char const *, char *);
int /*<<< orphan*/ takewritelock(int);
int rolloverfile(const char *filename, int fd) {
  struct stat statbuf;
  ino_t inode1, inode2;
  int fd2;
  int res = -1;
  fd2 = open(filename, O_WRONLY);
  res = fstat(fd, &statbuf);
  if (res < 0)
    goto RETURN;
  inode1 = statbuf.st_ino;
  res = fstat(fd2, &statbuf);
  if (res < 0)
    goto RETURN;
  inode2 = statbuf.st_ino;
  if (inode1 == inode2) {
    close(fd2);
    res = rename(filename, "hallo.2");
    if (res < 0)
      goto RETURN;
    fd2 = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
    if (fd2 < 0)
      goto RETURN;
  }
  takewritelock(fd2);
  close(fd);
  res = dup2(fd2, fd);
RETURN:
  return res;
}


x86 assembly (clang, -O0):
<rolloverfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11ab <rolloverfile+0x4b>
jmp    1271 <rolloverfile+0x111>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11d1 <rolloverfile+0x71>
jmp    1271 <rolloverfile+0x111>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1253 <rolloverfile+0xf3>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1211 <rolloverfile+0xb1>
jmp    1271 <rolloverfile+0x111>
mov    -0x8(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
mov    0x2daa(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    124e <rolloverfile+0xee>
jmp    1271 <rolloverfile+0x111>
jmp    1253 <rolloverfile+0xf3>
mov    -0x2c(%rbp),%edi
call   1030 <takewritelock@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <dup2@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rolloverfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e50(%rip),%r12
mov    (%r12),%esi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %r14d,%edi
call   1080 <fstat@plt>
test   %eax,%eax
js     121e <rolloverfile+0xbe>
mov    (%rsp),%r13
mov    %rsp,%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
test   %eax,%eax
js     121e <rolloverfile+0xbe>
cmp    (%rsp),%r13
jne    11f7 <rolloverfile+0x97>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe42(%rip),%rsi
mov    %r15,%rdi
call   1070 <rename@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1220 <rolloverfile+0xc0>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
or     (%r12),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1220 <rolloverfile+0xc0>
mov    %ebx,%edi
call   1030 <takewritelock@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <dup2@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_WRONLY = global i32 0
@.str = [8 x i8] c"hallo.2\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
define i32 @rolloverfile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 -1, i32* %9
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_WRONLY
%12 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %4
%14 = call i32 @fstat(i32 %13, %struct.stat* %5)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
br label %60
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %6
%21 = load i32, i32* %8
%22 = call i32 @fstat(i32 %21, %struct.stat* %5)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
br label %60
26: ; preds = %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %7
%29 = load i64, i64* %6
%30 = load i64, i64* %7
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %52
32: ; preds = %26
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %3
%36 = call i32 @rename(i8* %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
br label %60
40: ; preds = %32
%41 = load i8*, i8** %3
%42 = load i32, i32* @O_WRONLY
%43 = load i32, i32* @O_CREAT
%44 = or i32 %42, %43
%45 = load i32, i32* @O_EXCL
%46 = or i32 %44, %45
%47 = call i32 (i8*, i32, ...) @open(i8* %41, i32 %46, i32 438)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %40
br label %60
51: ; preds = %40
br label %52
52: ; preds = %51, %26
%53 = load i32, i32* %8
%54 = call i32 @takewritelock(i32 %53)
%55 = load i32, i32* %4
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %8
%58 = load i32, i32* %4
%59 = call i32 @dup2(i32 %57, i32 %58)
store i32 %59, i32* %9
br label %60
60: ; preds = %52, %50, %39, %25, %17
%61 = load i32, i32* %9
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_WRONLY = global i32 0
@.str = [8 x i8] c"hallo.2\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
define i32 @rolloverfile(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = call i32 @fstat(i32 %1, %struct.stat* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %34, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i64, i64* %10
%12 = call i32 @fstat(i32 %6, %struct.stat* %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %34, label %14
14: ; preds = %9
%15 = load i64, i64* %10
%16 = icmp eq i64 %11, %15
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = tail call i32 @close(i32 %6)
%19 = tail call i32 @rename(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i32 %19, 0
br i1 %20, label %34, label %21
21: ; preds = %17
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_EXCL
%26 = or i32 %24, %25
%27 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %26, i32 438)
%28 = icmp slt i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %21, %14
%30 = phi i32 [ %27, %21 ], [ %6, %14 ]
%31 = tail call i32 @takewritelock(i32 %30)
%32 = tail call i32 @close(i32 %1)
%33 = tail call i32 @dup2(i32 %30, i32 %1)
br label %34
34: ; preds = %21, %17, %9, %2, %29
%35 = phi i32 [ %7, %2 ], [ %12, %9 ], [ %19, %17 ], [ %19, %21 ], [ %33, %29 ]
ret i32 %35
}