#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 */
scalar_t__ chdir(char *);
char *filename_unix(char *, int, char const *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char *, char);
char *filename_full(char *buffer, size_t buflen, const char *pattern) {
  size_t len;
  char *str, *s1, *s2, Path[260], NewPath[260], Temp[260];
  str = filename_unix(Temp, 260, pattern);
  if (str == (char *)NULL)
    return ((char *)NULL);
  if (strcmp(str, "/dev/null") == 0) {
    len = ((buflen) < (strlen(str) + 1) ? (buflen) : (strlen(str) + 1)) - 1;
    (void)strncpy(buffer, str, len + 1);
    buffer[len] = '\0';
    return (buffer);
  }
  (void)getcwd(Path, 260);
  s1 = str;
  s2 = str;
  if ((str = strrchr(str, '/')) == (char *)NULL) {
    s1 = Path;
  } else {
    *str = '\0';
    if (*s1 == '\0')
      s1 = "/";
    s2 = ++str;
    if (chdir(s1)) {
      chdir(Path);
      return ((char *)NULL);
    }
    (void)getcwd(NewPath, 260);
    if (chdir(Path))
      return ((char *)NULL);
    s1 = NewPath;
  }
  str = &NewPath[strlen(s1) - 1];
  if (*str == '/')
    *str = '\0';
  (void)sprintf(Path, "%s/%s", s1, s2);
  len = ((buflen) < (strlen(Path) + 1) ? (buflen) : (strlen(Path) + 1)) - 1;
  (void)strncpy(buffer, Path, len + 1);
  buffer[len] = '\0';
  return (buffer);
}


