#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
double RAND_MAX;
long atol(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
double rand();
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ *stderr;
scalar_t__ strlen(char *);
int main(int argc, char **argv) {
  if (argc != 4) {
    fprintf(stderr, "ERROR. Takes 3 arguments: bits, rules, filename\n");
    exit(EXIT_FAILURE);
  }
  long bits = atol(argv[1]);
  long rules = atol(argv[2]);
  char *filename = argv[3];
  if (bits <= 0) {
    fprintf(stderr, "ERROR. bits must be > 0, got %ld\n", bits);
    exit(EXIT_FAILURE);
  }
  if (rules <= 0) {
    fprintf(stderr, "ERROR. rules must be > 0, got %ld\n", rules);
    exit(EXIT_FAILURE);
  }
  if (strlen(filename) == 0) {
    fprintf(stderr, "ERROR. filename must be nonzero length.\n");
    exit(EXIT_FAILURE);
  }
  struct timeval seed;
  gettimeofday(&seed, NULL);
  srand(seed.tv_sec * seed.tv_usec);
  FILE *outfile = fopen(filename, "w");
  fprintf(outfile, "%d\n", bits);
  int i, j;
  int random_rule;
  for (i = 0; i < rules; i++) {
    for (j = 0; j < bits; j++) {
      random_rule = (int)((3.0 * rand()) / (RAND_MAX + 1.0));
      switch (random_rule) {
      case 0:
        fputc('0', outfile);
        break;
      case 1:
        fputc('1', outfile);
        break;
      case 2:
        fputc('?', outfile);
        break;
      default:
        fputc((char)48 + random_rule, outfile);
      }
    }
    fputc('\n', outfile);
  }
  fclose(outfile);
  return EXIT_SUCCESS;
}


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   $0x4,-0x8(%rbp)
je     11e6 <main+0x46>
mov    0x2e11(%rip),%rax
mov    (%rax),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     1249 <main+0xa9>
mov    0x2db2(%rip),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jg     127e <main+0xde>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    12b7 <main+0x117>
mov    0x2d40(%rip),%rax
mov    (%rax),%rdi
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x30(%rbp),%edi
imul   -0x2c(%rbp),%edi
call   1070 <srand@plt>
mov    -0x28(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1408 <main+0x268>
movl   $0x0,-0x40(%rbp)
movslq -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    13ec <main+0x24c>
xor    %eax,%eax
call   10c0 <rand@plt>
movsd  0xccd(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    0x2caa(%rip),%rax
movsd  (%rax),%xmm1
movsd  0xcbe(%rip),%xmm2
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
test   %eax,%eax
je     138e <main+0x1ee>
jmp    136c <main+0x1cc>
mov    -0x48(%rbp),%eax
sub    $0x1,%eax
je     13a1 <main+0x201>
jmp    137d <main+0x1dd>
mov    -0x48(%rbp),%eax
sub    $0x2,%eax
je     13b4 <main+0x214>
jmp    13c7 <main+0x227>
mov    -0x38(%rbp),%rsi
mov    $0x30,%edi
call   1060 <fputc@plt>
jmp    13d9 <main+0x239>
mov    -0x38(%rbp),%rsi
mov    $0x31,%edi
call   1060 <fputc@plt>
jmp    13d9 <main+0x239>
mov    -0x38(%rbp),%rsi
mov    $0x3f,%edi
call   1060 <fputc@plt>
jmp    13d9 <main+0x239>
mov    -0x44(%rbp),%eax
add    $0x30,%eax
mov    -0x38(%rbp),%rsi
movsbl %al,%edi
call   1060 <fputc@plt>
jmp    13de <main+0x23e>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1316 <main+0x176>
mov    -0x38(%rbp),%rsi
mov    $0xa,%edi
call   1060 <fputc@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1301 <main+0x161>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2bb0(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    12d6 <main+0x136>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atol@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
call   1080 <atol@plt>
test   %r14,%r14
jle    12ee <main+0x14e>
mov    %rax,%r15
test   %rax,%rax
jle    1304 <main+0x164>
mov    0x18(%rbx),%rbx
cmpb   $0x0,(%rbx)
je     1321 <main+0x181>
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x4(%rsp),%edi
imul   (%rsp),%edi
call   1060 <srand@plt>
lea    0xea3(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xe93(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2db6(%rip),%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10c0 <rand@plt>
mulsd  0xda1(%rip),%xmm0
movsd  (%r12),%xmm1
addsd  0xd9b(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
lea    0x30(%rax),%edx
lea    0x0(,%rax,8),%ecx
mov    $0x3f3130,%esi
shr    %cl,%esi
cmp    $0x3,%eax
movzbl %dl,%eax
cmovb  %esi,%eax
movsbl %al,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0xffffffffffffffff,%rbp
jne    1250 <main+0xb0>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%r13
cmp    %r15,%r13
jne    1240 <main+0xa0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cfb(%rip),%rax
mov    (%rax),%rcx
lea    0xd29(%rip),%rdi
mov    $0x30,%esi
jmp    1337 <main+0x197>
mov    0x2ce3(%rip),%rax
mov    (%rax),%rdi
lea    0xd42(%rip),%rsi
mov    %r14,%rdx
jmp    1318 <main+0x178>
mov    0x2ccd(%rip),%rax
mov    (%rax),%rdi
lea    0xd4e(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1341 <main+0x1a1>
mov    0x2cb0(%rip),%rax
mov    (%rax),%rcx
lea    0xd54(%rip),%rdi
mov    $0x28,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@stderr = global i32* null
@.str = [49 x i8] c"ERROR. Takes 3 arguments: bits, rules, filename\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"ERROR. bits must be > 0, got %ld\0A\00"
@.str.2 = [35 x i8] c"ERROR. rules must be > 0, got %ld\0A\00"
@.str.3 = [41 x i8] c"ERROR. filename must be nonzero length.\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@RAND_MAX = global double 0.000000e+00
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.timeval
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
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, 4
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32*, i32** @stderr
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i64 @atol(i8* %24)
store i64 %25, i64* %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i64 @atol(i8* %28)
store i64 %29, i64* %7
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
store i8* %32, i8** %8
%33 = load i64, i64* %6
%34 = icmp sle i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i32*, i32** @stderr
%37 = load i64, i64* %6
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %21
%42 = load i64, i64* %7
%43 = icmp sle i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32*, i32** @stderr
%46 = load i64, i64* %7
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %41
%51 = load i8*, i8** %8
%52 = call i64 @strlen(i8* %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = load i32*, i32** @stderr
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %50
%60 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%64 = load i32, i32* %63
%65 = mul nsw i32 %62, %64
%66 = call i32 @srand(i32 %65)
%67 = load i8*, i8** %8
%68 = call i32* @fopen(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %68, i32** %10
%69 = load i32*, i32** %10
%70 = load i64, i64* %6
%71 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64 %70)
store i32 0, i32* %11
br label %72
72: ; preds = %113, %59
%73 = load i32, i32* %11
%74 = sext i32 %73 to i64
%75 = load i64, i64* %7
%76 = icmp slt i64 %74, %75
br i1 %76, label %77, label %116
77: ; preds = %72
store i32 0, i32* %12
br label %78
78: ; preds = %107, %77
%79 = load i32, i32* %12
%80 = sext i32 %79 to i64
%81 = load i64, i64* %6
%82 = icmp slt i64 %80, %81
br i1 %82, label %83, label %110
83: ; preds = %78
%84 = call double (...) @rand()
%85 = fmul double 3.000000e+00, %84
%86 = load double, double* @RAND_MAX
%87 = fadd double %86, 1.000000e+00
%88 = fdiv double %85, %87
%89 = fptosi double %88 to i32
store i32 %89, i32* %13
%90 = load i32, i32* %13
switch i32 %90, label %100 [
i32 0, label %91
i32 1, label %94
i32 2, label %97
]
91: ; preds = %83
%92 = load i32*, i32** %10
%93 = call i32 @fputc(i8 signext 48, i32* %92)
br label %106
94: ; preds = %83
%95 = load i32*, i32** %10
%96 = call i32 @fputc(i8 signext 49, i32* %95)
br label %106
97: ; preds = %83
%98 = load i32*, i32** %10
%99 = call i32 @fputc(i8 signext 63, i32* %98)
br label %106
100: ; preds = %83
%101 = load i32, i32* %13
%102 = add nsw i32 48, %101
%103 = trunc i32 %102 to i8
%104 = load i32*, i32** %10
%105 = call i32 @fputc(i8 signext %103, i32* %104)
br label %106
106: ; preds = %100, %97, %94, %91
br label %107
107: ; preds = %106
%108 = load i32, i32* %12
%109 = add nsw i32 %108, 1
store i32 %109, i32* %12
br label %78
110: ; preds = %78
%111 = load i32*, i32** %10
%112 = call i32 @fputc(i8 signext 10, i32* %111)
br label %113
113: ; preds = %110
%114 = load i32, i32* %11
%115 = add nsw i32 %114, 1
store i32 %115, i32* %11
br label %72
116: ; preds = %72
%117 = load i32*, i32** %10
%118 = call i32 @fclose(i32* %117)
%119 = load i32, i32* @EXIT_SUCCESS
ret i32 %119
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@stderr = global i32* null
@.str = [49 x i8] c"ERROR. Takes 3 arguments: bits, rules, filename\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"ERROR. bits must be > 0, got %ld\0A\00"
@.str.2 = [35 x i8] c"ERROR. rules must be > 0, got %ld\0A\00"
@.str.3 = [41 x i8] c"ERROR. filename must be nonzero length.\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@RAND_MAX = global double 0.000000e+00
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = icmp eq i32 %0, 4
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32*, i32** @stderr
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i64 48, i64 1, i32* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @atol(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i64 @atol(i8* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = icmp slt i64 %13, 1
br i1 %19, label %20, label %25
20: ; preds = %10
%21 = load i32*, i32** @stderr
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %13)
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %10
%26 = icmp slt i64 %16, 1
br i1 %26, label %27, label %32
27: ; preds = %25
%28 = load i32*, i32** @stderr
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %16)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %25
%33 = load i8, i8* %18
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32*, i32** @stderr
%37 = tail call i64 @fwrite(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i64 40, i64 1, i32* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = bitcast %struct.timeval* %3 to i8*
%42 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%46 = load i32, i32* %45
%47 = mul nsw i32 %46, %44
%48 = tail call i32 @srand(i32 %47)
%49 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%50 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64 %13)
br label %51
51: ; preds = %40, %72
%52 = phi i64 [ 0, %40 ], [ %74, %72 ]
br label %53
53: ; preds = %51, %53
%54 = phi i64 [ 0, %51 ], [ %70, %53 ]
%55 = tail call double (...) @rand()
%56 = fmul double %55, 3.000000e+00
%57 = load double, double* @RAND_MAX
%58 = fadd double %57, 1.000000e+00
%59 = fdiv double %56, %58
%60 = fptosi double %59 to i32
%61 = icmp ult i32 %60, 3
%62 = trunc i32 %60 to i8
%63 = add i8 %62, 48
%64 = trunc i32 %60 to i24
%65 = shl i24 %64, 3
%66 = lshr i24 4141360, %65
%67 = trunc i24 %66 to i8
%68 = select i1 %61, i8 %67, i8 %63
%69 = tail call i32 @fputc(i8 signext %68, i32* %49)
%70 = add nuw nsw i64 %54, 1
%71 = icmp eq i64 %70, %13
br i1 %71, label %72, label %53
72: ; preds = %53
%73 = tail call i32 @fputc(i8 signext 10, i32* %49)
%74 = add nuw nsw i64 %52, 1
%75 = icmp eq i64 %74, %16
br i1 %75, label %76, label %51
76: ; preds = %72
%77 = tail call i32 @fclose(i32* %49)
%78 = load i32, i32* @EXIT_SUCCESS
ret i32 %78
}