#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;
int NAME_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int snprintf(char *, size_t, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int rewrite(const char *snapname, const char *name, char *rewritten,
            size_t rmax) {
  if (strlen(snapname) + strlen(name) > NAME_MAX - 10) {
    errno = ENAMETOOLONG;
    return -1;
  }
  const char *tmp = name;
  if (tmp[0] == '/') {
    tmp = &name[1];
  }
  int n = snprintf(rewritten, rmax, "snap.%s.%s", snapname, tmp);
  if (n < 0 || n >= rmax) {
    fprintf(stderr, "snprintf truncated\n");
    return -1;
  }
  rewritten[rmax - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<rewrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%ecx
sub    $0xa,%ecx
cmp    %ecx,%eax
jle    1196 <rewrite+0x66>
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    0x2e50(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <rewrite+0xf9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11ba <rewrite+0x8a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xe2f(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     11f1 <rewrite+0xc1>
movslq -0x34(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     1212 <rewrite+0xe2>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <rewrite+0xf9>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %ebp,%eax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%ecx
add    $0xfffffff6,%ecx
cmp    %ecx,%eax
jle    1179 <rewrite+0x49>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    0x2e63(%rip),%rcx
mov    %eax,(%rcx)
jmp    11c5 <rewrite+0x95>
xor    %r8d,%r8d
cmpb   $0x2f,(%rbx)
sete   %r8b
add    %rbx,%r8
lea    0xe73(%rip),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rcx
call   1040 <snprintf@plt>
test   %eax,%eax
js     11b0 <rewrite+0x80>
cltq
cmp    %r15,%rax
jae    11b0 <rewrite+0x80>
movb   $0x0,-0x1(%r15,%r14,1)
xor    %eax,%eax
jmp    11ca <rewrite+0x9a>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NAME_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"snap.%s.%s\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"snprintf truncated\0A\00"
define i32 @rewrite(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = load i8*, i8** %6
%13 = call i32 @strlen(i8* %12)
%14 = load i8*, i8** %7
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %13, %15
%17 = load i32, i32* @NAME_MAX
%18 = sub nsw i32 %17, 10
%19 = icmp sgt i32 %16, %18
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* @ENAMETOOLONG
store i32 %21, i32* @errno
store i32 -1, i32* %5
br label %53
22: ; preds = %4
%23 = load i8*, i8** %7
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 47
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8* %31, i8** %10
br label %32
32: ; preds = %29, %22
%33 = load i8*, i8** %8
%34 = load i64, i64* %9
%35 = load i8*, i8** %6
%36 = load i8*, i8** %10
%37 = call i32 @snprintf(i8* %33, i64 %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %36)
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp slt i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %32
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = load i64, i64* %9
%44 = icmp uge i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %40, %32
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %53
48: ; preds = %40
%49 = load i8*, i8** %8
%50 = load i64, i64* %9
%51 = sub i64 %50, 1
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
store i32 0, i32* %5
br label %53
53: ; preds = %48, %45, %20
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"snap.%s.%s\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"snprintf truncated\0A\00"
define i32 @rewrite(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %6, %5
%8 = load i32, i32* @NAME_MAX
%9 = add nsw i32 %8, -10
%10 = icmp sgt i32 %7, %9
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = load i32, i32* @ENAMETOOLONG
store i32 %12, i32* @errno
br label %29
13: ; preds = %4
%14 = load i8, i8* %1
%15 = icmp eq i8 %14, 47
%16 = zext i1 %15 to i64
%17 = getelementptr i8, i8* %1, i64 %16
%18 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %17)
%19 = icmp sgt i32 %18, -1
%20 = sext i32 %18 to i64
%21 = icmp ult i64 %20, %3
%22 = and i1 %19, %21
br i1 %22, label %26, label %23
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %29
26: ; preds = %13
%27 = add i64 %3, -1
%28 = getelementptr inbounds i8, i8* %2, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %23, %26, %11
%30 = phi i32 [ -1, %11 ], [ -1, %23 ], [ 0, %26 ]
ret i32 %30
}