x86 assembly (clang, -O0):
<filename_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x390,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x370(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x104,%esi
call   1090 <filename_unix@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c8 <filename_full+0x48>
movq   $0x0,-0x8(%rbp)
jmp    143b <filename_full+0x2bb>
mov    -0x30(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1270 <filename_full+0xf0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x378(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x378(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    121e <filename_full+0x9e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x380(%rbp)
jmp    1233 <filename_full+0xb3>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x380(%rbp)
mov    -0x380(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    143b <filename_full+0x2bb>
lea    -0x150(%rbp),%rdi
mov    $0x104,%esi
call   1070 <getcwd@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12bd <filename_full+0x13d>
lea    -0x150(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    135a <filename_full+0x1da>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12df <filename_full+0x15f>
lea    0xd2f(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     131b <filename_full+0x19b>
lea    -0x150(%rbp),%rdi
call   1050 <chdir@plt>
movq   $0x0,-0x8(%rbp)
jmp    143b <filename_full+0x2bb>
lea    -0x260(%rbp),%rdi
mov    $0x104,%esi
call   1070 <getcwd@plt>
lea    -0x150(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     134f <filename_full+0x1cf>
movq   $0x0,-0x8(%rbp)
jmp    143b <filename_full+0x2bb>
lea    -0x260(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
movslq %eax,%rcx
lea    -0x260(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    138e <filename_full+0x20e>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x150(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xc68(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x388(%rbp)
lea    -0x150(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x388(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    13e8 <filename_full+0x268>
mov    -0x18(%rbp),%rax
mov    %rax,-0x390(%rbp)
jmp    1400 <filename_full+0x280>
lea    -0x150(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x390(%rbp)
mov    -0x390(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x150(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x390,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filename_full>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x330,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x220(%rsp),%rdi
mov    $0x104,%esi
call   1090 <filename_unix@plt>
test   %rax,%rax
je     1266 <filename_full+0xe6>
mov    %rax,%rbx
lea    0xe45(%rip),%rsi
mov    %rax,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1219 <filename_full+0x99>
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x104,%esi
call   1070 <getcwd@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1272 <filename_full+0xf2>
mov    %rax,%r13
movb   $0x0,(%rax)
cmpb   $0x0,(%rbx)
lea    0xe0c(%rip),%rax
cmove  %rax,%rbx
mov    %rbx,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     1244 <filename_full+0xc4>
mov    %rsp,%rdi
call   1050 <chdir@plt>
jmp    1266 <filename_full+0xe6>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
ja     1239 <filename_full+0xb9>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r14
mov    %r15,%rdi
mov    %rbx,%rsi
jmp    12d1 <filename_full+0x151>
lea    0x110(%rsp),%r12
mov    %r12,%rdi
mov    $0x104,%esi
call   1070 <getcwd@plt>
mov    %rsp,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     126b <filename_full+0xeb>
xor    %r15d,%r15d
jmp    12df <filename_full+0x15f>
add    $0x1,%r13
mov    %r13,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
lea    0x110(%rsp),%rcx
cmpb   $0x2f,-0x1(%rax,%rcx,1)
jne    1293 <filename_full+0x113>
movb   $0x0,0x10f(%rsp,%rax,1)
lea    0xd72(%rip),%rsi
mov    %rsp,%r13
mov    %r13,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
call   10a0 <sprintf@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
ja     12cb <filename_full+0x14b>
mov    %rsp,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r14
mov    %rsp,%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %r15,%rax
add    $0x330,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @filename_full(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [260 x i8]
%13 = alloca [260 x i8]
%14 = alloca [260 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%15 = getelementptr inbounds [260 x i8], [260 x i8]* %14, i64 0, i64 0
%16 = load i8*, i8** %7
%17 = call i8* @filename_unix(i8* %15, i32 260, i8* %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %3
store i8* null, i8** %4
br label %128
21: ; preds = %3
%22 = load i8*, i8** %9
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %51
25: ; preds = %21
%26 = load i64, i64* %6
%27 = load i8*, i8** %9
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = icmp ult i64 %26, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = load i64, i64* %6
br label %39
34: ; preds = %25
%35 = load i8*, i8** %9
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %36, 1
%38 = sext i32 %37 to i64
br label %39
39: ; preds = %34, %32
%40 = phi i64 [ %33, %32 ], [ %38, %34 ]
%41 = sub i64 %40, 1
store i64 %41, i64* %8
%42 = load i8*, i8** %5
%43 = load i8*, i8** %9
%44 = load i64, i64* %8
%45 = add i64 %44, 1
%46 = call i32 @strncpy(i8* %42, i8* %43, i64 %45)
%47 = load i8*, i8** %5
%48 = load i64, i64* %8
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %5
store i8* %50, i8** %4
br label %128
51: ; preds = %21
%52 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%53 = call i32 @getcwd(i8* %52, i32 260)
%54 = load i8*, i8** %9
store i8* %54, i8** %10
%55 = load i8*, i8** %9
store i8* %55, i8** %11
%56 = load i8*, i8** %9
%57 = call i8* @strrchr(i8* %56, i8 signext 47)
store i8* %57, i8** %9
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %61
59: ; preds = %51
%60 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
store i8* %60, i8** %10
br label %86
61: ; preds = %51
%62 = load i8*, i8** %9
store i8 0, i8* %62
%63 = load i8*, i8** %10
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %61
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %68
68: ; preds = %67, %61
%69 = load i8*, i8** %9
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %9
store i8* %70, i8** %11
%71 = load i8*, i8** %10
%72 = call i64 @chdir(i8* %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %68
%75 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%76 = call i64 @chdir(i8* %75)
store i8* null, i8** %4
br label %128
77: ; preds = %68
%78 = getelementptr inbounds [260 x i8], [260 x i8]* %13, i64 0, i64 0
%79 = call i32 @getcwd(i8* %78, i32 260)
%80 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%81 = call i64 @chdir(i8* %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %77
store i8* null, i8** %4
br label %128
84: ; preds = %77
%85 = getelementptr inbounds [260 x i8], [260 x i8]* %13, i64 0, i64 0
store i8* %85, i8** %10
br label %86
86: ; preds = %84, %59
%87 = load i8*, i8** %10
%88 = call i32 @strlen(i8* %87)
%89 = sub nsw i32 %88, 1
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [260 x i8], [260 x i8]* %13, i64 0, i64 %90
store i8* %91, i8** %9
%92 = load i8*, i8** %9
%93 = load i8, i8* %92
%94 = sext i8 %93 to i32
%95 = icmp eq i32 %94, 47
br i1 %95, label %96, label %98
96: ; preds = %86
%97 = load i8*, i8** %9
store i8 0, i8* %97
br label %98
98: ; preds = %96, %86
%99 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%100 = load i8*, i8** %10
%101 = load i8*, i8** %11
%102 = call i32 @sprintf(i8* %99, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %100, i8* %101)
%103 = load i64, i64* %6
%104 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%105 = call i32 @strlen(i8* %104)
%106 = add nsw i32 %105, 1
%107 = sext i32 %106 to i64
%108 = icmp ult i64 %103, %107
br i1 %108, label %109, label %111
109: ; preds = %98
%110 = load i64, i64* %6
br label %116
111: ; preds = %98
%112 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%113 = call i32 @strlen(i8* %112)
%114 = add nsw i32 %113, 1
%115 = sext i32 %114 to i64
br label %116
116: ; preds = %111, %109
%117 = phi i64 [ %110, %109 ], [ %115, %111 ]
%118 = sub i64 %117, 1
store i64 %118, i64* %8
%119 = load i8*, i8** %5
%120 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%121 = load i64, i64* %8
%122 = add i64 %121, 1
%123 = call i32 @strncpy(i8* %119, i8* %120, i64 %122)
%124 = load i8*, i8** %5
%125 = load i64, i64* %8
%126 = getelementptr inbounds i8, i8* %124, i64 %125
store i8 0, i8* %126
%127 = load i8*, i8** %5
store i8* %127, i8** %4
br label %128
128: ; preds = %116, %83, %74, %39, %20
%129 = load i8*, i8** %4
ret i8* %129
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @filename_full(i8* %0, i64 %1, i8* %2) {
%4 = alloca [260 x i8]
%5 = alloca [260 x i8]
%6 = alloca [260 x i8]
%7 = getelementptr inbounds [260 x i8], [260 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 0
%10 = call i8* @filename_unix(i8* %9, i32 260, i8* %2)
%11 = icmp eq i8* %10, null
br i1 %11, label %71, label %12
12: ; preds = %3
%13 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = call i32 @strlen(i8* %10)
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = icmp ugt i64 %18, %1
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = call i32 @strlen(i8* %10)
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
br label %24
24: ; preds = %15, %20
%25 = phi i64 [ %23, %20 ], [ %1, %15 ]
%26 = add i64 %25, -1
%27 = call i32 @strncpy(i8* %0, i8* %10, i64 %25)
%28 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 0, i8* %28
br label %71
29: ; preds = %12
%30 = call i32 @getcwd(i8* %7, i32 260)
%31 = call i8* @strrchr(i8* %10, i8 signext 47)
%32 = icmp eq i8* %31, null
br i1 %32, label %46, label %33
33: ; preds = %29
store i8 0, i8* %31
%34 = load i8, i8* %10
%35 = icmp eq i8 %34, 0
%36 = select i1 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %10
%37 = call i64 @chdir(i8* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %33
%40 = call i64 @chdir(i8* %7)
br label %71
41: ; preds = %33
%42 = getelementptr inbounds i8, i8* %31, i64 1
%43 = call i32 @getcwd(i8* %8, i32 260)
%44 = call i64 @chdir(i8* %7)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %71
46: ; preds = %41, %29
%47 = phi i8* [ %7, %29 ], [ %8, %41 ]
%48 = phi i8* [ %10, %29 ], [ %42, %41 ]
%49 = call i32 @strlen(i8* %47)
%50 = add nsw i32 %49, -1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = icmp eq i8 %53, 47
br i1 %54, label %55, label %56
55: ; preds = %46
store i8 0, i8* %52
br label %56
56: ; preds = %55, %46
%57 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %48)
%58 = call i32 @strlen(i8* %7)
%59 = add nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = icmp ugt i64 %60, %1
br i1 %61, label %66, label %62
62: ; preds = %56
%63 = call i32 @strlen(i8* %7)
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
br label %66
66: ; preds = %56, %62
%67 = phi i64 [ %65, %62 ], [ %1, %56 ]
%68 = add i64 %67, -1
%69 = call i32 @strncpy(i8* %0, i8* %7, i64 %67)
%70 = getelementptr inbounds i8, i8* %0, i64 %68
store i8 0, i8* %70
br label %71
71: ; preds = %41, %3, %66, %39, %24
%72 = phi i8* [ %0, %24 ], [ %0, %66 ], [ null, %39 ], [ null, %3 ], [ null, %41 ]
ret i8* %72
}