#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*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
char *strrchr(char const *, char);
char *swap_file(const char *path, const char *name) {
  int plen, nlen;
  char *p = strrchr(path, '/');
  if (p != NULL)
    plen = strlen(path) - strlen(p);
  else
    plen = 0;
  nlen = strlen(name);
  char *new_path = malloc(plen + 2 + nlen);
  if (new_path != NULL) {
    if (plen > 0) {
      memcpy(new_path, path, plen);
      memcpy(&new_path[plen], "/", 1);
      plen++;
    }
    memcpy(&new_path[plen], name, nlen);
    new_path[nlen + plen] = 0;
  } else
    fprintf(stderr, "swap_file: allocation failed\n");
  return new_path;
}


x86 assembly (clang, -O0):
<swap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a1 <swap_file+0x51>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11a8 <swap_file+0x58>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    $0x2,%edi
add    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     123d <swap_file+0xed>
cmpl   $0x0,-0x14(%rbp)
jle    1210 <swap_file+0xc0>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
lea    0xe03(%rip),%rsi
mov    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1252 <swap_file+0x102>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1189 <swap_file+0x39>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %eax,%ebp
jmp    118b <swap_file+0x3b>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
lea    0x2(%rbp,%r13,1),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11f3 <swap_file+0xa3>
test   %ebp,%ebp
jle    11d2 <swap_file+0x82>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %ebp,%edi
add    %r15,%rdi
lea    0xe3b(%rip),%rsi
mov    $0x1,%edx
call   1060 <memcpy@plt>
add    $0x1,%ebp
movslq %ebp,%rbx
mov    %r15,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
add    %r13d,%ebx
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
jmp    1208 <swap_file+0xb8>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"swap_file: allocation failed\0A\00"
define i8* @swap_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i8* @strrchr(i8* %9, i8 signext 47)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = load i8*, i8** %7
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %15, %17
store i32 %18, i32* %5
br label %20
19: ; preds = %2
store i32 0, i32* %5
br label %20
20: ; preds = %19, %13
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 2
%25 = load i32, i32* %6
%26 = add nsw i32 %24, %25
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %59
30: ; preds = %20
%31 = load i32, i32* %5
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = load i8*, i8** %8
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = call i32 @memcpy(i8* %34, i8* %35, i32 %36)
%38 = load i8*, i8** %8
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i32 @memcpy(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %45
45: ; preds = %33, %30
%46 = load i8*, i8** %8
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i8*, i8** %4
%51 = load i32, i32* %6
%52 = call i32 @memcpy(i8* %49, i8* %50, i32 %51)
%53 = load i8*, i8** %8
%54 = load i32, i32* %6
%55 = load i32, i32* %5
%56 = add nsw i32 %54, %55
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %53, i64 %57
store i8 0, i8* %58
br label %62
59: ; preds = %20
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %62
62: ; preds = %59, %45
%63 = load i8*, i8** %8
ret i8* %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"swap_file: allocation failed\0A\00"
define i8* @swap_file(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i32 @strlen(i8* %3)
%8 = sub nsw i32 %6, %7
br label %9
9: ; preds = %2, %5
%10 = phi i32 [ %8, %5 ], [ 0, %2 ]
%11 = tail call i32 @strlen(i8* %1)
%12 = add nsw i32 %10, 2
%13 = add nsw i32 %12, %11
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %32, label %16
16: ; preds = %9
%17 = icmp sgt i32 %10, 0
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = tail call i32 @memcpy(i8* %14, i8* %0, i32 %10)
%20 = zext i32 %10 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = tail call i32 @memcpy(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%23 = add nuw nsw i32 %10, 1
br label %24
24: ; preds = %18, %16
%25 = phi i32 [ %23, %18 ], [ %10, %16 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %14, i64 %26
%28 = tail call i32 @memcpy(i8* %27, i8* %1, i32 %11)
%29 = add nsw i32 %25, %11
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %14, i64 %30
store i8 0, i8* %31
br label %35
32: ; preds = %9
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %32, %24
ret i8* %14
}