#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 creat(char *, int);
int /*<<< orphan*/ printf(char *);
int rand();
int snprintf(char *, int, char *, int, char, int);
int /*<<< orphan*/ srand(unsigned int);
int strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char *operator= "*+-/";
  int fd = 1;
  if (argc >= 2) {
    operator= argv[1];
    if (argc >= 3) {
      if ((fd = creat(argv[2], 0644)) == -1) {
        printf("create file error");
      }
    }
  }
  int size = strlen(operator);
  int left, right;
  char formula[30];
  char op;
  srand((unsigned)time(NULL));
  for (int i = 0, len = 0; i < 100; ++i) {
    op = operator[rand() % size];
    left = rand() % 100;
    if (op == '/') {
      right = rand() % 10 + 1;
    } else if (op == '*') {
      right = rand() % 10 + 1;
    } else if (op == '-') {
      right = rand() % (left + 1);
    } else {
      right = rand() % 100;
    }
    len = snprintf(formula, 30, "%2d %c %2d = \n", left, op, right);
    write(fd, formula, len);
  }
  return 0;
}


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)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11fc <main+0x7c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11f7 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11f2 <main+0x72>
lea    0xe18(%rip),%rdi
call   1050 <printf@plt>
jmp    11f7 <main+0x77>
jmp    11fc <main+0x7c>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0x64,-0x58(%rbp)
jge    1334 <main+0x1b4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
cltd
idivl  -0x20(%rbp)
mov    -0x68(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x51(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
movsbl -0x51(%rbp),%eax
cmp    $0x2f,%eax
jne    1289 <main+0x109>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x28(%rbp)
jmp    12f1 <main+0x171>
movsbl -0x51(%rbp),%eax
cmp    $0x2a,%eax
jne    12b0 <main+0x130>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x28(%rbp)
jmp    12ec <main+0x16c>
movsbl -0x51(%rbp),%eax
cmp    $0x2d,%eax
jne    12d5 <main+0x155>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
jmp    12e7 <main+0x167>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
jmp    12ec <main+0x16c>
jmp    12f1 <main+0x171>
lea    -0x50(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    -0x51(%rbp),%al
mov    -0x28(%rbp),%r9d
mov    $0x1e,%esi
lea    0xd0c(%rip),%rdx
movsbl %al,%r8d
call   1060 <snprintf@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x5c(%rbp),%edx
call   1030 <write@plt>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    1226 <main+0xa6>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movl   $0x1,0xc(%rsp)
cmp    $0x2,%edi
jl     11ce <main+0x4e>
mov    0x8(%rsi),%rbx
je     11d5 <main+0x55>
mov    0x10(%rsi),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
jne    11d5 <main+0x55>
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,0xc(%rsp)
ff
jmp    11d5 <main+0x55>
lea    0xe2b(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x64,%r15d
lea    0x10(%rsp),%rbp
jmp    1261 <main+0xe1>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%r9d
add    $0x1,%r9d
movsbl %r14b,%r8d
mov    %rbp,%rdi
mov    $0x1e,%esi
lea    0xdd8(%rip),%rdx
mov    %ebx,%ecx
call   1060 <snprintf@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0xffffffff,%r15d
mov    %r12,%rbx
je     12f5 <main+0x175>
xor    %eax,%eax
call   10a0 <rand@plt>
cltd
idiv   %r13d
movslq %edx,%rax
mov    %rbx,%r12
movzbl (%rbx,%rax,1),%r14d
xor    %eax,%eax
call   10a0 <rand@plt>
movslq %eax,%rbx
imul   $0x51eb851f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%ebx
cmp    $0x2a,%r14b
je     1200 <main+0x80>
cmp    $0x2d,%r14b
je     12e0 <main+0x160>
cmp    $0x2f,%r14b
je     1200 <main+0x80>
xor    %eax,%eax
call   10a0 <rand@plt>
movslq %eax,%r9
imul   $0x51eb851f,%r9,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%r9d
jmp    122c <main+0xac>
nopl   (%rax)
xor    %eax,%eax
call   10a0 <rand@plt>
lea    0x1(%rbx),%ecx
cltd
idiv   %ecx
mov    %edx,%r9d
jmp    122c <main+0xac>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"*+-/\00"
@.str.1 = [18 x i8] c"create file error\00"
@.str.2 = [15 x i8] c"%2d %c %2d = \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [30 x i8]
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 1, i32* %7
%15 = load i32, i32* %4
%16 = icmp sge i32 %15, 2
br i1 %16, label %17, label %33
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i32, i32* %4
%22 = icmp sge i32 %21, 3
br i1 %22, label %23, label %32
23: ; preds = %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @creat(i8* %26, i32 420)
store i32 %27, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
br label %32
32: ; preds = %31, %17
br label %33
33: ; preds = %32, %2
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %8
%36 = call i64 @time(i32* null)
%37 = trunc i64 %36 to i32
%38 = call i32 @srand(i32 %37)
store i32 0, i32* %13
store i32 0, i32* %14
br label %39
39: ; preds = %91, %33
%40 = load i32, i32* %13
%41 = icmp slt i32 %40, 100
br i1 %41, label %42, label %94
42: ; preds = %39
%43 = load i8*, i8** %6
%44 = call i32 (...) @rand()
%45 = load i32, i32* %8
%46 = srem i32 %44, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %43, i64 %47
%49 = load i8, i8* %48
store i8 %49, i8* %12
%50 = call i32 (...) @rand()
%51 = srem i32 %50, 100
store i32 %51, i32* %9
%52 = load i8, i8* %12
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 47
br i1 %54, label %55, label %59
55: ; preds = %42
%56 = call i32 (...) @rand()
%57 = srem i32 %56, 10
%58 = add nsw i32 %57, 1
store i32 %58, i32* %10
br label %81
59: ; preds = %42
%60 = load i8, i8* %12
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 42
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = call i32 (...) @rand()
%65 = srem i32 %64, 10
%66 = add nsw i32 %65, 1
store i32 %66, i32* %10
br label %80
67: ; preds = %59
%68 = load i8, i8* %12
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 45
br i1 %70, label %71, label %76
71: ; preds = %67
%72 = call i32 (...) @rand()
%73 = load i32, i32* %9
%74 = add nsw i32 %73, 1
%75 = srem i32 %72, %74
store i32 %75, i32* %10
br label %79
76: ; preds = %67
%77 = call i32 (...) @rand()
%78 = srem i32 %77, 100
store i32 %78, i32* %10
br label %79
79: ; preds = %76, %71
br label %80
80: ; preds = %79, %63
br label %81
81: ; preds = %80, %55
%82 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%83 = load i32, i32* %9
%84 = load i8, i8* %12
%85 = load i32, i32* %10
%86 = call i32 @snprintf(i8* %82, i32 30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %83, i8 signext %84, i32 %85)
store i32 %86, i32* %14
%87 = load i32, i32* %7
%88 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%89 = load i32, i32* %14
%90 = call i32 @write(i32 %87, i8* %88, i32 %89)
br label %91
91: ; preds = %81
%92 = load i32, i32* %13
%93 = add nsw i32 %92, 1
store i32 %93, i32* %13
br label %39
94: ; preds = %39
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"*+-/\00"
@.str.1 = [18 x i8] c"create file error\00"
@.str.2 = [15 x i8] c"%2d %c %2d = \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i32 %0, 2
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @creat(i8* %11, i32 420)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %5, %14, %9, %2
%17 = phi i32 [ -1, %14 ], [ %12, %9 ], [ 1, %5 ], [ 1, %2 ]
%18 = phi i8* [ %7, %14 ], [ %7, %9 ], [ %7, %5 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
%19 = tail call i32 @strlen(i8* %18)
%20 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%21 = tail call i64 @time(i32* null)
%22 = trunc i64 %21 to i32
%23 = tail call i32 @srand(i32 %22)
br label %25
24: ; preds = %49
ret i32 0
25: ; preds = %16, %49
%26 = phi i32 [ 0, %16 ], [ %53, %49 ]
%27 = call i32 (...) @rand()
%28 = srem i32 %27, %19
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %18, i64 %29
%31 = load i8, i8* %30
%32 = call i32 (...) @rand()
%33 = srem i32 %32, 100
switch i8 %31, label %46 [
i8 47, label %34
i8 42, label %38
i8 45, label %42
]
34: ; preds = %25
%35 = call i32 (...) @rand()
%36 = srem i32 %35, 10
%37 = add nsw i32 %36, 1
br label %49
38: ; preds = %25
%39 = call i32 (...) @rand()
%40 = srem i32 %39, 10
%41 = add nsw i32 %40, 1
br label %49
42: ; preds = %25
%43 = call i32 (...) @rand()
%44 = add nsw i32 %33, 1
%45 = srem i32 %43, %44
br label %49
46: ; preds = %25
%47 = call i32 (...) @rand()
%48 = srem i32 %47, 100
br label %49
49: ; preds = %38, %46, %42, %34
%50 = phi i32 [ %37, %34 ], [ %41, %38 ], [ %45, %42 ], [ %48, %46 ]
%51 = call i32 @snprintf(i8* %20, i32 30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %33, i8 signext %31, i32 %50)
%52 = call i32 @write(i32 %17, i8* %20, i32 %51)
%53 = add nuw nsw i32 %26, 1
%54 = icmp eq i32 %53, 100
br i1 %54, label %24, label %25
}