#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 PATH_MAX;
int /*<<< orphan*/ *realpath(char const *, char *);
int snprintf(char *, unsigned int, char *, char const *);
char *strrchr(char const *, char);
int lock_build(const char *devname, char *buf, unsigned length) {
  const char *basename;
  char resolved_path[PATH_MAX];
  if (realpath(devname, resolved_path) != NULL)
    devname = resolved_path;
  basename = strrchr(devname, '/');
  if (basename)
    basename++;
  else
    basename = devname;
  return snprintf(buf, length, "/var/lock/LCK..%s", basename);
}


x86 assembly (clang, -O0):
<lock_build>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <realpath@plt>
cmp    $0x0,%rax
je     118a <lock_build+0x5a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b8 <lock_build+0x88>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c0 <lock_build+0x90>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
lea    0xe2e(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_build>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1050 <realpath@plt>
test   %rax,%rax
cmove  %r12,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rcx
test   %rax,%rax
cmove  %rbx,%rcx
lea    0xe74(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <snprintf@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [18 x i8] c"/var/lock/LCK..%s\00"
define i32 @lock_build(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %8
%13 = alloca i8, i64 %11
store i64 %11, i64* %9
%14 = load i8*, i8** %4
%15 = call i32* @realpath(i8* %14, i8* %13)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %3
store i8* %13, i8** %4
br label %18
18: ; preds = %17, %3
%19 = load i8*, i8** %4
%20 = call i8* @strrchr(i8* %19, i8 signext 47)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %7
br label %28
26: ; preds = %18
%27 = load i8*, i8** %4
store i8* %27, i8** %7
br label %28
28: ; preds = %26, %23
%29 = load i8*, i8** %5
%30 = load i32, i32* %6
%31 = load i8*, i8** %7
%32 = call i32 @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %8
ret i32 %32
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [18 x i8] c"/var/lock/LCK..%s\00"
define i32 @lock_build(i8* %0, i8* %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32* @realpath(i8* %0, i8* %6)
%8 = icmp eq i32* %7, null
%9 = select i1 %8, i8* %0, i8* %6
%10 = call i8* @strrchr(i8* dereferenceable(1) %9, i8 signext 47)
%11 = icmp eq i8* %10, null
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = select i1 %11, i8* %9, i8* %12
%14 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
ret i32 %14
}