#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ O_WRONLY;
int brstub_validate_version(char *, unsigned long);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int brstub_write_validation(char *filp, char *bpath,
                            unsigned long startversion) {
  int fd1 = 0;
  int fd2 = 0;
  int ret = 0;
  char *string = "string\n";
  fd1 = open(filp, O_RDONLY);
  if (fd1 < 0)
    goto err;
  close(fd1);
  ret = brstub_validate_version(bpath, startversion);
  if (ret != -2)
    goto err;
  fd1 = open(filp, O_RDWR);
  if (fd1 < 0)
    goto err;
  ret = write(fd1, string, strlen(string));
  if (ret <= 0)
    goto err;
  fsync(fd1);
  ret = brstub_validate_version(bpath, startversion);
  if (ret != 0)
    goto err;
  ret = write(fd1, string, strlen(string));
  if (ret <= 0)
    goto err;
  fsync(fd1);
  ret = brstub_validate_version(bpath, startversion);
  if (ret != 0)
    goto err;
  close(fd1);
  fd1 = open(filp, O_RDWR);
  if (fd1 < 0)
    goto err;
  fd2 = open(filp, O_WRONLY);
  if (fd1 < 0)
    goto err;
  ret = write(fd1, string, strlen(string));
  if (ret <= 0)
    goto err;
  ret = write(fd2, string, strlen(string));
  if (ret <= 0)
    goto err;
  fsync(fd1);
  fsync(fd2);
  close(fd1);
  close(fd2);
  ret = brstub_validate_version(bpath, startversion);
  if (ret != 0)
    goto err;
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<brstub_write_validation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe6d(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11bb <brstub_write_validation+0x5b>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <brstub_validate_version@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xfffffffe,-0x2c(%rbp)
je     11e2 <brstub_write_validation+0x82>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x10(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1206 <brstub_write_validation+0xa6>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     123d <brstub_write_validation+0xdd>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%edi
call   1070 <fsync@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <brstub_validate_version@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1264 <brstub_write_validation+0x104>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     129b <brstub_write_validation+0x13b>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%edi
call   1070 <fsync@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <brstub_validate_version@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12c2 <brstub_write_validation+0x162>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12ee <brstub_write_validation+0x18e>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x10(%rbp),%rdi
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1312 <brstub_write_validation+0x1b2>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     1349 <brstub_write_validation+0x1e9>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     1380 <brstub_write_validation+0x220>
jmp    13cb <brstub_write_validation+0x26b>
mov    -0x24(%rbp),%edi
call   1070 <fsync@plt>
mov    -0x28(%rbp),%edi
call   1070 <fsync@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <brstub_validate_version@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     13bf <brstub_write_validation+0x25f>
jmp    13cb <brstub_write_validation+0x26b>
movl   $0x0,-0x4(%rbp)
jmp    13d2 <brstub_write_validation+0x272>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brstub_write_validation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     11eb <brstub_write_validation+0x8b>
mov    %eax,%edi
call   1050 <close@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <brstub_validate_version@plt>
cmp    $0xfffffffe,%eax
jne    11eb <brstub_write_validation+0x8b>
mov    0x2e44(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11eb <brstub_write_validation+0x8b>
mov    %eax,%ebp
lea    0xe43(%rip),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11eb <brstub_write_validation+0x8b>
mov    %ebp,%edi
call   1070 <fsync@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <brstub_validate_version@plt>
test   %eax,%eax
je     11ff <brstub_write_validation+0x9f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11eb <brstub_write_validation+0x8b>
mov    %ebp,%edi
call   1070 <fsync@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <brstub_validate_version@plt>
test   %eax,%eax
jne    11eb <brstub_write_validation+0x8b>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11eb <brstub_write_validation+0x8b>
mov    %eax,%ebp
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r12d
lea    0xd98(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11eb <brstub_write_validation+0x8b>
lea    0xd75(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11eb <brstub_write_validation+0x8b>
mov    %ebp,%edi
call   1070 <fsync@plt>
mov    %r12d,%edi
call   1070 <fsync@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <brstub_validate_version@plt>
test   %eax,%eax
jne    11eb <brstub_write_validation+0x8b>
xor    %eax,%eax
jmp    11f0 <brstub_write_validation+0x90>

LLVM IR (-O0):
@.str = [8 x i8] c"string\0A\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
define i32 @brstub_write_validation(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
br label %120
18: ; preds = %3
%19 = load i32, i32* %8
%20 = call i32 @close(i32 %19)
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = call i32 @brstub_validate_version(i8* %21, i64 %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp ne i32 %24, -2
br i1 %25, label %26, label %27
26: ; preds = %18
br label %120
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %120
34: ; preds = %27
%35 = load i32, i32* %8
%36 = load i8*, i8** %11
%37 = load i8*, i8** %11
%38 = call i32 @strlen(i8* %37)
%39 = call i32 @write(i32 %35, i8* %36, i32 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp sle i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %34
br label %120
43: ; preds = %34
%44 = load i32, i32* %8
%45 = call i32 @fsync(i32 %44)
%46 = load i8*, i8** %6
%47 = load i64, i64* %7
%48 = call i32 @brstub_validate_version(i8* %46, i64 %47)
store i32 %48, i32* %10
%49 = load i32, i32* %10
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %43
br label %120
52: ; preds = %43
%53 = load i32, i32* %8
%54 = load i8*, i8** %11
%55 = load i8*, i8** %11
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @write(i32 %53, i8* %54, i32 %56)
store i32 %57, i32* %10
%58 = load i32, i32* %10
%59 = icmp sle i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %52
br label %120
61: ; preds = %52
%62 = load i32, i32* %8
%63 = call i32 @fsync(i32 %62)
%64 = load i8*, i8** %6
%65 = load i64, i64* %7
%66 = call i32 @brstub_validate_version(i8* %64, i64 %65)
store i32 %66, i32* %10
%67 = load i32, i32* %10
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %61
br label %120
70: ; preds = %61
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
%73 = load i8*, i8** %5
%74 = load i32, i32* @O_RDWR
%75 = call i32 @open(i8* %73, i32 %74)
store i32 %75, i32* %8
%76 = load i32, i32* %8
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %70
br label %120
79: ; preds = %70
%80 = load i8*, i8** %5
%81 = load i32, i32* @O_WRONLY
%82 = call i32 @open(i8* %80, i32 %81)
store i32 %82, i32* %9
%83 = load i32, i32* %8
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %79
br label %120
86: ; preds = %79
%87 = load i32, i32* %8
%88 = load i8*, i8** %11
%89 = load i8*, i8** %11
%90 = call i32 @strlen(i8* %89)
%91 = call i32 @write(i32 %87, i8* %88, i32 %90)
store i32 %91, i32* %10
%92 = load i32, i32* %10
%93 = icmp sle i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %86
br label %120
95: ; preds = %86
%96 = load i32, i32* %9
%97 = load i8*, i8** %11
%98 = load i8*, i8** %11
%99 = call i32 @strlen(i8* %98)
%100 = call i32 @write(i32 %96, i8* %97, i32 %99)
store i32 %100, i32* %10
%101 = load i32, i32* %10
%102 = icmp sle i32 %101, 0
br i1 %102, label %103, label %104
103: ; preds = %95
br label %120
104: ; preds = %95
%105 = load i32, i32* %8
%106 = call i32 @fsync(i32 %105)
%107 = load i32, i32* %9
%108 = call i32 @fsync(i32 %107)
%109 = load i32, i32* %8
%110 = call i32 @close(i32 %109)
%111 = load i32, i32* %9
%112 = call i32 @close(i32 %111)
%113 = load i8*, i8** %6
%114 = load i64, i64* %7
%115 = call i32 @brstub_validate_version(i8* %113, i64 %114)
store i32 %115, i32* %10
%116 = load i32, i32* %10
%117 = icmp ne i32 %116, 0
br i1 %117, label %118, label %119
118: ; preds = %104
br label %120
119: ; preds = %104
store i32 0, i32* %4
br label %121
120: ; preds = %118, %103, %94, %85, %78, %69, %60, %51, %42, %33, %26, %17
store i32 -1, i32* %4
br label %121
121: ; preds = %120, %119
%122 = load i32, i32* %4
ret i32 %122
}

LLVM IR (-O2):
@.str = [8 x i8] c"string\0A\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
define i32 @brstub_write_validation(i8* nocapture %0, i8* %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %53, label %7
7: ; preds = %3
%8 = tail call i32 @close(i32 %5)
%9 = tail call i32 @brstub_validate_version(i8* %1, i64 %2)
%10 = icmp eq i32 %9, -2
br i1 %10, label %11, label %53
11: ; preds = %7
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* %0, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %53, label %15
15: ; preds = %11
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %53, label %19
19: ; preds = %15
%20 = tail call i32 @fsync(i32 %13)
%21 = tail call i32 @brstub_validate_version(i8* %1, i64 %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %53
23: ; preds = %19
%24 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%25 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %25, 1
br i1 %26, label %53, label %27
27: ; preds = %23
%28 = tail call i32 @fsync(i32 %13)
%29 = tail call i32 @brstub_validate_version(i8* %1, i64 %2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %53
31: ; preds = %27
%32 = tail call i32 @close(i32 %13)
%33 = load i32, i32* @O_RDWR
%34 = tail call i32 @open(i8* %0, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %53, label %36
36: ; preds = %31
%37 = load i32, i32* @O_WRONLY
%38 = tail call i32 @open(i8* %0, i32 %37)
%39 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%40 = tail call i32 @write(i32 %34, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = icmp slt i32 %40, 1
br i1 %41, label %53, label %42
42: ; preds = %36
%43 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%44 = tail call i32 @write(i32 %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = icmp slt i32 %44, 1
br i1 %45, label %53, label %46
46: ; preds = %42
%47 = tail call i32 @fsync(i32 %34)
%48 = tail call i32 @fsync(i32 %38)
%49 = tail call i32 @close(i32 %34)
%50 = tail call i32 @close(i32 %38)
%51 = tail call i32 @brstub_validate_version(i8* %1, i64 %2)
%52 = icmp eq i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %46, %42, %36, %31, %27, %23, %19, %15, %11, %7, %3
br label %54
54: ; preds = %46, %53
%55 = phi i32 [ -1, %53 ], [ 0, %46 ]
ret i32 %55
}