#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 */
typedef int /*<<< orphan*/ verify;
typedef int /*<<< orphan*/ flag;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int, char const *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ system(char *);
int write(int, char const *, int);
int main(int argc, char *argv[]) {
  static const char flag[] = "CTF{D1ff3r3nT_D3v1Ce_S4mE_Tr1Ckz}";
  static const char flag2[64] = "CTF{B3h1nD_S3vEn_D4v1C3s}";
  static const char key[256] = "flag";
  char verify[64] = {};
  system("sysctl -w kernel.panic_on_oops=1");
  int f = open("/dev/pcivault", O_RDWR);
  if (f < 0)
    err(1, "open");
  if (ioctl(f, 0, flag2) < 0)
    err(1, "ioctl");
  while (1) {
    if (ioctl(f, 1, key) < 0)
      err(1, "ioctl");
    if (write(f, flag, sizeof(flag)) != sizeof(flag))
      err(1, "writing flag");
    if (read(f, verify, sizeof(verify)) != sizeof(flag) ||
        strcmp(verify, flag)) {
      fprintf(stderr, "Verifying flag failed\n");
    }
    sleep(2);
  }
  close(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    0xfa3(%rip),%rdi
call   1040 <system@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xfaf(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1205 <main+0x65>
mov    $0x1,%edi
lea    0xf9f(%rip),%rsi
call   10a0 <err@plt>
mov    -0x54(%rbp),%edi
xor    %esi,%esi
lea    0xe1f(%rip),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1231 <main+0x91>
mov    $0x1,%edi
lea    0xf78(%rip),%rsi
call   10a0 <err@plt>
jmp    1236 <main+0x96>
mov    -0x54(%rbp),%edi
mov    $0x1,%esi
lea    0xe2b(%rip),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1265 <main+0xc5>
mov    $0x1,%edi
lea    0xf44(%rip),%rsi
call   10a0 <err@plt>
mov    -0x54(%rbp),%edi
lea    0xd91(%rip),%rsi
mov    $0x22,%edx
call   1030 <write@plt>
cltq
cmp    $0x22,%rax
je     1296 <main+0xf6>
mov    $0x1,%edi
lea    0xf19(%rip),%rsi
call   10a0 <err@plt>
mov    -0x54(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
cltq
cmp    $0x22,%rax
jne    12cd <main+0x12d>
lea    -0x50(%rbp),%rdi
lea    0xd42(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12e2 <main+0x142>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xeda(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
jmp    1236 <main+0x96>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xfb5(%rip),%rdi
call   1040 <system@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0xfc1(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ec <main+0x5c>
lea    0xfbd(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
lea    0xe3d(%rip),%rdx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <ioctl@plt>
test   %rax,%rax
jns    1212 <main+0x82>
lea    0xf9c(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
lea    0xe57(%rip),%r14
lea    0xde0(%rip),%r12
mov    %rsp,%r13
mov    0x2dae(%rip),%r15
lea    0xf86(%rip),%rbp
jmp    1255 <main+0xc5>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15),%edi
mov    %rbp,%rsi
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    127a <main+0xea>
mov    $0x1,%edi
lea    0xf2f(%rip),%rsi
call   1090 <err@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x22,%edx
call   1030 <write@plt>
cmp    $0x22,%eax
je     129f <main+0x10f>
mov    $0x1,%edi
lea    0xf10(%rip),%rsi
call   1090 <err@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
cmp    $0x22,%eax
jne    1240 <main+0xb0>
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1240 <main+0xb0>
jmp    124b <main+0xbb>

LLVM IR (-O0):
@main.flag = internal constant [34 x i8] c"CTF{D1ff3r3nT_D3v1Ce_S4mE_Tr1Ckz}\00"
@main.flag2 = internal constant [64 x i8] c"CTF{B3h1nD_S3vEn_D4v1C3s}\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@main.key = internal constant [256 x i8] c"flag\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [33 x i8] c"sysctl -w kernel.panic_on_oops=1\00"
@.str.1 = [14 x i8] c"/dev/pcivault\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"ioctl\00"
@.str.4 = [13 x i8] c"writing flag\00"
@stderr = global i32 0
@.str.5 = [23 x i8] c"Verifying flag failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [64 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [64 x i8]* %6 to i8*
%9 = call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %7
%18 = call i64 @ioctl(i32 %17, i32 0, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @main.flag2, i64 0, i64 0))
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
br label %23
23: ; preds = %22, %49
%24 = load i32, i32* %7
%25 = call i64 @ioctl(i32 %24, i32 1, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @main.key, i64 0, i64 0))
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = load i32, i32* %7
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @main.flag, i64 0, i64 0), i32 34)
%32 = sext i32 %31 to i64
%33 = icmp ne i64 %32, 34
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = load i32, i32* %7
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 64)
%40 = sext i32 %39 to i64
%41 = icmp ne i64 %40, 34
br i1 %41, label %46, label %42
42: ; preds = %36
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @main.flag, i64 0, i64 0))
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42, %36
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %46, %42
%50 = call i32 @sleep(i32 2)
br label %23
}

LLVM IR (-O2):
@main.flag = internal constant [34 x i8] c"CTF{D1ff3r3nT_D3v1Ce_S4mE_Tr1Ckz}\00"
@main.flag2 = internal constant [64 x i8] c"CTF{B3h1nD_S3vEn_D4v1C3s}\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@main.key = internal constant [256 x i8] c"flag\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [33 x i8] c"sysctl -w kernel.panic_on_oops=1\00"
@.str.1 = [14 x i8] c"/dev/pcivault\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"ioctl\00"
@.str.4 = [13 x i8] c"writing flag\00"
@stderr = global i32 0
@.str.5 = [23 x i8] c"Verifying flag failed\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i64 @ioctl(i32 %7, i32 0, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @main.flag2, i64 0, i64 0))
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
br label %17
17: ; preds = %16, %36
%18 = call i64 @ioctl(i32 %7, i32 1, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @main.key, i64 0, i64 0))
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = call i32 @write(i32 %7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @main.flag, i64 0, i64 0), i32 34)
%24 = icmp eq i32 %23, 34
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = call i32 @read(i32 %7, i8* %4, i32 64)
%29 = icmp eq i32 %28, 34
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @main.flag, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30, %27
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %33, %30
%37 = call i32 @sleep(i32 2)
br label %17
}