#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*/ next_msg_char;
typedef int /*<<< orphan*/ next_chaff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fsync(int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char next_letter();
int rand();
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ system(char *);
void chaff_file(int difficulty, char *infile) {
  FILE *output = stdout;
  FILE *input;
  int closein = 0;
  unsigned char next_msg_char = 0;
  unsigned char next_char = 0;
  unsigned char next_chaff = 0;
  if (strcmp(infile, "stdin") != 0) {
    input = fopen(infile, "r");
    closein = 1;
  } else
    input = stdin;
  fread(&next_msg_char, sizeof(next_msg_char), 1, input);
  while (!feof(input) && !ferror(input)) {
    if ((rand() % (difficulty + 1)) != 0) {
      next_chaff = next_letter();
      fwrite(&next_chaff, sizeof(next_chaff), 1, output);
    } else {
      fwrite(&next_msg_char, sizeof(next_msg_char), 1, output);
      fread(&next_msg_char, sizeof(next_msg_char), 1, input);
    }
  }
  if (closein)
    fclose(input);
  fsync(fileno(output));
  system("sync; sync;");
  return;
}


x86 assembly (clang, -O0):
<chaff_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x0,-0x25(%rbp)
movb   $0x0,-0x26(%rbp)
movb   $0x0,-0x27(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     122a <chaff_file+0x6a>
mov    -0x10(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1238 <chaff_file+0x78>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x25(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1090 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x28(%rbp)
jne    1279 <chaff_file+0xb9>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x28(%rbp)
mov    -0x28(%rbp),%al
test   $0x1,%al
jne    1289 <chaff_file+0xc9>
jmp    12f2 <chaff_file+0x132>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12c5 <chaff_file+0x105>
mov    $0x0,%al
call   1050 <next_letter@plt>
mov    %al,-0x27(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x27(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10d0 <fwrite@plt>
jmp    12ed <chaff_file+0x12d>
mov    -0x18(%rbp),%rcx
lea    -0x25(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10d0 <fwrite@plt>
mov    -0x20(%rbp),%rcx
lea    -0x25(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
jmp    124c <chaff_file+0x8c>
cmpl   $0x0,-0x24(%rbp)
je     1305 <chaff_file+0x145>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
call   10b0 <fsync@plt>
lea    0xcec(%rip),%rdi
call   1070 <system@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaff_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e00(%rip),%rax
mov    (%rax),%r15
movb   $0x0,0xf(%rsp)
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
mov    %rax,%r14
test   %rax,%rax
je     120b <chaff_file+0x4b>
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
mov    %rax,%rbx
jmp    1215 <chaff_file+0x55>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
je     126c <chaff_file+0xac>
test   %r14,%r14
je     1245 <chaff_file+0x85>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   10c0 <fsync@plt>
lea    0xdad(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <system@plt>
add    $0x1,%ebp
lea    0xf(%rsp),%r12
jmp    12ae <chaff_file+0xee>
cs nopw 0x0(%rax,%rax,1)
movsbl 0xf(%rsp),%edi
mov    %r15,%rsi
call   1080 <fputc@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
jne    1238 <chaff_file+0x78>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
jne    1238 <chaff_file+0x78>
xor    %eax,%eax
call   10e0 <rand@plt>
cltd
idiv   %ebp
test   %edx,%edx
je     1280 <chaff_file+0xc0>
xor    %eax,%eax
call   1050 <next_letter@plt>
movsbl %al,%edi
mov    %r15,%rsi
call   1080 <fputc@plt>
jmp    12a2 <chaff_file+0xe2>

LLVM IR (-O0):
@stdout = global i32* null
@.str = [6 x i8] c"stdin\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.2 = [12 x i8] c"sync; sync;\00"
define void @chaff_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
store i32 %0, i32* %3
store i8* %1, i8** %4
%11 = load i32*, i32** @stdout
store i32* %11, i32** %5
store i32 0, i32* %7
store i8 0, i8* %8
store i8 0, i8* %9
store i8 0, i8* %10
%12 = load i8*, i8** %4
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %6
store i32 1, i32* %7
br label %20
18: ; preds = %2
%19 = load i32*, i32** @stdin
store i32* %19, i32** %6
br label %20
20: ; preds = %18, %15
%21 = load i32*, i32** %6
%22 = call i32 @fread(i8* %8, i32 1, i32 1, i32* %21)
br label %23
23: ; preds = %49, %20
%24 = load i32*, i32** %6
%25 = call i32 @feof(i32* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32*, i32** %6
%29 = call i32 @ferror(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br label %32
32: ; preds = %27, %23
%33 = phi i1 [ false, %23 ], [ %31, %27 ]
br i1 %33, label %34, label %50
34: ; preds = %32
%35 = call i32 (...) @rand()
%36 = load i32, i32* %3
%37 = add nsw i32 %36, 1
%38 = srem i32 %35, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = call zeroext i8 (...) @next_letter()
store i8 %41, i8* %10
%42 = load i32*, i32** %5
%43 = call i32 @fwrite(i8* %10, i32 1, i32 1, i32* %42)
br label %49
44: ; preds = %34
%45 = load i32*, i32** %5
%46 = call i32 @fwrite(i8* %8, i32 1, i32 1, i32* %45)
%47 = load i32*, i32** %6
%48 = call i32 @fread(i8* %8, i32 1, i32 1, i32* %47)
br label %49
49: ; preds = %44, %40
br label %23
50: ; preds = %32
%51 = load i32, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %50
%57 = load i32*, i32** %5
%58 = call i32 @fileno(i32* %57)
%59 = call i32 @fsync(i32 %58)
%60 = call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@stdout = global i32* null
@.str = [6 x i8] c"stdin\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.2 = [12 x i8] c"sync; sync;\00"
define void @chaff_file(i32 %0, i8* %1) {
%3 = alloca i8
%4 = load i32*, i32** @stdout
store i8 0, i8* %3
%5 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %11
9: ; preds = %2
%10 = load i32*, i32** @stdin
br label %11
11: ; preds = %9, %7
%12 = phi i32* [ %8, %7 ], [ %10, %9 ]
%13 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %12)
%14 = tail call i32 @feof(i32* %12)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %11
%17 = add nsw i32 %0, 1
br label %18
18: ; preds = %16, %34
%19 = tail call i32 @ferror(i32* %12)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = tail call i32 (...) @rand()
%23 = srem i32 %22, %17
%24 = icmp eq i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = tail call zeroext i8 (...) @next_letter()
%27 = sext i8 %26 to i32
%28 = tail call i32 @fputc(i32 %27, i32* %4)
br label %34
29: ; preds = %21
%30 = load i8, i8* %3
%31 = sext i8 %30 to i32
%32 = tail call i32 @fputc(i32 %31, i32* %4)
%33 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %12)
br label %34
34: ; preds = %29, %25
%35 = tail call i32 @feof(i32* %12)
%36 = icmp eq i32 %35, 0
br i1 %36, label %18, label %37
37: ; preds = %18, %34, %11
br i1 %6, label %40, label %38
38: ; preds = %37
%39 = tail call i32 @fclose(i32* %12)
br label %40
40: ; preds = %38, %37
%41 = tail call i32 @fileno(i32* %4)
%42 = tail call i32 @fsync(i32 %41)
%43 = tail call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
ret void
}