#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ unlink(char *);
int main(void) {
  char buffer[64];
  int fd = open("junk", O_CREAT | O_RDWR);
  if (fd < 0) {
    perror("open(\""
           "junk"
           "\", O_CREAT | O_RDWR);");
    return (1);
  }
  if (unlink("junk") < 0) {
    perror("unlink(\""
           "junk"
           "\");");
  }
  int result = lockf(fd, F_TLOCK, 0);
  if (result) {
    sprintf(buffer, "lockf(%d, F_TLOCK, 0);", fd);
    perror(buffer);
  }
  close(fd);
  return (result);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    11b2 <main+0x52>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    121e <main+0xbe>
lea    0xe47(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    11d4 <main+0x74>
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
mov    -0x54(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     1210 <main+0xb0>
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%edx
lea    0xe33(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    0x2e89(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11c7 <main+0x67>
mov    %eax,%ebx
lea    0xe6d(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     11da <main+0x7a>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11e8 <main+0x88>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebp
jmp    11bc <main+0x5c>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
jmp    119d <main+0x3d>
lea    0xe46(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1070 <perror@plt>
jmp    11b5 <main+0x55>

LLVM IR (-O0):
@.str = [5 x i8] c"junk\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"open(\22junk\22, O_CREAT | O_RDWR);\00"
@.str.2 = [16 x i8] c"unlink(\22junk\22);\00"
@F_TLOCK = global i32 0
@.str.3 = [23 x i8] c"lockf(%d, F_TLOCK, 0);\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %34
13: ; preds = %0
%14 = call i64 @unlink(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* %3
%20 = load i32, i32* @F_TLOCK
%21 = call i32 @lockf(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%26 = load i32, i32* %3
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%29 = call i32 @perror(i8* %28)
br label %30
30: ; preds = %24, %18
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %4
store i32 %33, i32* %1
br label %34
34: ; preds = %30, %11
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [5 x i8] c"junk\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"open(\22junk\22, O_CREAT | O_RDWR);\00"
@.str.2 = [16 x i8] c"unlink(\22junk\22);\00"
@F_TLOCK = global i32 0
@.str.3 = [23 x i8] c"lockf(%d, F_TLOCK, 0);\00"
define i32 @main() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %24
10: ; preds = %0
%11 = tail call i64 @unlink(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* @F_TLOCK
%17 = tail call i32 @lockf(i32 %6, i32 %16, i32 0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %6)
%21 = call i32 @perror(i8* %2)
br label %22
22: ; preds = %19, %15
%23 = tail call i32 @close(i32 %6)
br label %24
24: ; preds = %22, %8
%25 = phi i32 [ 1, %8 ], [ %17, %22 ]
ret i32 %25
}