#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 /*<<< orphan*/ ENAMETOOLONG;
size_t PATH_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char *);
char *append_path_component(char *path, const char *component,
                            size_t component_length) {
  size_t path_length, slash_length;
  path_length = strlen(path);
  slash_length = ((path[path_length - 1] == '/') ? 0 : 1) +
                 ((component[0] == '/') ? 0 : 1) - 1;
  if (path_length + slash_length + component_length >= PATH_MAX) {
    errno = ENAMETOOLONG;
    return NULL;
  }
  if (slash_length > 0)
    path[path_length] = '/';
  memcpy(path + path_length + slash_length, component, component_length);
  path[path_length + slash_length + component_length] = 0;
  return path;
}


x86 assembly (clang, -O0):
<append_path_component>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x2f,%edx
cmove  %ecx,%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%esi
mov    $0x1,%ecx
xor    %edx,%edx
cmp    $0x2f,%esi
cmove  %edx,%ecx
add    %ecx,%eax
sub    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
add    -0x20(%rbp),%rax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
jb     11b8 <append_path_component+0x98>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1204 <append_path_component+0xe4>
cmpq   $0x0,-0x30(%rbp)
jbe    11cf <append_path_component+0xaf>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    -0x30(%rbp),%rcx
add    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_path_component>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
xor    %edx,%edx
cmpb   $0x2f,-0x1(%rax,%r12,1)
sete   %dl
xor    %ecx,%ecx
cmpb   $0x2f,(%r15)
setne  %cl
sub    %rdx,%rcx
lea    (%rax,%r14,1),%rbx
add    %rcx,%rbx
mov    0x2e95(%rip),%rdx
cmp    (%rdx),%rbx
jae    1185 <append_path_component+0x65>
test   %rcx,%rcx
je     116a <append_path_component+0x4a>
movb   $0x2f,(%r12,%rax,1)
add    %r12,%rax
add    %rcx,%rax
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
jmp    119a <append_path_component+0x7a>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
mov    %eax,(%rcx)
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i8* @append_path_component(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i8*, i8** %5
%13 = load i64, i64* %8
%14 = sub i64 %13, 1
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 47
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 0, i32 1
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 0, i32 1
%28 = add nsw i32 %20, %27
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
store i64 %30, i64* %9
%31 = load i64, i64* %8
%32 = load i64, i64* %9
%33 = add i64 %31, %32
%34 = load i64, i64* %7
%35 = add i64 %33, %34
%36 = load i64, i64* @PATH_MAX
%37 = icmp uge i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %3
%39 = load i32, i32* @ENAMETOOLONG
store i32 %39, i32* @errno
store i8* null, i8** %4
br label %64
40: ; preds = %3
%41 = load i64, i64* %9
%42 = icmp ugt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i8*, i8** %5
%45 = load i64, i64* %8
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8 47, i8* %46
br label %47
47: ; preds = %43, %40
%48 = load i8*, i8** %5
%49 = load i64, i64* %8
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = load i64, i64* %9
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = load i8*, i8** %6
%54 = load i64, i64* %7
%55 = call i32 @memcpy(i8* %52, i8* %53, i64 %54)
%56 = load i8*, i8** %5
%57 = load i64, i64* %8
%58 = load i64, i64* %9
%59 = add i64 %57, %58
%60 = load i64, i64* %7
%61 = add i64 %59, %60
%62 = getelementptr inbounds i8, i8* %56, i64 %61
store i8 0, i8* %62
%63 = load i8*, i8** %5
store i8* %63, i8** %4
br label %64
64: ; preds = %47, %38
%65 = load i8*, i8** %4
ret i8* %65
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i8* @append_path_component(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %4, -1
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 47
%9 = load i8, i8* %1
%10 = icmp ne i8 %9, 47
%11 = zext i1 %10 to i64
%12 = sext i1 %8 to i64
%13 = add nsw i64 %12, %11
%14 = add i64 %4, %2
%15 = add i64 %14, %13
%16 = load i64, i64* @PATH_MAX
%17 = icmp ult i64 %15, %16
br i1 %17, label %20, label %18
18: ; preds = %3
%19 = load i32, i32* @ENAMETOOLONG
store i32 %19, i32* @errno
br label %29
20: ; preds = %3
%21 = icmp eq i64 %13, 0
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = getelementptr inbounds i8, i8* %0, i64 %4
store i8 47, i8* %23
br label %24
24: ; preds = %22, %20
%25 = getelementptr inbounds i8, i8* %0, i64 %4
%26 = getelementptr inbounds i8, i8* %25, i64 %13
%27 = tail call i32 @memcpy(i8* %26, i8* %1, i64 %2)
%28 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %28
br label %29
29: ; preds = %24, %18
%30 = phi i8* [ null, %18 ], [ %0, %24 ]
ret i8* %30
}