#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*/ F_LOCK;
int /*<<< orphan*/ F_TLOCK;
int /*<<< orphan*/ F_ULOCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ lockf(int, int /*<<< orphan*/, int);
int open(char const *, int, int);
int /*<<< orphan*/ unlink(char const *);
int main(void) {
  static const char *filename = "/tmp/test-syncfs.tmp";
  int fd, err = 1;
  fd = open(filename, O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    return 1;
  (void)unlink(filename);
  if (lockf(fd, F_LOCK, 1024) < 0)
    goto err;
  if (lockf(fd, F_ULOCK, 1024) < 0)
    goto err;
  if (lockf(fd, F_TLOCK, 1024) < 0)
    goto err;
  if (lockf(fd, F_ULOCK, 1024) < 0)
    goto err;
  err = 0;
err:
  (void)close(fd);
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2ee3(%rip),%rdi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1192 <main+0x52>
movl   $0x1,-0x4(%rbp)
jmp    1247 <main+0x107>
mov    0x2ea7(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    $0x400,%edx
call   1040 <lockf@plt>
cmp    $0x0,%rax
jge    11c3 <main+0x83>
jmp    1239 <main+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    $0x400,%edx
call   1040 <lockf@plt>
cmp    $0x0,%rax
jge    11e8 <main+0xa8>
jmp    1239 <main+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    $0x400,%edx
call   1040 <lockf@plt>
cmp    $0x0,%rax
jge    120d <main+0xcd>
jmp    1239 <main+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    $0x400,%edx
call   1040 <lockf@plt>
cmp    $0x0,%rax
jge    1232 <main+0xf2>
jmp    1239 <main+0xf9>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e8c(%rip),%rax
mov    0x2ea5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea2(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11f2 <main+0xb2>
mov    %eax,%ebx
lea    0xe87(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x400,%edx
call   1040 <lockf@plt>
mov    $0x1,%r14d
test   %rax,%rax
js     11e9 <main+0xa9>
mov    0x2e2b(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    $0x400,%edx
call   1040 <lockf@plt>
test   %rax,%rax
js     11e9 <main+0xa9>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x400,%edx
call   1040 <lockf@plt>
test   %rax,%rax
js     11e9 <main+0xa9>
mov    (%r15),%esi
mov    %ebx,%edi
mov    $0x400,%edx
call   1040 <lockf@plt>
mov    %rax,%r14
shr    $0x3f,%r14
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11f8 <main+0xb8>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@main.filename = internal global i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i32 0, i32 0)
@.str = [21 x i8] c"/tmp/test-syncfs.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_LOCK = global i32 0
@F_ULOCK = global i32 0
@F_TLOCK = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
%4 = load i8*, i8** @main.filename
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* %4, i32 %7, i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 1, i32* %1
br label %43
12: ; preds = %0
%13 = load i8*, i8** @main.filename
%14 = call i32 @unlink(i8* %13)
%15 = load i32, i32* %2
%16 = load i32, i32* @F_LOCK
%17 = call i64 @lockf(i32 %15, i32 %16, i32 1024)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12
br label %39
20: ; preds = %12
%21 = load i32, i32* %2
%22 = load i32, i32* @F_ULOCK
%23 = call i64 @lockf(i32 %21, i32 %22, i32 1024)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
br label %39
26: ; preds = %20
%27 = load i32, i32* %2
%28 = load i32, i32* @F_TLOCK
%29 = call i64 @lockf(i32 %27, i32 %28, i32 1024)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %39
32: ; preds = %26
%33 = load i32, i32* %2
%34 = load i32, i32* @F_ULOCK
%35 = call i64 @lockf(i32 %33, i32 %34, i32 1024)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32
br label %39
38: ; preds = %32
store i32 0, i32* %3
br label %39
39: ; preds = %38, %37, %31, %25, %19
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %3
store i32 %42, i32* %1
br label %43
43: ; preds = %39, %11
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [21 x i8] c"/tmp/test-syncfs.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_LOCK = global i32 0
@F_ULOCK = global i32 0
@F_TLOCK = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = icmp slt i32 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %0
%7 = tail call i32 @unlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @F_LOCK
%9 = tail call i64 @lockf(i32 %4, i32 %8, i32 1024)
%10 = icmp slt i64 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %6
%12 = load i32, i32* @F_ULOCK
%13 = tail call i64 @lockf(i32 %4, i32 %12, i32 1024)
%14 = icmp slt i64 %13, 0
br i1 %14, label %24, label %15
15: ; preds = %11
%16 = load i32, i32* @F_TLOCK
%17 = tail call i64 @lockf(i32 %4, i32 %16, i32 1024)
%18 = icmp slt i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i32, i32* @F_ULOCK
%21 = tail call i64 @lockf(i32 %4, i32 %20, i32 1024)
%22 = lshr i64 %21, 63
%23 = trunc i64 %22 to i32
br label %24
24: ; preds = %19, %15, %11, %6
%25 = phi i32 [ 1, %6 ], [ 1, %11 ], [ 1, %15 ], [ %23, %19 ]
%26 = tail call i32 @close(i32 %4)
br label %27
27: ; preds = %0, %24
%28 = phi i32 [ %25, %24 ], [ 1, %0 ]
ret i32 %28
}