void file_full_path(char *file_name, char *default_path, char *full_path,
                    int size) {
  char default_path_str[200];
  while (file_name && !strncmp(file_name, "./", 2))
    file_name += 2;
  assert(full_path);
  if (!file_name || !*file_name) {
    snprintf(full_path, size, "%s", "");
    return;
  }
  if (*file_name == '/') {
    if (size < strlen(file_name) + 1)
      fatal("%s: buffer too small", __FUNCTION__);
    snprintf(full_path, size, "%s", file_name);
    return;
  }
  if (!default_path || !*default_path) {
    default_path = default_path_str;
    if (!getcwd(default_path, sizeof default_path_str))
      fatal("%s: buffer too small for 'getcwd'", __FUNCTION__);
  }
  if (strlen(default_path) + strlen(file_name) + 2 > size)
    fatal("%s: buffer too small", __FUNCTION__);
  snprintf(full_path, size, "%s/%s", default_path, file_name);
}


x86 assembly (clang, -O0):
<file_full_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0xf1(%rbp)
je     11b0 <file_full_path+0x50>
mov    -0x8(%rbp),%rdi
lea    0xe68(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0xf1(%rbp)
mov    -0xf1(%rbp),%al
test   $0x1,%al
jne    11c3 <file_full_path+0x63>
jmp    11d4 <file_full_path+0x74>
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    117a <file_full_path+0x1a>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1070 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
je     11f7 <file_full_path+0x97>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1219 <file_full_path+0xb9>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
lea    0xe46(%rip),%rdx
lea    0xe3b(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1348 <file_full_path+0x1e8>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1288 <file_full_path+0x128>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x100(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x100(%rbp),%rax
add    $0x1,%rcx
cmp    %rcx,%rax
jae    1269 <file_full_path+0x109>
lea    0xda8(%rip),%rdi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xdd0(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1348 <file_full_path+0x1e8>
cmpq   $0x0,-0x10(%rbp)
je     12a0 <file_full_path+0x140>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12de <file_full_path+0x17e>
lea    -0xf0(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xc8,%esi
mov    $0x0,%al
call   1060 <getcwd@plt>
cmp    $0x0,%eax
jne    12d9 <file_full_path+0x179>
lea    0xd5c(%rip),%rdi
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal@plt>
jmp    12de <file_full_path+0x17e>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x108(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x108(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jbe    132a <file_full_path+0x1ca>
lea    0xce7(%rip),%rdi
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xd08(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_full_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xd0,%rsp
mov    %ecx,%r12d
mov    %rdx,%r14
test   %rdi,%rdi
je     11f7 <file_full_path+0x97>
mov    %rsi,%r15
mov    %rdi,%rbx
add    $0xfffffffffffffffe,%rbx
lea    0xe75(%rip),%rbp
nopl   0x0(%rax,%rax,1)
add    $0x2,%rbx
mov    $0x2,%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strncmp@plt>
test   %eax,%eax
je     1190 <file_full_path+0x30>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <assert@plt>
mov    (%rbx),%al
test   %al,%al
je     1201 <file_full_path+0xa1>
cmp    $0x2f,%al
jne    122b <file_full_path+0xcb>
movslq %r12d,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
cmp    %rbp,%rax
jbe    11e5 <file_full_path+0x85>
lea    0xe2c(%rip),%rdi
lea    0xe3a(%rip),%rsi
xor    %eax,%eax
call   1080 <fatal@plt>
lea    0xe60(%rip),%rdx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rcx
jmp    1215 <file_full_path+0xb5>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <assert@plt>
movslq %r12d,%rsi
lea    0xe41(%rip),%rdx
lea    0xe36(%rip),%rcx
mov    %r14,%rdi
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <snprintf@plt>
test   %r15,%r15
je     1236 <file_full_path+0xd6>
cmpb   $0x0,(%r15)
jne    1261 <file_full_path+0x101>
mov    %rsp,%r15
mov    $0xc8,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <getcwd@plt>
test   %eax,%eax
jne    1261 <file_full_path+0x101>
lea    0xdd4(%rip),%rdi
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
call   1080 <fatal@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rbp,%rax
add    $0x2,%rax
movslq %r12d,%rbp
cmp    %rbp,%rax
jbe    1298 <file_full_path+0x138>
lea    0xd79(%rip),%rdi
lea    0xd87(%rip),%rsi
xor    %eax,%eax
call   1080 <fatal@plt>
lea    0xdaa(%rip),%rdx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
add    $0xd0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [21 x i8] c"%s: buffer too small\00"
@__FUNCTION__.file_full_path = [15 x i8] c"file_full_path\00"
@.str.4 = [34 x i8] c"%s: buffer too small for 'getcwd'\00"
@.str.5 = [6 x i8] c"%s/%s\00"
define void @file_full_path(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [200 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
br label %10
10: ; preds = %20, %4
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i8*, i8** %5
%15 = call i32 @strncmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 2)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
br label %18
18: ; preds = %13, %10
%19 = phi i1 [ false, %10 ], [ %17, %13 ]
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 2
store i8* %22, i8** %5
br label %10
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %24)
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28, %23
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %33, i64 %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %91
37: ; preds = %28
%38 = load i8*, i8** %5
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 47
br i1 %41, label %42, label %57
42: ; preds = %37
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = load i8*, i8** %5
%46 = call i64 @strlen(i8* %45)
%47 = add i64 %46, 1
%48 = icmp ult i64 %44, %47
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.file_full_path, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
%52 = load i8*, i8** %7
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = load i8*, i8** %5
%56 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %52, i64 %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %55)
br label %91
57: ; preds = %37
%58 = load i8*, i8** %6
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = load i8*, i8** %6
%62 = load i8, i8* %61
%63 = icmp ne i8 %62, 0
br i1 %63, label %72, label %64
64: ; preds = %60, %57
%65 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
store i8* %65, i8** %6
%66 = load i8*, i8** %6
%67 = call i32 (i8*, i64, ...) bitcast (i32 (...)* @getcwd to i32 (i8*, i64, ...)*)(i8* %66, i64 200)
%68 = icmp ne i32 %67, 0
br i1 %68, label %71, label %69
69: ; preds = %64
%70 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.file_full_path, i64 0, i64 0))
br label %71
71: ; preds = %69, %64
br label %72
72: ; preds = %71, %60
%73 = load i8*, i8** %6
%74 = call i64 @strlen(i8* %73)
%75 = load i8*, i8** %5
%76 = call i64 @strlen(i8* %75)
%77 = add i64 %74, %76
%78 = add i64 %77, 2
%79 = load i32, i32* %8
%80 = sext i32 %79 to i64
%81 = icmp ugt i64 %78, %80
br i1 %81, label %82, label %84
82: ; preds = %72
%83 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.file_full_path, i64 0, i64 0))
br label %84
84: ; preds = %82, %72
%85 = load i8*, i8** %7
%86 = load i32, i32* %8
%87 = sext i32 %86 to i64
%88 = load i8*, i8** %6
%89 = load i8*, i8** %5
%90 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %85, i64 %87, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %88, i8* %89)
br label %91
91: ; preds = %84, %51, %32
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [21 x i8] c"%s: buffer too small\00"
@__FUNCTION__.file_full_path = [15 x i8] c"file_full_path\00"
@.str.4 = [34 x i8] c"%s: buffer too small for 'getcwd'\00"
@.str.5 = [6 x i8] c"%s/%s\00"
define void @file_full_path(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [200 x i8]
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%7 = icmp eq i8* %0, null
br i1 %7, label %16, label %8
8: ; preds = %4, %8
%9 = phi i8* [ %12, %8 ], [ %0, %4 ]
%10 = tail call i32 @strncmp(i8* %9, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 2)
%11 = icmp eq i32 %10, 0
%12 = getelementptr inbounds i8, i8* %9, i64 2
br i1 %11, label %8, label %13
13: ; preds = %8
%14 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %2)
%15 = load i8, i8* %9
switch i8 %15, label %30 [
i8 0, label %18
i8 47, label %21
]
16: ; preds = %4
%17 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %2)
br label %18
18: ; preds = %13, %16
%19 = sext i32 %3 to i64
%20 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %52
21: ; preds = %13
%22 = sext i32 %3 to i64
%23 = tail call i64 @strlen(i8* %9)
%24 = add i64 %23, 1
%25 = icmp ugt i64 %24, %22
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.file_full_path, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %52
30: ; preds = %13
%31 = icmp eq i8* %1, null
br i1 %31, label %35, label %32
32: ; preds = %30
%33 = load i8, i8* %1
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32, %30
%36 = call i32 (i8*, i64, ...) bitcast (i32 (...)* @getcwd to i32 (i8*, i64, ...)*)(i8* %6, i64 200)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.file_full_path, i64 0, i64 0))
br label %40
40: ; preds = %35, %38, %32
%41 = phi i8* [ %1, %32 ], [ %6, %35 ], [ %6, %38 ]
%42 = call i64 @strlen(i8* dereferenceable(1) %41)
%43 = call i64 @strlen(i8* %9)
%44 = add i64 %42, 2
%45 = add i64 %44, %43
%46 = sext i32 %3 to i64
%47 = icmp ugt i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__FUNCTION__.file_full_path, i64 0, i64 0))
br label %50
50: ; preds = %48, %40
%51 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %41, i8* %9)
br label %52
52: ; preds = %50, %28, %18
ret void
}