#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 EXIT_SUCCESS;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ decrypt(unsigned int *, unsigned int *);
int /*<<< orphan*/ encrypt(unsigned int *, unsigned int *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int in, out;
  bool enc = true;
  char inbuf[8];
  char key[8 * 4];
  unsigned int i, *pkey, *ptr_in;
  memset(key, 0, 8 * 4);
  strncpy(key, "A TRULY GENUINE PASSWORD", 25);
  pkey = (unsigned int *)key;
  if (argc == 4)
    enc = false;
  if (!(in = open(argv[1], O_RDONLY)) ||
      !(out = open(argv[2], O_WRONLY | O_EXCL | O_CREAT))) {
    printf("\nUsage: Feistel input output [flag]\n");
    exit(0);
  }
  int n;
  while ((n = read(in, inbuf, 8)) > 0) {
    ptr_in = (unsigned int *)inbuf;
    if (enc == true)
      encrypt(ptr_in, pkey);
    else
      decrypt(ptr_in, pkey);
    write(out, inbuf, 8);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
lea    -0x50(%rbp),%rdi
lea    0xe38(%rip),%rsi
mov    $0x19,%edx
call   1040 <strncpy@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11eb <main+0x5b>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1241 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    1254 <main+0xc4>
lea    0xdd1(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1259 <main+0xc9>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x8,%edx
call   1090 <read@plt>
mov    %eax,-0x6c(%rbp)
cmp    $0x0,%eax
jle    12bd <main+0x12d>
lea    -0x24(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    129a <main+0x10a>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
call   1070 <encrypt@plt>
jmp    12a7 <main+0x117>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
call   1030 <decrypt@plt>
mov    -0x18(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
jmp    1259 <main+0xc9>
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
lea    0xe43(%rip),%rsi
mov    %rbp,%rdi
mov    $0x19,%edx
call   1040 <strncpy@plt>
mov    0x8(%rbx),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
je     1291 <main+0x101>
mov    %eax,%r15d
mov    0x2dca(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
je     1291 <main+0x101>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x8,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    127b <main+0xeb>
lea    0x8(%rsp),%rbp
lea    0x10(%rsp),%r12
jmp    1268 <main+0xd8>
nopw   0x0(%rax,%rax,1)
call   1030 <decrypt@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1060 <write@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    127b <main+0xeb>
mov    %rbp,%rdi
mov    %r12,%rsi
cmp    $0x4,%r14d
je     1240 <main+0xb0>
call   1070 <encrypt@plt>
jmp    1245 <main+0xb5>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd81(%rip),%rdi
call   1050 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"A TRULY GENUINE PASSWORD\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@.str.1 = [37 x i8] c"\0AUsage: Feistel input output [flag]\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [8 x i8]
%10 = alloca [32 x i8]
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 32)
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%18 = call i32 @strncpy(i8* %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 25)
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%20 = bitcast i8* %19 to i32*
store i32* %20, i32** %12
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 4
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 0, i32* %8
br label %24
24: ; preds = %23, %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %24
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_EXCL
%37 = or i32 %35, %36
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %34, i32 %39)
store i32 %40, i32* %7
%41 = icmp ne i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %31, %24
%43 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %31
br label %46
46: ; preds = %64, %45
%47 = load i32, i32* %6
%48 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 8)
store i32 %49, i32* %14
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %68
51: ; preds = %46
%52 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%53 = bitcast i8* %52 to i32*
store i32* %53, i32** %13
%54 = load i32, i32* %8
%55 = icmp eq i32 %54, 1
br i1 %55, label %56, label %60
56: ; preds = %51
%57 = load i32*, i32** %13
%58 = load i32*, i32** %12
%59 = call i32 @encrypt(i32* %57, i32* %58)
br label %64
60: ; preds = %51
%61 = load i32*, i32** %13
%62 = load i32*, i32** %12
%63 = call i32 @decrypt(i32* %61, i32* %62)
br label %64
64: ; preds = %60, %56
%65 = load i32, i32* %7
%66 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%67 = call i32 @write(i32 %65, i8* %66, i32 8)
br label %46
68: ; preds = %46
%69 = load i32, i32* @EXIT_SUCCESS
ret i32 %69
}

LLVM IR (-O2):
@.str = [25 x i8] c"A TRULY GENUINE PASSWORD\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [36 x i8] c"\0AUsage: Feistel input output [flag]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 32)
%8 = call i32 @strncpy(i8* %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 25)
%9 = bitcast [32 x i8]* %4 to i32*
%10 = icmp eq i32 %0, 4
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %31, label %16
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_EXCL
%21 = or i32 %20, %19
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %18, i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %16
%27 = call i32 @read(i32 %14, i8* %5, i32 8)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = bitcast [8 x i8]* %3 to i32*
br label %34
31: ; preds = %16, %2
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %29, %39
br i1 %10, label %37, label %35
35: ; preds = %34
%36 = call i32 @encrypt(i32* %30, i32* %9)
br label %39
37: ; preds = %34
%38 = call i32 @decrypt(i32* %30, i32* %9)
br label %39
39: ; preds = %37, %35
%40 = call i32 @write(i32 %24, i8* %5, i32 8)
%41 = call i32 @read(i32 %14, i8* %5, i32 8)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %34, label %43
43: ; preds = %39, %26
%44 = load i32, i32* @EXIT_SUCCESS
ret i32 %44
}