#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 flock {
  int l_start;
  int l_len;
  void *l_type;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
void *F_UNLCK;
void *F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd = -1;
  char *filename = NULL;
  struct flock lock = {
      0,
  };
  int i = 0;
  int ret = -1;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename> ", argv[0]);
    goto out;
  }
  filename = argv[1];
  fd = open(filename, O_RDWR | O_CREAT, 0);
  if (fd < 0) {
    fprintf(stderr, "open (%s) failed (%s)\n", filename, strerror(errno));
    goto out;
  }
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 1;
  lock.l_len = 1;
  while (i < 100) {
    lock.l_type = F_WRLCK;
    ret = fcntl(fd, F_SETLK, &lock);
    if (ret < 0) {
      fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
      goto out;
    }
    lock.l_type = F_UNLCK;
    ret = fcntl(fd, F_SETLK, &lock);
    if (ret < 0) {
      fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
      goto out;
    }
    i++;
  }
  ret = 0;
out:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
movl   $0x0,-0x3c(%rbp)
movl   $0xffffffff,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c0 <main+0x70>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1342 <main+0x1f2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1235 <main+0xe5>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1342 <main+0x1f2>
mov    0x2d6c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0x64,-0x3c(%rbp)
jge    133b <main+0x1eb>
mov    0x2d3a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12ca <main+0x17a>
mov    0x2d22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1342 <main+0x1f2>
mov    0x2cff(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    132d <main+0x1dd>
mov    0x2cbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1342 <main+0x1f2>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    125d <main+0x10d>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
cmp    $0x2,%edi
jne    1254 <main+0x114>
mov    0x8(%rsi),%rbx
mov    0x2e75(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1275 <main+0x135>
mov    %eax,%r13d
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x10(%rsp)
movabs $0x100000001,%rcx
mov    %rcx,(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2e1c(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %r13d,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     1226 <main+0xe6>
mov    $0x64,%ebp
mov    0x2df5(%rip),%r15
mov    %rsp,%r14
xchg   %ax,%ax
mov    (%r15),%rax
mov    %rax,0x8(%rsp)
mov    (%r12),%esi
mov    %r13d,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1226 <main+0xe6>
add    $0xffffffff,%ebp
je     12a9 <main+0x169>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    (%r12),%esi
mov    %r13d,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11e0 <main+0xa0>
mov    %eax,%ebx
mov    0x2d91(%rip),%rax
mov    (%rax),%ebp
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xde7(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    12ab <main+0x16b>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd99(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    12ab <main+0x16b>
mov    0x2d44(%rip),%rax
mov    (%rax),%ebp
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xd83(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    12ab <main+0x16b>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i8*, i32, i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename> \00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [23 x i8] c"open (%s) failed (%s)\0A\00"
@errno = global i32 0
@F_WRLCK = global i8* null
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [25 x i8] c"fcntl setlk failed (%s)\0A\00"
@F_UNLCK = global i8* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.flock
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i8* null, i8** %7
%11 = bitcast %struct.flock* %8 to i8*
store i32 0, i32* %9
store i32 -1, i32* %10
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %77
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27, i32 0)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %7
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %35)
br label %77
37: ; preds = %20
%38 = load i8*, i8** @F_WRLCK
%39 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i8* %38, i8** %39
%40 = load i32, i32* @SEEK_SET
%41 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 1, i32* %42
%43 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 1, i32* %43
br label %44
44: ; preds = %73, %37
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 100
br i1 %46, label %47, label %76
47: ; preds = %44
%48 = load i8*, i8** @F_WRLCK
%49 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i8* %48, i8** %49
%50 = load i32, i32* %6
%51 = load i32, i32* @F_SETLK
%52 = call i32 @fcntl(i32 %50, i32 %51, %struct.flock* %8)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %47
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %58)
br label %77
60: ; preds = %47
%61 = load i8*, i8** @F_UNLCK
%62 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i8* %61, i8** %62
%63 = load i32, i32* %6
%64 = load i32, i32* @F_SETLK
%65 = call i32 @fcntl(i32 %63, i32 %64, %struct.flock* %8)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %60
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %71)
br label %77
73: ; preds = %60
%74 = load i32, i32* %9
%75 = add nsw i32 %74, 1
store i32 %75, i32* %9
br label %44
76: ; preds = %44
store i32 0, i32* %10
br label %77
77: ; preds = %76, %68, %55, %31, %14
%78 = load i32, i32* %10
ret i32 %78
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i8*, i32, i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename> \00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [23 x i8] c"open (%s) failed (%s)\0A\00"
@errno = global i32 0
@F_WRLCK = global i8* null
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [25 x i8] c"fcntl setlk failed (%s)\0A\00"
@F_UNLCK = global i8* null
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%6 = bitcast i32* %5 to i64*
store i64 0, i64* %6
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %60
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* %14, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %23)
br label %60
25: ; preds = %12
%26 = load i8*, i8** @F_WRLCK
%27 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%28 = load i32, i32* @SEEK_SET
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 1, i32* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 1, i32* %31
store i8* %26, i8** %27
%32 = load i32, i32* @F_SETLK
%33 = call i32 @fcntl(i32 %18, i32 %32, %struct.flock* %3)
%34 = icmp slt i32 %33, 0
br i1 %34, label %43, label %49
35: ; preds = %49
%36 = add nuw nsw i32 %50, 1
%37 = icmp eq i32 %36, 100
br i1 %37, label %60, label %38
38: ; preds = %35
%39 = load i8*, i8** @F_WRLCK
store i8* %39, i8** %27
%40 = load i32, i32* @F_SETLK
%41 = call i32 @fcntl(i32 %18, i32 %40, %struct.flock* %3)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %38, %25
%44 = phi i32 [ %33, %25 ], [ %41, %38 ]
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %60
49: ; preds = %25, %38
%50 = phi i32 [ %36, %38 ], [ 0, %25 ]
%51 = load i8*, i8** @F_UNLCK
store i8* %51, i8** %27
%52 = load i32, i32* @F_SETLK
%53 = call i32 @fcntl(i32 %18, i32 %52, %struct.flock* %3)
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %35
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %35, %55, %43, %20, %8
%61 = phi i32 [ -1, %8 ], [ -1, %20 ], [ %44, %43 ], [ %53, %55 ], [ 0, %35 ]
ret i32 %61
}