#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 *, char *);
int /*<<< orphan*/ isdigit(char);
scalar_t__ ispunct(char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *mask_set(char *destination, char *source, char *mask) {
  char local_source[128];
  char *local_source_ptr;
  int number_of_pound_signs;
  int number_of_digits;
  char *holder;
  int count_characters(char *s, char c);
  int count_digits(char *s);
  int pad_leading_characters(char *d, char *s, int number_digits_to_pad,
                             char c_to_pad);
  if (!*source) {
    *destination = '\0';
    return destination;
  }
  if (strlen(source) > strlen(mask)) {
    *destination = '\0';
    return destination;
  }
  number_of_pound_signs = count_characters(mask, '#');
  number_of_digits = count_digits(source);
  if (number_of_pound_signs < number_of_digits) {
    fprintf(stderr, "mask_set() has invalid input: source=(%s) mask=(%s)\n",
            source, mask);
    strcpy(destination, source);
    return destination;
  }
  pad_leading_characters(local_source, source,
                         number_of_pound_signs - number_of_digits, '0');
  holder = destination;
  local_source_ptr = local_source;
  while (*local_source_ptr && *mask) {
    if (*mask == '#') {
      if (!isdigit(*local_source_ptr) && *local_source_ptr != *mask) {
        return (char *)0;
      }
      *destination++ = *local_source_ptr++;
    } else {
      *destination++ = *mask;
      if (ispunct(*local_source_ptr))
        local_source_ptr++;
    }
    mask++;
  }
  *destination = '\0';
  return holder;
}


x86 assembly (clang, -O0):
<mask_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11b8 <mask_set+0x38>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <mask_set+0x244>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0xc0(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
cmp    %rcx,%rax
jle    11f8 <mask_set+0x78>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <mask_set+0x244>
mov    -0x20(%rbp),%rdi
mov    $0x23,%esi
call   10a0 <count_characters@plt>
mov    %eax,-0xac(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <count_digits@plt>
mov    %eax,-0xb0(%rbp)
mov    -0xac(%rbp),%eax
cmp    -0xb0(%rbp),%eax
jge    1264 <mask_set+0xe4>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdbb(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <mask_set+0x244>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xac(%rbp),%edx
sub    -0xb0(%rbp),%edx
mov    $0x30,%ecx
call   1090 <pad_leading_characters@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xc1(%rbp)
je     12cc <mask_set+0x14c>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xc1(%rbp)
mov    -0xc1(%rbp),%al
test   $0x1,%al
jne    12df <mask_set+0x15f>
jmp    13b2 <mask_set+0x232>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
jne    135a <mask_set+0x1da>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    132d <mask_set+0x1ad>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%eax
mov    -0x20(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     132d <mask_set+0x1ad>
movq   $0x0,-0x8(%rbp)
jmp    13c4 <mask_set+0x244>
mov    -0xa8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0xa8(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    13a1 <mask_set+0x221>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0xa8(%rbp),%rax
movsbl (%rax),%edi
call   1070 <ispunct@plt>
cmp    $0x0,%rax
je     139c <mask_set+0x21c>
mov    -0xa8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa8(%rbp)
jmp    13a1 <mask_set+0x221>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    129e <mask_set+0x11e>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xb8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mask_set>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%r14
cmpb   $0x0,(%rsi)
je     11b5 <mask_set+0x35>
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %rax,%rbp
jle    11cc <mask_set+0x4c>
movb   $0x0,(%r14)
mov    %r14,%rax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    $0x23,%esi
call   10a0 <count_characters@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1060 <count_digits@plt>
sub    %eax,%ebp
jge    120f <mask_set+0x8f>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1080 <fprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
jmp    11b9 <mask_set+0x39>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
mov    $0x30,%ecx
call   1090 <pad_leading_characters@plt>
mov    (%rsp),%al
mov    %r14,%rbp
test   %al,%al
je     1294 <mask_set+0x114>
mov    %r14,%rbp
jmp    1267 <mask_set+0xe7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edi
call   1030 <isdigit@plt>
movzbl (%r15),%ecx
test   %eax,%eax
je     1288 <mask_set+0x108>
add    $0x1,%r15
mov    %cl,0x0(%rbp)
add    $0x1,%rbp
add    $0x1,%rbx
movzbl (%r15),%eax
test   %al,%al
je     1294 <mask_set+0x114>
movzbl (%rbx),%ecx
cmp    $0x23,%cl
je     1240 <mask_set+0xc0>
test   %cl,%cl
je     1294 <mask_set+0x114>
mov    %cl,0x0(%rbp)
movsbl %al,%edi
call   1070 <ispunct@plt>
cmp    $0x1,%rax
sbb    $0xffffffffffffffff,%r15
jmp    1257 <mask_set+0xd7>
cmp    (%rbx),%cl
je     1250 <mask_set+0xd0>
xor    %r14d,%r14d
jmp    11b9 <mask_set+0x39>
movb   $0x0,0x0(%rbp)
jmp    11b9 <mask_set+0x39>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [53 x i8] c"mask_set() has invalid input: source=(%s) mask=(%s)\0A\00"
define i8* @mask_set(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [128 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %6
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i8*, i8** %5
store i8 0, i8* %17
%18 = load i8*, i8** %5
store i8* %18, i8** %4
br label %110
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = call i64 @strlen(i8* %20)
%22 = load i8*, i8** %7
%23 = call i64 @strlen(i8* %22)
%24 = icmp sgt i64 %21, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %5
store i8 0, i8* %26
%27 = load i8*, i8** %5
store i8* %27, i8** %4
br label %110
28: ; preds = %19
%29 = load i8*, i8** %7
%30 = call i32 @count_characters(i8* %29, i8 signext 35)
store i32 %30, i32* %10
%31 = load i8*, i8** %6
%32 = call i32 @count_digits(i8* %31)
store i32 %32, i32* %11
%33 = load i32, i32* %10
%34 = load i32, i32* %11
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %45
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %6
%39 = load i8*, i8** %7
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %38, i8* %39)
%41 = load i8*, i8** %5
%42 = load i8*, i8** %6
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i8*, i8** %5
store i8* %44, i8** %4
br label %110
45: ; preds = %28
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%47 = load i8*, i8** %6
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = sub nsw i32 %48, %49
%51 = call i32 @pad_leading_characters(i8* %46, i8* %47, i32 %50, i8 signext 48)
%52 = load i8*, i8** %5
store i8* %52, i8** %12
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
store i8* %53, i8** %9
br label %54
54: ; preds = %104, %45
%55 = load i8*, i8** %9
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = load i8*, i8** %7
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 0
br label %64
64: ; preds = %59, %54
%65 = phi i1 [ false, %54 ], [ %63, %59 ]
br i1 %65, label %66, label %107
66: ; preds = %64
%67 = load i8*, i8** %7
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 35
br i1 %70, label %71, label %91
71: ; preds = %66
%72 = load i8*, i8** %9
%73 = load i8, i8* %72
%74 = call i32 @isdigit(i8 signext %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %85, label %76
76: ; preds = %71
%77 = load i8*, i8** %9
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = load i8*, i8** %7
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp ne i32 %79, %82
br i1 %83, label %84, label %85
84: ; preds = %76
store i8* null, i8** %4
br label %110
85: ; preds = %76, %71
%86 = load i8*, i8** %9
%87 = getelementptr inbounds i8, i8* %86, i32 1
store i8* %87, i8** %9
%88 = load i8, i8* %86
%89 = load i8*, i8** %5
%90 = getelementptr inbounds i8, i8* %89, i32 1
store i8* %90, i8** %5
store i8 %88, i8* %89
br label %104
91: ; preds = %66
%92 = load i8*, i8** %7
%93 = load i8, i8* %92
%94 = load i8*, i8** %5
%95 = getelementptr inbounds i8, i8* %94, i32 1
store i8* %95, i8** %5
store i8 %93, i8* %94
%96 = load i8*, i8** %9
%97 = load i8, i8* %96
%98 = call i64 @ispunct(i8 signext %97)
%99 = icmp ne i64 %98, 0
br i1 %99, label %100, label %103
100: ; preds = %91
%101 = load i8*, i8** %9
%102 = getelementptr inbounds i8, i8* %101, i32 1
store i8* %102, i8** %9
br label %103
103: ; preds = %100, %91
br label %104
104: ; preds = %103, %85
%105 = load i8*, i8** %7
%106 = getelementptr inbounds i8, i8* %105, i32 1
store i8* %106, i8** %7
br label %54
107: ; preds = %64
%108 = load i8*, i8** %5
store i8 0, i8* %108
%109 = load i8*, i8** %12
store i8* %109, i8** %4
br label %110
110: ; preds = %107, %84, %36, %25, %16
%111 = load i8*, i8** %4
ret i8* %111
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"mask_set() has invalid input: source=(%s) mask=(%s)\0A\00"
define i8* @mask_set(i8* %0, i8* %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %3
store i8 0, i8* %0
br label %55
9: ; preds = %3
%10 = tail call i64 @strlen(i8* %1)
%11 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%12 = icmp sgt i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9
store i8 0, i8* %0
br label %55
14: ; preds = %9
%15 = tail call i32 @count_characters(i8* %2, i8 signext 35)
%16 = tail call i32 @count_digits(i8* %1)
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%21 = tail call i32 @strcpy(i8* %0, i8* %1)
br label %55
22: ; preds = %14
%23 = sub nsw i32 %15, %16
%24 = call i32 @pad_leading_characters(i8* %5, i8* %1, i32 %23, i8 signext 48)
%25 = load i8, i8* %5
%26 = icmp eq i8 %25, 0
br i1 %26, label %53, label %27
27: ; preds = %22, %47
%28 = phi i8 [ %51, %47 ], [ %25, %22 ]
%29 = phi i8* [ %49, %47 ], [ %0, %22 ]
%30 = phi i8* [ %50, %47 ], [ %2, %22 ]
%31 = phi i8* [ %48, %47 ], [ %5, %22 ]
%32 = load i8, i8* %30
switch i8 %32, label %42 [
i8 0, label %53
i8 35, label %33
]
33: ; preds = %27
%34 = call i32 @isdigit(i8 signext %28)
%35 = icmp eq i32 %34, 0
%36 = load i8, i8* %31
br i1 %35, label %37, label %40
37: ; preds = %33
%38 = load i8, i8* %30
%39 = icmp eq i8 %36, %38
br i1 %39, label %40, label %55
40: ; preds = %33, %37
%41 = getelementptr inbounds i8, i8* %31, i64 1
store i8 %36, i8* %29
br label %47
42: ; preds = %27
store i8 %32, i8* %29
%43 = call i64 @ispunct(i8 signext %28)
%44 = icmp ne i64 %43, 0
%45 = zext i1 %44 to i64
%46 = getelementptr i8, i8* %31, i64 %45
br label %47
47: ; preds = %42, %40
%48 = phi i8* [ %41, %40 ], [ %46, %42 ]
%49 = getelementptr inbounds i8, i8* %29, i64 1
%50 = getelementptr inbounds i8, i8* %30, i64 1
%51 = load i8, i8* %48
%52 = icmp eq i8 %51, 0
br i1 %52, label %53, label %27
53: ; preds = %47, %27, %22
%54 = phi i8* [ %0, %22 ], [ %29, %27 ], [ %49, %47 ]
store i8 0, i8* %54
br label %55
55: ; preds = %37, %53, %18, %13, %8
%56 = phi i8* [ %0, %13 ], [ %0, %18 ], [ %0, %53 ], [ %0, %8 ], [ null, %37 ]
ret i8* %56
}