int onelock(pid, tempfile, name)
char *tempfile, *name;
{
  int fd;
  fd = creat(tempfile, 0444);
  if (fd < 0)
    return (-1);
  write(fd, (char *)&pid, sizeof(int));
  close(fd);
  if (link(tempfile, name) < 0) {
    unlink(tempfile);
    return (-1);
  }
  unlink(tempfile);
  return (0);
}


x86 assembly (clang, -O0):
<onelock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x124,%esi
mov    $0x0,%al
call   1060 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118c <onelock+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <onelock+0x9a>
mov    -0x1c(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
mov    $0x0,%al
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1070 <link@plt>
cmp    $0x0,%eax
jge    11d8 <onelock+0x88>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <onelock+0x9a>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onelock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,0xc(%rsp)
mov    %rsi,%rdi
mov    $0x124,%esi
xor    %eax,%eax
call   1060 <creat@plt>
test   %eax,%eax
js     11b1 <onelock+0x61>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
mov    %eax,%edi
xor    %eax,%eax
call   1040 <write@plt>
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <close@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <link@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <unlink@plt>
sar    $0x1f,%ebp
jmp    11b6 <onelock+0x66>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @onelock(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %6
%10 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @creat to i32 (i8*, i32, ...)*)(i8* %9, i32 292)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %30
14: ; preds = %3
%15 = load i32, i32* %8
%16 = bitcast i32* %5 to i8*
%17 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 %15, i8* %16, i64 4)
%18 = load i32, i32* %8
%19 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %18)
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @link to i32 (i8*, i8*, ...)*)(i8* %20, i8* %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) bitcast (i32 (...)* @unlink to i32 (i8*, ...)*)(i8* %25)
store i32 -1, i32* %4
br label %30
27: ; preds = %14
%28 = load i8*, i8** %6
%29 = call i32 (i8*, ...) bitcast (i32 (...)* @unlink to i32 (i8*, ...)*)(i8* %28)
store i32 0, i32* %4
br label %30
30: ; preds = %27, %24, %13
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @onelock(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
store i32 %0, i32* %4
%5 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @creat to i32 (i8*, i32, ...)*)(i8* %1, i32 292)
%6 = icmp slt i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %3
%8 = bitcast i32* %4 to i8*
%9 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 %5, i8* %8, i64 4)
%10 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %5)
%11 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @link to i32 (i8*, i8*, ...)*)(i8* %1, i8* %2)
%12 = call i32 (i8*, ...) bitcast (i32 (...)* @unlink to i32 (i8*, ...)*)(i8* %1)
%13 = ashr i32 %11, 31
br label %14
14: ; preds = %7, %3
%15 = phi i32 [ -1, %3 ], [ %13, %7 ]
ret i32 %15
}