#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 */
struct stack {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t const);
int /*<<< orphan*/ stack_empty(struct stack **);
void *stack_move(struct stack **, struct stack **);
size_t strlen(char const *);
const char *file_path_from_stack(struct stack **path, const char *fname) {
  assert(fname != NULL);
  const size_t fnamelen = strlen(fname);
  struct stack *tmp = NULL;
  size_t plen = 0;
  while (!stack_empty(path)) {
    const void *frag = stack_move(path, &tmp);
    assert(frag != NULL);
    plen += strlen((const char *)frag) + 1;
  }
  char *full = malloc(plen + fnamelen + 1);
  if (full == NULL) {
    while (!stack_empty(&tmp))
      stack_move(&tmp, path);
    return NULL;
  }
  char *p = full;
  while (!stack_empty(&tmp)) {
    const void *frag = stack_move(&tmp, path);
    const size_t flen = strlen((const char *)frag);
    memcpy(p, frag, flen);
    p[flen] = '/';
    p += flen + 1;
  }
  assert((size_t)(p - full) == plen);
  if (fnamelen > 0) {
    memcpy(p, fname, fnamelen);
    p[fnamelen] = '\0';
  } else {
    full[plen - 1] = '\0';
  }
  return full;
}


x86 assembly (clang, -O0):
<file_path_from_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <stack_empty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11bd <file_path_from_stack+0x5d>
jmp    11fa <file_path_from_stack+0x9a>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stack_move@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    119f <file_path_from_stack+0x3f>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    125c <file_path_from_stack+0xfc>
jmp    121f <file_path_from_stack+0xbf>
lea    -0x28(%rbp),%rdi
call   1040 <stack_empty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123d <file_path_from_stack+0xdd>
jmp    124f <file_path_from_stack+0xef>
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdi
call   1050 <stack_move@plt>
jmp    121f <file_path_from_stack+0xbf>
movq   $0x0,-0x8(%rbp)
jmp    1333 <file_path_from_stack+0x1d3>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <stack_empty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1282 <file_path_from_stack+0x122>
jmp    12d2 <file_path_from_stack+0x172>
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdi
call   1050 <stack_move@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    -0x58(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1264 <file_path_from_stack+0x104>
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x30(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
jbe    131b <file_path_from_stack+0x1bb>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    132b <file_path_from_stack+0x1cb>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_path_from_stack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1060 <assert@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
movq   $0x0,0x8(%rsp)
mov    %r13,%rdi
call   1040 <stack_empty@plt>
xor    %r12d,%r12d
test   %eax,%eax
jne    11e7 <file_path_from_stack+0x87>
lea    0x8(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <stack_move@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
add    %rax,%r12
add    $0x1,%r12
mov    %r13,%rdi
call   1040 <stack_empty@plt>
test   %eax,%eax
je     11b0 <file_path_from_stack+0x50>
lea    (%r12,%r15,1),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
call   1040 <stack_empty@plt>
test   %rbx,%rbx
je     12a7 <file_path_from_stack+0x147>
mov    %r15,0x20(%rsp)
mov    %r14,0x18(%rsp)
mov    %rbx,0x10(%rsp)
test   %eax,%eax
jne    126f <file_path_from_stack+0x10f>
lea    0x8(%rsp),%rbp
mov    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1050 <stack_move@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
movb   $0x2f,(%rbx,%r15,1)
add    %r15,%rbx
add    $0x1,%rbx
mov    %rbp,%rdi
call   1040 <stack_empty@plt>
test   %eax,%eax
je     1230 <file_path_from_stack+0xd0>
mov    %rbx,%rax
mov    0x10(%rsp),%r14
sub    %r14,%rax
xor    %edi,%edi
cmp    %r12,%rax
sete   %dil
call   1060 <assert@plt>
mov    0x20(%rsp),%rbp
test   %rbp,%rbp
je     12af <file_path_from_stack+0x14f>
mov    %rbx,%rdi
mov    0x18(%rsp),%rsi
mov    %rbp,%rdx
call   1070 <memcpy@plt>
add    %rbp,%rbx
jmp    12b7 <file_path_from_stack+0x157>
test   %eax,%eax
je     12cf <file_path_from_stack+0x16f>
xor    %ebp,%ebp
jmp    12bd <file_path_from_stack+0x15d>
lea    (%r12,%r14,1),%rbx
add    $0xffffffffffffffff,%rbx
mov    %r14,%rbp
movb   $0x0,(%rbx)
mov    %rbp,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <stack_move@plt>
mov    %rbx,%rdi
call   1040 <stack_empty@plt>
test   %eax,%eax
je     12e0 <file_path_from_stack+0x180>
jmp    12bd <file_path_from_stack+0x15d>

LLVM IR (-O0):
%struct.stack = type { i32 }
define i8* @file_path_from_stack(%struct.stack** %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.stack**
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.stack*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
store %struct.stack** %0, %struct.stack*** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %6
store %struct.stack* null, %struct.stack** %7
store i64 0, i64* %8
br label %20
20: ; preds = %25, %2
%21 = load %struct.stack**, %struct.stack*** %4
%22 = call i32 @stack_empty(%struct.stack** %21)
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
br i1 %24, label %25, label %37
25: ; preds = %20
%26 = load %struct.stack**, %struct.stack*** %4
%27 = call i8* @stack_move(%struct.stack** %26, %struct.stack** %7)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp ne i8* %28, null
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i8*, i8** %9
%33 = call i64 @strlen(i8* %32)
%34 = add i64 %33, 1
%35 = load i64, i64* %8
%36 = add i64 %35, %34
store i64 %36, i64* %8
br label %20
37: ; preds = %20
%38 = load i64, i64* %8
%39 = load i64, i64* %6
%40 = add i64 %38, %39
%41 = add i64 %40, 1
%42 = call i8* @malloc(i64 %41)
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %54
45: ; preds = %37
br label %46
46: ; preds = %50, %45
%47 = call i32 @stack_empty(%struct.stack** %7)
%48 = icmp ne i32 %47, 0
%49 = xor i1 %48, true
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load %struct.stack**, %struct.stack*** %4
%52 = call i8* @stack_move(%struct.stack** %7, %struct.stack** %51)
br label %46
53: ; preds = %46
store i8* null, i8** %3
br label %103
54: ; preds = %37
%55 = load i8*, i8** %10
store i8* %55, i8** %11
br label %56
56: ; preds = %60, %54
%57 = call i32 @stack_empty(%struct.stack** %7)
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
br i1 %59, label %60, label %76
60: ; preds = %56
%61 = load %struct.stack**, %struct.stack*** %4
%62 = call i8* @stack_move(%struct.stack** %7, %struct.stack** %61)
store i8* %62, i8** %12
%63 = load i8*, i8** %12
%64 = call i64 @strlen(i8* %63)
store i64 %64, i64* %13
%65 = load i8*, i8** %11
%66 = load i8*, i8** %12
%67 = load i64, i64* %13
%68 = call i32 @memcpy(i8* %65, i8* %66, i64 %67)
%69 = load i8*, i8** %11
%70 = load i64, i64* %13
%71 = getelementptr inbounds i8, i8* %69, i64 %70
store i8 47, i8* %71
%72 = load i64, i64* %13
%73 = add i64 %72, 1
%74 = load i8*, i8** %11
%75 = getelementptr inbounds i8, i8* %74, i64 %73
store i8* %75, i8** %11
br label %56
76: ; preds = %56
%77 = load i8*, i8** %11
%78 = load i8*, i8** %10
%79 = ptrtoint i8* %77 to i64
%80 = ptrtoint i8* %78 to i64
%81 = sub i64 %79, %80
%82 = load i64, i64* %8
%83 = icmp eq i64 %81, %82
%84 = zext i1 %83 to i32
%85 = call i32 @assert(i32 %84)
%86 = load i64, i64* %6
%87 = icmp ugt i64 %86, 0
br i1 %87, label %88, label %96
88: ; preds = %76
%89 = load i8*, i8** %11
%90 = load i8*, i8** %5
%91 = load i64, i64* %6
%92 = call i32 @memcpy(i8* %89, i8* %90, i64 %91)
%93 = load i8*, i8** %11
%94 = load i64, i64* %6
%95 = getelementptr inbounds i8, i8* %93, i64 %94
store i8 0, i8* %95
br label %101
96: ; preds = %76
%97 = load i8*, i8** %10
%98 = load i64, i64* %8
%99 = sub i64 %98, 1
%100 = getelementptr inbounds i8, i8* %97, i64 %99
store i8 0, i8* %100
br label %101
101: ; preds = %96, %88
%102 = load i8*, i8** %10
store i8* %102, i8** %3
br label %103
103: ; preds = %101, %53
%104 = load i8*, i8** %3
ret i8* %104
}

LLVM IR (-O2):
%struct.stack = type { i32 }
define i8* @file_path_from_stack(%struct.stack** %0, i8* %1) {
%3 = alloca %struct.stack*
%4 = icmp ne i8* %1, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = bitcast %struct.stack** %3 to i8*
store %struct.stack* null, %struct.stack** %3
%9 = call i32 @stack_empty(%struct.stack** %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %2, %11
%12 = phi i64 [ %19, %11 ], [ 0, %2 ]
%13 = call i8* @stack_move(%struct.stack** %0, %struct.stack** %3)
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = call i64 @strlen(i8* dereferenceable(1) %13)
%18 = add i64 %12, 1
%19 = add i64 %18, %17
%20 = call i32 @stack_empty(%struct.stack** %0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %11, label %22
22: ; preds = %11, %2
%23 = phi i64 [ 0, %2 ], [ %19, %11 ]
%24 = add i64 %23, 1
%25 = add i64 %24, %7
%26 = call i8* @malloc(i64 %25)
%27 = icmp eq i8* %26, null
%28 = call i32 @stack_empty(%struct.stack** %3)
%29 = icmp eq i32 %28, 0
br i1 %27, label %31, label %30
30: ; preds = %22
br i1 %29, label %36, label %46
31: ; preds = %22
br i1 %29, label %32, label %63
32: ; preds = %31, %32
%33 = call i8* @stack_move(%struct.stack** %3, %struct.stack** %0)
%34 = call i32 @stack_empty(%struct.stack** %3)
%35 = icmp eq i32 %34, 0
br i1 %35, label %32, label %63
36: ; preds = %30, %36
%37 = phi i8* [ %43, %36 ], [ %26, %30 ]
%38 = call i8* @stack_move(%struct.stack** %3, %struct.stack** %0)
%39 = call i64 @strlen(i8* dereferenceable(1) %38)
%40 = call i32 @memcpy(i8* %37, i8* %38, i64 %39)
%41 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 47, i8* %41
%42 = add i64 %39, 1
%43 = getelementptr inbounds i8, i8* %37, i64 %42
%44 = call i32 @stack_empty(%struct.stack** %3)
%45 = icmp eq i32 %44, 0
br i1 %45, label %36, label %46
46: ; preds = %36, %30
%47 = phi i8* [ %26, %30 ], [ %43, %36 ]
%48 = ptrtoint i8* %47 to i64
%49 = ptrtoint i8* %26 to i64
%50 = sub i64 %48, %49
%51 = icmp eq i64 %50, %23
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = icmp eq i64 %7, 0
br i1 %54, label %58, label %55
55: ; preds = %46
%56 = call i32 @memcpy(i8* %47, i8* %1, i64 %7)
%57 = getelementptr inbounds i8, i8* %47, i64 %7
br label %61
58: ; preds = %46
%59 = add i64 %23, -1
%60 = getelementptr inbounds i8, i8* %26, i64 %59
br label %61
61: ; preds = %58, %55
%62 = phi i8* [ %57, %55 ], [ %60, %58 ]
store i8 0, i8* %62
br label %63
63: ; preds = %32, %61, %31
%64 = phi i8* [ null, %31 ], [ %26, %61 ], [ null, %32 ]
ret i8* %64
}