#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 snprintf(char *, size_t, char *, char const *, char const *);
int strlen(char const *);
int append_file_to_dir(char *buf, size_t len, const char *dir,
                       const char *file) {
  int n = 0;
  if (dir[strlen(dir) - 1] == '/') {
    n = snprintf(buf, len, "%s%s", dir, file);
  } else {
    n = snprintf(buf, len, "%s/%s", dir, file);
  }
  if (n == -1) {
    return -1;
  }
  return n <= (int)len ? 0 : -1;
}


x86 assembly (clang, -O0):
<append_file_to_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    118d <append_file_to_dir+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe80(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
jmp    11ac <append_file_to_dir+0x8c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe61(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c2 <append_file_to_dir+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <append_file_to_dir+0xba>
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmovle %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_file_to_dir>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rdx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
lea    0xeb6(%rip),%rax
lea    0xeb4(%rip),%rdx
cmove  %rax,%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rcx
mov    %r14,%r8
call   1040 <snprintf@plt>
cmp    $0xffffffff,%eax
sete   %cl
cmp    %r15d,%eax
setg   %al
or     %cl,%al
movzbl %al,%eax
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i32 @append_file_to_dir(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %10
%11 = load i8*, i8** %8
%12 = load i8*, i8** %8
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %11, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %26
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = load i8*, i8** %8
%24 = load i8*, i8** %9
%25 = call i32 @snprintf(i8* %21, i64 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %24)
store i32 %25, i32* %10
br label %32
26: ; preds = %4
%27 = load i8*, i8** %6
%28 = load i64, i64* %7
%29 = load i8*, i8** %8
%30 = load i8*, i8** %9
%31 = call i32 @snprintf(i8* %27, i64 %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %30)
store i32 %31, i32* %10
br label %32
32: ; preds = %26, %20
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 -1, i32* %5
br label %43
36: ; preds = %32
%37 = load i32, i32* %10
%38 = load i64, i64* %7
%39 = trunc i64 %38 to i32
%40 = icmp sle i32 %37, %39
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 0, i32 -1
store i32 %42, i32* %5
br label %43
43: ; preds = %36, %35
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i32 @append_file_to_dir(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %2)
%6 = add nsw i32 %5, -1
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %2, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
%11 = select i1 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%12 = tail call i32 @snprintf(i8* %0, i64 %1, i8* %11, i8* %2, i8* %3)
%13 = icmp eq i32 %12, -1
%14 = trunc i64 %1 to i32
%15 = icmp sgt i32 %12, %14
%16 = or i1 %13, %15
%17 = sext i1 %16 to i32
ret i32 %17
}