#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 *);
int /*<<< orphan*/ printf(char *, unsigned char, unsigned char, unsigned char,
                          unsigned char);
int /*<<< orphan*/ segment_hash(char *, int, char *, size_t, unsigned char *,
                                int, int);
int sscanf(char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
size_t strlen(char *);
int main(int argc, char **argv) {
  char w1[4], w2[4], w3[7], buf[7];
  unsigned char quad[4];
  size_t key_len;
  unsigned short *p1;
  unsigned char *p2;
  if (argc != 3) {
    fprintf(stderr, "Usage: cloak <secret> <cloaked host>\n");
    return 1;
  }
  key_len = strlen(argv[1]);
  if (3 != sscanf(argv[2], "%3s.%3s.%6s", w1, w2, w3)) {
    fprintf(stderr, "Host must be of format xxx.xxx.xxxxxx\n");
    return 2;
  }
  p1 = (unsigned short *)quad;
  for (*p1 = 1; *p1 != 0; (*p1)++) {
    segment_hash(buf, 6, argv[1], key_len, quad, 2, 13);
    if (!strcmp(buf, w3))
      break;
  }
  p2 = quad + 2;
  for (*p2 = 1; *p2 != 0; (*p2)++) {
    segment_hash(buf, 3, argv[1], key_len, quad, 3, 11);
    if (!strcmp(buf, w2))
      break;
  }
  p2++;
  for (*p2 = 1; *p2 != 0; (*p2)++) {
    segment_hash(buf, 3, argv[1], key_len, quad, 4, 10);
    if (!strcmp(buf, w1))
      break;
  }
  printf("%d.%d.%d.%d\n", quad[0], quad[1], quad[2], quad[3]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a5 <main+0x245>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1f(%rbp),%r8
lea    0xe59(%rip),%rsi
call   1080 <sscanf@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    %ecx,%eax
je     1202 <main+0xa2>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a5 <main+0x245>
lea    -0x2a(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movw   $0x1,(%rax)
mov    -0x40(%rbp),%rax
movzwl (%rax),%eax
cmp    $0x0,%eax
je     1281 <main+0x121>
lea    -0x26(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x2a(%rbp),%r8
mov    $0x6,%esi
mov    $0x2,%r9d
movl   $0xd,(%rsp)
call   1030 <segment_hash@plt>
lea    -0x26(%rbp),%rdi
lea    -0x1f(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1269 <main+0x109>
jmp    1281 <main+0x121>
jmp    126e <main+0x10e>
mov    -0x40(%rbp),%rax
mov    (%rax),%cx
add    $0x1,%cx
mov    %cx,(%rax)
jmp    1213 <main+0xb3>
lea    -0x2a(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x48(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     12fd <main+0x19d>
lea    -0x26(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x2a(%rbp),%r8
mov    $0x3,%r9d
mov    %r9d,%esi
movl   $0xb,(%rsp)
call   1030 <segment_hash@plt>
lea    -0x26(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12e8 <main+0x188>
jmp    12fd <main+0x19d>
jmp    12ed <main+0x18d>
mov    -0x48(%rbp),%rax
mov    (%rax),%cl
add    $0x1,%cl
mov    %cl,(%rax)
jmp    1294 <main+0x134>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x48(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     137b <main+0x21b>
lea    -0x26(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x2a(%rbp),%r8
mov    $0x3,%esi
mov    $0x4,%r9d
movl   $0xa,(%rsp)
call   1030 <segment_hash@plt>
lea    -0x26(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1366 <main+0x206>
jmp    137b <main+0x21b>
jmp    136b <main+0x20b>
mov    -0x48(%rbp),%rax
mov    (%rax),%cl
add    $0x1,%cl
mov    %cl,(%rax)
jmp    1310 <main+0x1b0>
mov    -0x2a(%rbp),%dl
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%al
lea    0xcce(%rip),%rdi
movzbl %dl,%esi
movzbl %cl,%edx
movzbl %al,%ecx
movzbl -0x27(%rbp),%r8d
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
cmp    $0x3,%edi
jne    12d7 <main+0x177>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe96(%rip),%rsi
lea    0x1e(%rsp),%rdx
lea    0x1a(%rsp),%rcx
lea    0x29(%rsp),%r8
call   1080 <sscanf@plt>
cmp    $0x3,%eax
jne    12f3 <main+0x193>
movw   $0x1,0x16(%rsp)
lea    0x22(%rsp),%r15
lea    0x16(%rsp),%r12
lea    0x29(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rbx),%rdx
movl   $0xd,(%rsp)
mov    %r15,%rdi
mov    $0x6,%esi
mov    %r14,%rcx
mov    %r12,%r8
mov    $0x2,%r9d
call   1030 <segment_hash@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     120b <main+0xab>
addw   $0x1,0x16(%rsp)
jne    11d0 <main+0x70>
movb   $0x1,0x18(%rsp)
lea    0x22(%rsp),%r15
lea    0x16(%rsp),%r12
lea    0x1a(%rsp),%r13
nop
mov    0x8(%rbx),%rdx
movl   $0xb,(%rsp)
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r14,%rcx
mov    %r12,%r8
mov    $0x3,%r9d
call   1030 <segment_hash@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     125a <main+0xfa>
addb   $0x1,0x18(%rsp)
jne    1220 <main+0xc0>
movb   $0x1,0x19(%rsp)
lea    0x22(%rsp),%r15
lea    0x16(%rsp),%r12
lea    0x1e(%rsp),%r13
xchg   %ax,%ax
mov    0x8(%rbx),%rdx
movl   $0xa,(%rsp)
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r14,%rcx
mov    %r12,%r8
mov    $0x4,%r9d
call   1030 <segment_hash@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
movzbl 0x19(%rsp),%edi
test   %eax,%eax
je     12b4 <main+0x154>
inc    %dil
mov    %dil,0x19(%rsp)
jne    1270 <main+0x110>
xor    %edi,%edi
movzbl 0x16(%rsp),%esi
movzbl 0x17(%rsp),%edx
movzbl 0x18(%rsp),%ecx
movzbl %dil,%r8d
lea    0xd8b(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
jmp    130d <main+0x1ad>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd19(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    130d <main+0x1ad>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: cloak <secret> <cloaked host>\0A\00"
@.str.1 = [12 x i8] c"%3s.%3s.%6s\00"
@.str.2 = [39 x i8] c"Host must be of format xxx.xxx.xxxxxx\0A\00"
@.str.3 = [13 x i8] c"%d.%d.%d.%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4 x i8]
%7 = alloca [4 x i8]
%8 = alloca [7 x i8]
%9 = alloca [7 x i8]
%10 = alloca [4 x i8]
%11 = alloca i64
%12 = alloca i16*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %126
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %11
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%30 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28, i8* %29)
%31 = icmp ne i32 3, %30
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %126
35: ; preds = %19
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%37 = bitcast i8* %36 to i16*
store i16* %37, i16** %12
%38 = load i16*, i16** %12
store i16 1, i16* %38
br label %39
39: ; preds = %58, %35
%40 = load i16*, i16** %12
%41 = load i16, i16* %40
%42 = zext i16 %41 to i32
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %62
44: ; preds = %39
%45 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = load i64, i64* %11
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%51 = call i32 @segment_hash(i8* %45, i32 6, i8* %48, i64 %49, i8* %50, i32 2, i32 13)
%52 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%53 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%54 = call i32 @strcmp(i8* %52, i8* %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %44
br label %62
57: ; preds = %44
br label %58
58: ; preds = %57
%59 = load i16*, i16** %12
%60 = load i16, i16* %59
%61 = add i16 %60, 1
store i16 %61, i16* %59
br label %39
62: ; preds = %56, %39
%63 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%64 = getelementptr inbounds i8, i8* %63, i64 2
store i8* %64, i8** %13
%65 = load i8*, i8** %13
store i8 1, i8* %65
br label %66
66: ; preds = %85, %62
%67 = load i8*, i8** %13
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %89
71: ; preds = %66
%72 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = load i64, i64* %11
%77 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%78 = call i32 @segment_hash(i8* %72, i32 3, i8* %75, i64 %76, i8* %77, i32 3, i32 11)
%79 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%80 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%81 = call i32 @strcmp(i8* %79, i8* %80)
%82 = icmp ne i32 %81, 0
br i1 %82, label %84, label %83
83: ; preds = %71
br label %89
84: ; preds = %71
br label %85
85: ; preds = %84
%86 = load i8*, i8** %13
%87 = load i8, i8* %86
%88 = add i8 %87, 1
store i8 %88, i8* %86
br label %66
89: ; preds = %83, %66
%90 = load i8*, i8** %13
%91 = getelementptr inbounds i8, i8* %90, i32 1
store i8* %91, i8** %13
%92 = load i8*, i8** %13
store i8 1, i8* %92
br label %93
93: ; preds = %112, %89
%94 = load i8*, i8** %13
%95 = load i8, i8* %94
%96 = zext i8 %95 to i32
%97 = icmp ne i32 %96, 0
br i1 %97, label %98, label %116
98: ; preds = %93
%99 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%100 = load i8**, i8*** %5
%101 = getelementptr inbounds i8*, i8** %100, i64 1
%102 = load i8*, i8** %101
%103 = load i64, i64* %11
%104 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%105 = call i32 @segment_hash(i8* %99, i32 3, i8* %102, i64 %103, i8* %104, i32 4, i32 10)
%106 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%107 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%108 = call i32 @strcmp(i8* %106, i8* %107)
%109 = icmp ne i32 %108, 0
br i1 %109, label %111, label %110
110: ; preds = %98
br label %116
111: ; preds = %98
br label %112
112: ; preds = %111
%113 = load i8*, i8** %13
%114 = load i8, i8* %113
%115 = add i8 %114, 1
store i8 %115, i8* %113
br label %93
116: ; preds = %110, %93
%117 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%118 = load i8, i8* %117
%119 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%120 = load i8, i8* %119
%121 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
%122 = load i8, i8* %121
%123 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 3
%124 = load i8, i8* %123
%125 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %118, i8 zeroext %120, i8 zeroext %122, i8 zeroext %124)
store i32 0, i32* %3
br label %126
126: ; preds = %116, %32, %16
%127 = load i32, i32* %3
ret i32 %127
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: cloak <secret> <cloaked host>\0A\00"
@.str.1 = [12 x i8] c"%3s.%3s.%6s\00"
@.str.2 = [39 x i8] c"Host must be of format xxx.xxx.xxxxxx\0A\00"
@.str.3 = [13 x i8] c"%d.%d.%d.%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = alloca [4 x i8]
%5 = alloca [7 x i8]
%6 = alloca [7 x i8]
%7 = alloca [4 x i8]
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [7 x i8], [7 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%13 = icmp eq i32 %0, 3
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %68
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i64 @strlen(i8* dereferenceable(1) %19)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %9, i8* %10)
%24 = icmp eq i32 %23, 3
br i1 %24, label %28, label %25
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %68
28: ; preds = %17
%29 = bitcast [4 x i8]* %7 to i16*
store i16 1, i16* %29
br label %30
30: ; preds = %28, %35
%31 = load i8*, i8** %18
%32 = call i32 @segment_hash(i8* %11, i32 6, i8* %31, i64 %20, i8* %12, i32 2, i32 13)
%33 = call i32 @strcmp(i8* %11, i8* %10)
%34 = icmp eq i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %30
%36 = load i16, i16* %29
%37 = add i16 %36, 1
store i16 %37, i16* %29
%38 = icmp eq i16 %37, 0
br i1 %38, label %39, label %30
39: ; preds = %30, %35
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 2
store i8 1, i8* %40
br label %41
41: ; preds = %39, %46
%42 = load i8*, i8** %18
%43 = call i32 @segment_hash(i8* %11, i32 3, i8* %42, i64 %20, i8* %12, i32 3, i32 11)
%44 = call i32 @strcmp(i8* %11, i8* %9)
%45 = icmp eq i32 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %41
%47 = load i8, i8* %40
%48 = add i8 %47, 1
store i8 %48, i8* %40
%49 = icmp eq i8 %48, 0
br i1 %49, label %50, label %41
50: ; preds = %41, %46
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
store i8 1, i8* %51
br label %52
52: ; preds = %50, %58
%53 = load i8*, i8** %18
%54 = call i32 @segment_hash(i8* %11, i32 3, i8* %53, i64 %20, i8* %12, i32 4, i32 10)
%55 = call i32 @strcmp(i8* %11, i8* %8)
%56 = icmp eq i32 %55, 0
%57 = load i8, i8* %51
br i1 %56, label %61, label %58
58: ; preds = %52
%59 = add i8 %57, 1
store i8 %59, i8* %51
%60 = icmp eq i8 %59, 0
br i1 %60, label %61, label %52
61: ; preds = %58, %52
%62 = phi i8 [ %57, %52 ], [ 0, %58 ]
%63 = load i8, i8* %12
%64 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 1
%65 = load i8, i8* %64
%66 = load i8, i8* %40
%67 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %63, i8 zeroext %65, i8 zeroext %66, i8 zeroext %62)
br label %68
68: ; preds = %61, %25, %14
%69 = phi i32 [ 1, %14 ], [ 2, %25 ], [ 0, %61 ]
ret i32 %69
}