#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*/ buffer;

/* Variables and functions */
int atoi(char const *const);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int rand();
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ srand(unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ time(int /*<<< orphan*/ *);
int main(int argc, const char *const *argv) {
  char buffer[32] = {0};
  int curLen = 0;
  const int n = (argc > 1) ? atoi(argv[1]) : 1000;
  srand((unsigned int)time(NULL));
  while (curLen < n) {
    int len = snprintf(buffer, sizeof(buffer), "%d", rand());
    if (len < 0) {
      fputs("output error.\n", stderr);
      return -1;
    }
    if (len + curLen > n) {
      len = n - curLen;
    }
    fwrite(buffer, 1, len, stdout);
    curLen += len;
  }
  return 0;
}


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)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
movl   $0x0,-0x34(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11cc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x40(%rbp)
jmp    11d9 <main+0x59>
mov    $0x3e8,%eax
mov    %eax,-0x40(%rbp)
jmp    11d9 <main+0x59>
mov    -0x40(%rbp),%eax
mov    %eax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    128f <main+0x10f>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    $0x20,%esi
lea    0xde4(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    124f <main+0xcf>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdi
call   1040 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <main+0x116>
mov    -0x3c(%rbp),%eax
add    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jle    1267 <main+0xe7>
mov    -0x38(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x3c(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11ef <main+0x6f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    $0x3e8,%r13d
cmp    $0x2,%edi
jl     11a1 <main+0x31>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r13d
xor    %r14d,%r14d
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
test   %r13d,%r13d
jle    1237 <main+0xc7>
lea    0xe3e(%rip),%r14
mov    %rsp,%r15
mov    0x2e0c(%rip),%r12
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1090 <rand@plt>
mov    %r15,%rdi
mov    $0x20,%esi
mov    %r14,%rdx
mov    %eax,%ecx
call   1030 <snprintf@plt>
test   %eax,%eax
js     121c <main+0xac>
lea    (%rax,%rbx,1),%ecx
mov    %r13d,%ebp
sub    %ebx,%ebp
cmp    %r13d,%ecx
cmovle %eax,%ebp
mov    (%r12),%ecx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1080 <fwrite@plt>
add    %ebx,%ebp
mov    %ebp,%ebx
cmp    %ebp,%r13d
jg     11d0 <main+0x60>
xor    %r14d,%r14d
jmp    1237 <main+0xc7>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0xdd7(%rip),%rdi
call   1040 <fputs@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [15 x i8] c"output error.\0A\00"
@stderr = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [32 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [32 x i8]* %6 to i8*
store i32 0, i32* %7
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
br label %19
18: ; preds = %2
br label %19
19: ; preds = %18, %13
%20 = phi i32 [ %17, %13 ], [ 1000, %18 ]
store i32 %20, i32* %8
%21 = call i64 @time(i32* null)
%22 = trunc i64 %21 to i32
%23 = call i32 @srand(i32 %22)
br label %24
24: ; preds = %47, %19
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %55
28: ; preds = %24
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%30 = call i32 (...) @rand()
%31 = call i32 @snprintf(i8* %29, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %35)
store i32 -1, i32* %3
br label %56
37: ; preds = %28
%38 = load i32, i32* %9
%39 = load i32, i32* %7
%40 = add nsw i32 %38, %39
%41 = load i32, i32* %8
%42 = icmp sgt i32 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load i32, i32* %8
%45 = load i32, i32* %7
%46 = sub nsw i32 %44, %45
store i32 %46, i32* %9
br label %47
47: ; preds = %43, %37
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* %9
%50 = load i32, i32* @stdout
%51 = call i32 @fwrite(i8* %48, i32 1, i32 %49, i32 %50)
%52 = load i32, i32* %9
%53 = load i32, i32* %7
%54 = add nsw i32 %53, %52
store i32 %54, i32* %7
br label %24
55: ; preds = %24
store i32 0, i32* %3
br label %56
56: ; preds = %55, %34
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [15 x i8] c"output error.\0A\00"
@stderr = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
br label %10
10: ; preds = %2, %6
%11 = phi i32 [ %9, %6 ], [ 1000, %2 ]
%12 = tail call i64 @time(i32* null)
%13 = trunc i64 %12 to i32
%14 = tail call i32 @srand(i32 %13)
%15 = icmp sgt i32 %11, 0
br i1 %15, label %16, label %33
16: ; preds = %10, %24
%17 = phi i32 [ %31, %24 ], [ 0, %10 ]
%18 = call i32 (...) @rand()
%19 = call i32 @snprintf(i8* %4, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %33
24: ; preds = %16
%25 = add nsw i32 %19, %17
%26 = icmp sgt i32 %25, %11
%27 = sub nsw i32 %11, %17
%28 = select i1 %26, i32 %27, i32 %19
%29 = load i32, i32* @stdout
%30 = call i32 @fwrite(i8* %4, i32 1, i32 %28, i32 %29)
%31 = add nsw i32 %28, %17
%32 = icmp sgt i32 %11, %31
br i1 %32, label %16, label %33
33: ; preds = %24, %10, %21
%34 = phi i32 [ -1, %21 ], [ 0, %10 ], [ 0, %24 ]
ret i32 %34
}