#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*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fsync(int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char next_letter();
int rand();
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ system(char *);
void chaff_msg(int difficulty, unsigned char *message) {
  FILE *output = stdout;
  unsigned char next_msg_char = 0;
  unsigned char next_chaff = 0;
  int ix = 0;
  next_msg_char = message[ix];
  while (next_msg_char != 0) {
    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);
      ++ix;
      next_msg_char = message[ix];
    }
  }
  fsync(fileno(output));
  system("sync; sync;");
  return;
}


x86 assembly (clang, -O0):
<chaff_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x0,-0x1a(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     1213 <chaff_msg+0xb3>
mov    $0x0,%al
call   1080 <rand@plt>
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11e3 <chaff_msg+0x83>
mov    $0x0,%al
call   1030 <next_letter@plt>
mov    %al,-0x1a(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x1a(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
jmp    120e <chaff_msg+0xae>
mov    -0x18(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x19(%rbp)
jmp    119a <chaff_msg+0x3a>
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
call   1060 <fsync@plt>
lea    0xdd6(%rip),%rdi
call   1040 <system@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaff_msg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%r15
mov    (%rsi),%bl
test   %bl,%bl
je     11d1 <chaff_msg+0x71>
mov    %rsi,%r14
mov    %edi,%r12d
add    $0x1,%r12d
xor    %ebp,%ebp
jmp    11ac <chaff_msg+0x4c>
cs nopw 0x0(%rax,%rax,1)
movsbl %bl,%edi
mov    %r15,%rsi
call   1050 <fputc@plt>
movslq %ebp,%rbp
movzbl 0x1(%r14,%rbp,1),%ebx
add    $0x1,%rbp
test   %bl,%bl
je     11d1 <chaff_msg+0x71>
xor    %eax,%eax
call   1080 <rand@plt>
cltd
idiv   %r12d
test   %edx,%edx
je     1190 <chaff_msg+0x30>
xor    %eax,%eax
call   1030 <next_letter@plt>
movsbl %al,%edi
mov    %r15,%rsi
call   1050 <fputc@plt>
test   %bl,%bl
jne    11ac <chaff_msg+0x4c>
mov    %r15,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1070 <fsync@plt>
lea    0xe19(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <system@plt>

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

LLVM IR (-O2):
@stdout = global i32* null
@.str = [12 x i8] c"sync; sync;\00"
define void @chaff_msg(i32 %0, i8* nocapture %1) {
%3 = load i32*, i32** @stdout
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %2
%7 = add nsw i32 %0, 1
br label %8
8: ; preds = %6, %25
%9 = phi i32 [ 0, %6 ], [ %27, %25 ]
%10 = phi i8 [ %4, %6 ], [ %26, %25 ]
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, %7
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = tail call zeroext i8 (...) @next_letter()
%16 = sext i8 %15 to i32
%17 = tail call i32 @fputc(i32 %16, i32* %3)
br label %25
18: ; preds = %8
%19 = sext i8 %10 to i32
%20 = tail call i32 @fputc(i32 %19, i32* %3)
%21 = add nsw i32 %9, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
%24 = load i8, i8* %23
br label %25
25: ; preds = %18, %14
%26 = phi i8 [ %24, %18 ], [ %10, %14 ]
%27 = phi i32 [ %21, %18 ], [ %9, %14 ]
%28 = icmp eq i8 %26, 0
br i1 %28, label %29, label %8
29: ; preds = %25, %2
%30 = tail call i32 @fileno(i32* %3)
%31 = tail call i32 @fsync(i32 %30)
%32 = tail call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
ret void
}