const char *debug_location(file, line, func) const char *file;
int line;
const char *func;
{
  static char buf[1024];
  const char *p;
  p = strrchr(file, '/');
  if (p)
    p++;
  else
    p = file;
  if (func)
    snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
  else
    snprintf(buf, sizeof(buf), "%s:%d", p, line);
  return buf;
}


x86 assembly (clang, -O0):
<debug_location>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1161 <debug_location+0x41>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1169 <debug_location+0x49>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119f <debug_location+0x7f>
mov    -0x20(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    -0x18(%rbp),%r9
lea    0x2eb9(%rip),%rdi
mov    $0x400,%esi
lea    0xe6d(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11c1 <debug_location+0xa1>
mov    -0x20(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    0x2e92(%rip),%rdi
mov    $0x400,%esi
lea    0xe51(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    0x2e78(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debug_location>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rcx
test   %rax,%rax
cmove  %rbp,%rcx
lea    0x2ef7(%rip),%rdi
test   %rbx,%rbx
je     1169 <debug_location+0x49>
lea    0xeab(%rip),%rdx
mov    $0x400,%esi
mov    %r14d,%r8d
mov    %rbx,%r9
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    117f <debug_location+0x5f>
lea    0xe9b(%rip),%rdx
mov    $0x400,%esi
mov    %r14d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0x2eba(%rip),%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@debug_location.buf = internal global [1024 x i8] zeroinitializer
@.str = [11 x i8] c"%s:%d:%s()\00"
@.str.1 = [6 x i8] c"%s:%d\00"
define i8* @debug_location(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = call i8* @strrchr(i8* %8, i32 47)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %7
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %7
br label %17
15: ; preds = %3
%16 = load i8*, i8** %4
store i8* %16, i8** %7
br label %17
17: ; preds = %15, %12
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = load i32, i32* %5
%23 = load i8*, i8** %6
%24 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @debug_location.buf, i64 0, i64 0), i64 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %22, i8* %23)
br label %29
25: ; preds = %17
%26 = load i8*, i8** %7
%27 = load i32, i32* %5
%28 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @debug_location.buf, i64 0, i64 0), i64 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
br label %29
29: ; preds = %25, %20
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @debug_location.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@debug_location.buf = internal global [1024 x i8] zeroinitializer
@.str = [11 x i8] c"%s:%d:%s()\00"
@.str.1 = [6 x i8] c"%s:%d\00"
define i8* @debug_location(i8* %0, i32 %1, i8* %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i32 47)
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 1
%7 = select i1 %5, i8* %0, i8* %6
%8 = icmp eq i8* %2, null
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @debug_location.buf, i64 0, i64 0), i64 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %1, i8* %2)
br label %13
11: ; preds = %3
%12 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @debug_location.buf, i64 0, i64 0), i64 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 %1)
br label %13
13: ; preds = %11, %9
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @debug_location.buf, i64 0, i64 0)
}