#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 O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int, int);
int snprintf(char *, int, char *, int, char *);
int /*<<< orphan*/ write(int, char *, int);
int write_lockfile(char *lockname, char *caller) {
  int fd, len;
  char buf[256];
  fd = open(lockname, O_RDWR | O_CREAT | O_EXCL, 0644);
  if (fd < 0)
    return -1;
  len = snprintf(buf, 256, "%10d %s root\n", getpid(), caller);
  write(fd, buf, len);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_lockfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a5 <write_lockfile+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <write_lockfile+0xac>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%ecx
mov    -0x18(%rbp),%r8
mov    $0x100,%esi
lea    0xe2d(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_lockfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    0x2e7c(%rip),%rcx
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%r14
mov    0x2e57(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11c7 <write_lockfile+0x77>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe63(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11cc <write_lockfile+0x7c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [14 x i8] c"%10d %s root\0A\00"
define i32 @write_lockfile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14, i32 420)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %30
19: ; preds = %2
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = call i32 (...) @getpid()
%22 = load i8*, i8** %5
%23 = call i32 @snprintf(i8* %20, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %21, i8* %22)
store i32 %23, i32* %7
%24 = load i32, i32* %6
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
store i32 0, i32* %3
br label %30
30: ; preds = %19, %18
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [14 x i8] c"%10d %s root\0A\00"
define i32 @write_lockfile(i8* nocapture %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 420)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = tail call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %1)
%15 = call i32 @write(i32 %10, i8* %4, i32 %14)
%16 = call i32 @close(i32 %10)
br label %17
17: ; preds = %2, %12
%18 = phi i32 [ 0, %12 ], [ -1, %2 ]
ret i32 %18
}