#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 */
size_t strlen(char *);
int zsnprintf(char *, size_t, char *, char const *);
int canon_path_append(char *base, size_t out_len, const char *suffix) {
  size_t cbase_len;
  if (!suffix[0])
    return 0;
  cbase_len = strlen(base);
  if (cbase_len < 2)
    return zsnprintf(base, out_len, "/%s", suffix);
  return zsnprintf(base + cbase_len, out_len - cbase_len, "/%s", suffix);
}


x86 assembly (clang, -O0):
<canon_path_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
jne    114d <canon_path_append+0x2d>
movl   $0x0,-0x4(%rbp)
jmp    11a8 <canon_path_append+0x88>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x2,-0x28(%rbp)
jae    1185 <canon_path_append+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0xe88(%rip),%rdx
call   1040 <zsnprintf@plt>
mov    %eax,-0x4(%rbp)
jmp    11a8 <canon_path_append+0x88>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0xe60(%rip),%rdx
call   1040 <zsnprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canon_path_append>:
push   %r15
push   %r14
push   %rbx
cmpb   $0x0,(%rdx)
je     115e <canon_path_append+0x3e>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x1,%rax
jbe    1144 <canon_path_append+0x24>
add    %rax,%rbx
sub    %rax,%r14
lea    0xeb5(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rcx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <zsnprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"/%s\00"
define i32 @canon_path_append(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 0, i32* %4
br label %33
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp ult i64 %17, 2
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = load i8*, i8** %7
%23 = call i32 @zsnprintf(i8* %20, i64 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 %23, i32* %4
br label %33
24: ; preds = %14
%25 = load i8*, i8** %5
%26 = load i64, i64* %8
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i64, i64* %6
%29 = load i64, i64* %8
%30 = sub i64 %28, %29
%31 = load i8*, i8** %7
%32 = call i32 @zsnprintf(i8* %27, i64 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %31)
store i32 %32, i32* %4
br label %33
33: ; preds = %24, %19, %13
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [4 x i8] c"/%s\00"
define i32 @canon_path_append(i8* %0, i64 %1, i8* %2) {
%4 = load i8, i8* %2
%5 = icmp eq i8 %4, 0
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = icmp ult i64 %7, 2
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @zsnprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %15
11: ; preds = %6
%12 = getelementptr inbounds i8, i8* %0, i64 %7
%13 = sub i64 %1, %7
%14 = tail call i32 @zsnprintf(i8* %12, i64 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %15
15: ; preds = %3, %11, %9
%16 = phi i32 [ %10, %9 ], [ %14, %11 ], [ 0, %3 ]
ret i32 %16
}