#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int getpid();
scalar_t__ link(char *, char *);
int open(char *, int, int);
char *rindex(char *, char);
int sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int lockfile_make(char *name) {
  char buf[1024], tmpfn[1024];
  char *ptr;
  int fd;
  int i;
  strcpy(tmpfn, name);
  ptr = rindex(tmpfn, '/');
  if (!ptr)
    return -1;
  ptr++;
  ptr += sprintf(ptr, "LTMP.%d", getpid());
  *ptr = 0;
  i = sprintf(buf, "%10d\n", getpid());
  unlink(tmpfn);
  fd = open(tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
  if (fd < 0) {
    unlink(tmpfn);
    return -1;
  }
  write(fd, buf, i);
  close(fd);
  if (link(tmpfn, name) < 0) {
    unlink(tmpfn);
    return -1;
  }
  unlink(tmpfn);
  return 0;
}


x86 assembly (clang, -O0):
<lockfile_make>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x810(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
jne    11e1 <lockfile_make+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <lockfile_make+0x1a8>
mov    -0x818(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x818(%rbp)
mov    -0x818(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x830(%rbp),%rdi
mov    %eax,%edx
lea    0xde8(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %eax,%ecx
mov    -0x818(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x818(%rbp)
mov    -0x818(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x828(%rbp),%rdi
mov    %eax,%edx
lea    0xda6(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %eax,-0x820(%rbp)
lea    -0x810(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x810(%rbp),%rdi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    0x2d68(%rip),%rax
or     (%rax),%esi
mov    0x2d57(%rip),%rax
or     (%rax),%esi
mov    $0x124,%edx
call   1080 <open@plt>
mov    %eax,-0x81c(%rbp)
cmpl   $0x0,-0x81c(%rbp)
jge    12d0 <lockfile_make+0x140>
lea    -0x810(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <lockfile_make+0x1a8>
mov    -0x81c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x820(%rbp),%edx
call   1050 <write@plt>
mov    -0x81c(%rbp),%edi
call   1070 <close@plt>
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <link@plt>
cmp    $0x0,%rax
jge    1325 <lockfile_make+0x195>
lea    -0x810(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <lockfile_make+0x1a8>
lea    -0x810(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockfile_make>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     128e <lockfile_make+0xfe>
mov    %rax,%rbx
mov    %rax,%rbp
add    $0x1,%rbp
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe1c(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
cltq
movb   $0x0,0x1(%rbx,%rax,1)
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe05(%rip),%rsi
lea    0x400(%rsp),%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2da1(%rip),%rax
mov    0x2dca(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x124,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1286 <lockfile_make+0xf6>
mov    %eax,%ebp
lea    0x400(%rsp),%rsi
mov    %eax,%edi
mov    %r15d,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <link@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <unlink@plt>
sar    $0x3f,%r12
jmp    128e <lockfile_make+0xfe>
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %r12d,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"LTMP.%d\00"
@.str.1 = [6 x i8] c"%10d\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @lockfile_make(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i8* @rindex(i8* %12, i8 signext 47)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i32 -1, i32* %2
br label %61
17: ; preds = %1
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i32 (...) @getpid()
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %6
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %6
%26 = load i8*, i8** %6
store i8 0, i8* %26
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = call i32 (...) @getpid()
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 %29, i32* %8
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%31 = call i32 @unlink(i8* %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %32, i32 %37, i32 292)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %17
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%43 = call i32 @unlink(i8* %42)
store i32 -1, i32* %2
br label %61
44: ; preds = %17
%45 = load i32, i32* %7
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%47 = load i32, i32* %8
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%52 = load i8*, i8** %3
%53 = call i64 @link(i8* %51, i8* %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %44
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%57 = call i32 @unlink(i8* %56)
store i32 -1, i32* %2
br label %61
58: ; preds = %44
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%60 = call i32 @unlink(i8* %59)
store i32 0, i32* %2
br label %61
61: ; preds = %58, %55, %41, %16
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
@.str = [8 x i8] c"LTMP.%d\00"
@.str.1 = [6 x i8] c"%10d\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @lockfile_make(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* %0)
%7 = call i8* @rindex(i8* %5, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %34, label %9
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = call i32 (...) @getpid()
%12 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 0, i8* %14
%15 = call i32 (...) @getpid()
%16 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @unlink(i8* %5)
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %5, i32 %22, i32 292)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %9
%26 = call i32 @unlink(i8* %5)
br label %34
27: ; preds = %9
%28 = call i32 @write(i32 %23, i8* %4, i32 %16)
%29 = call i32 @close(i32 %23)
%30 = call i64 @link(i8* %5, i8* %0)
%31 = call i32 @unlink(i8* %5)
%32 = ashr i64 %30, 63
%33 = trunc i64 %32 to i32
br label %34
34: ; preds = %27, %1, %25
%35 = phi i32 [ -1, %25 ], [ -1, %1 ], [ %33, %27 ]
ret i32 %35
}