#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_TLOCK;
int /*<<< orphan*/ F_ULOCK;
int /*<<< orphan*/ O_RDWR;
int close(int);
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  int ret = 0;
  int fd;
  if (argc != 2) {
    printf("one arg needed for lock\n");
    return -1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("open failed");
    return -1;
  }
  ret = lseek(fd, 0, 0);
  if (ret == -1) {
    perror("lseek failed");
    return -1;
  }
  ret = lockf(fd, F_TLOCK, 0);
  if (ret == -1) {
    perror("lockf failed");
    return -1;
  }
  ret = lockf(fd, F_ULOCK, 0);
  if (ret == -1) {
    perror("ulockf failed");
    return -1;
  }
  ret = close(fd);
  if (ret == -1) {
    perror("close failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119f <main+0x3f>
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11da <main+0x7a>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x14f>
mov    -0x18(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1040 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120b <main+0xab>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x14f>
mov    -0x18(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1243 <main+0xe3>
lea    0xe01(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x14f>
mov    -0x18(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    127b <main+0x11b>
lea    0xdc8(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x14f>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12a8 <main+0x148>
lea    0xda9(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x14f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11d6 <main+0x76>
mov    0x8(%rsi),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e4 <main+0x84>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0x8d>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0x96>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0x9f>
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0xb0>
lea    0xe53(%rip),%rdi
jmp    1206 <main+0xa6>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
jmp    120b <main+0xab>
lea    0xe15(%rip),%rdi
jmp    1206 <main+0xa6>
lea    0xe18(%rip),%rdi
jmp    1206 <main+0xa6>
lea    0xe1d(%rip),%rdi
jmp    1206 <main+0xa6>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"one arg needed for lock\0A\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@.str.2 = [13 x i8] c"lseek failed\00"
@F_TLOCK = global i32 0
@.str.3 = [13 x i8] c"lockf failed\00"
@F_ULOCK = global i32 0
@.str.4 = [14 x i8] c"ulockf failed\00"
@.str.5 = [13 x i8] c"close failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
22: ; preds = %12
%23 = load i32, i32* %7
%24 = call i32 @lseek(i32 %23, i32 0, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
29: ; preds = %22
%30 = load i32, i32* %7
%31 = load i32, i32* @F_TLOCK
%32 = call i32 @lockf(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
37: ; preds = %29
%38 = load i32, i32* %7
%39 = load i32, i32* @F_ULOCK
%40 = call i32 @lockf(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
45: ; preds = %37
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
52: ; preds = %45
store i32 0, i32* %3
br label %53
53: ; preds = %52, %50, %43, %35, %27, %20, %10
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@.str.2 = [13 x i8] c"lseek failed\00"
@F_TLOCK = global i32 0
@.str.3 = [13 x i8] c"lockf failed\00"
@F_ULOCK = global i32 0
@.str.4 = [14 x i8] c"ulockf failed\00"
@.str.5 = [13 x i8] c"close failed\00"
@str = [24 x i8] c"one arg needed for lock\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %36
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %36
14: ; preds = %6
%15 = tail call i32 @lseek(i32 %10, i32 0, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %36
19: ; preds = %14
%20 = load i32, i32* @F_TLOCK
%21 = tail call i32 @lockf(i32 %10, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %36
25: ; preds = %19
%26 = load i32, i32* @F_ULOCK
%27 = tail call i32 @lockf(i32 %10, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %36
31: ; preds = %25
%32 = tail call i32 @close(i32 %10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %31, %34, %29, %23, %17, %12, %4
%37 = phi i32 [ -1, %4 ], [ -1, %12 ], [ -1, %17 ], [ -1, %23 ], [ -1, %29 ], [ -1, %34 ], [ 0, %31 ]
ret i32 %37
}