#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 */
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, void *, int);
int main(int argc, char *argv[]) {
  char nl = '\012';
  unsigned char schar;
  int counter = 0;
  int chars_per_delay;
  int seconds;
  int save;
  if (3 != argc) {
    printf("usage: delayecho <chars_per_delay> <seconds>\n");
    exit(1);
  }
  chars_per_delay = atoi(argv[1]);
  seconds = atoi(argv[2]);
  save = fcntl(0, F_GETFL);
  fcntl(0, F_SETFL, save | O_NONBLOCK);
  while (1) {
    int j;
    int k;
    j = read(0, (void *)&schar, 1);
    if (j == -1) {
      if (errno != EAGAIN) {
        printf("errno = %d\n", errno);
        exit(1);
      }
      continue;
    }
    if (j == 0) {
      exit(1);
    }
    if (counter++ == chars_per_delay) {
      sleep(seconds);
      counter = 0;
    }
    write(1, (void *)&schar, 1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0xa,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x3,%eax
cmp    -0x8(%rbp),%eax
je     11b7 <main+0x47>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    0x2de9(%rip),%rax
or     (%rax),%edx
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
xor    %edi,%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1268 <main+0xf8>
mov    0x2da2(%rip),%rax
mov    (%rax),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
je     1263 <main+0xf3>
mov    0x2d88(%rip),%rax
mov    (%rax),%rsi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    120a <main+0x9a>
cmpl   $0x0,-0x28(%rbp)
jne    127c <main+0x10c>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
cmp    -0x1c(%rbp),%eax
jne    129f <main+0x12f>
mov    -0x20(%rbp),%edi
call   1090 <sleep@plt>
movl   $0x0,-0x18(%rbp)
mov    $0x1,%edx
lea    -0x12(%rbp),%rsi
mov    %edx,%edi
call   1040 <write@plt>
jmp    120a <main+0x9a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1263 <main+0xe3>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
mov    0x10(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %r12d,%r12d
xor    %edi,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e14(%rip),%rcx
or     (%rcx),%eax
xor    %edi,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
lea    0x7(%rsp),%rbp
mov    0x2de3(%rip),%r13
mov    0x2dc4(%rip),%rbx
jmp    1209 <main+0x89>
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%rsi
cmp    (%rbx),%rsi
jne    124b <main+0xcb>
xor    %edi,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1200 <main+0x80>
test   %eax,%eax
je     1259 <main+0xd9>
cmp    %r15d,%r12d
jne    1233 <main+0xb3>
mov    %r14d,%edi
call   10a0 <sleep@plt>
xor    %r12d,%r12d
jmp    1237 <main+0xb7>
add    $0x1,%r12d
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    1209 <main+0x89>
lea    0xdae(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xda2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [46 x i8] c"usage: delayecho <chars_per_delay> <seconds>\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [12 x i8] c"errno = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%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
store i8 10, i8* %6
store i32 0, i32* %8
%14 = load i32, i32* %4
%15 = icmp ne i32 3, %14
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %9
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %10
%28 = load i32, i32* @F_GETFL
%29 = call i32 (i32, i32, ...) @fcntl(i32 0, i32 %28)
store i32 %29, i32* %11
%30 = load i32, i32* @F_SETFL
%31 = load i32, i32* %11
%32 = load i32, i32* @O_NONBLOCK
%33 = or i32 %31, %32
%34 = call i32 (i32, i32, ...) @fcntl(i32 0, i32 %30, i32 %33)
br label %35
35: ; preds = %19, %47, %61
%36 = call i32 @read(i32 0, i8* %7, i32 1)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %39
br label %35
48: ; preds = %35
%49 = load i32, i32* %12
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %48
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
%56 = load i32, i32* %9
%57 = icmp eq i32 %54, %56
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* %10
%60 = call i32 @sleep(i32 %59)
store i32 0, i32* %8
br label %61
61: ; preds = %58, %53
%62 = call i32 @write(i32 1, i8* %7, i32 1)
br label %35
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [12 x i8] c"errno = %d\0A\00"
@str = [45 x i8] c"usage: delayecho <chars_per_delay> <seconds>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = load i32, i32* @F_GETFL
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 0, i32 %15)
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %16
%20 = tail call i32 (i32, i32, ...) @fcntl(i32 0, i32 %17, i32 %19)
br label %21
21: ; preds = %39, %8
%22 = phi i32 [ %40, %39 ], [ 0, %8 ]
br label %23
23: ; preds = %21, %25
%24 = call i32 @read(i32 0, i8* %3, i32 1)
switch i32 %24, label %34 [
i32 -1, label %25
i32 0, label %32
]
25: ; preds = %23
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %23, label %29
29: ; preds = %25
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %26)
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %23
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %23
%35 = add nsw i32 %22, 1
%36 = icmp eq i32 %22, %11
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 @sleep(i32 %14)
br label %39
39: ; preds = %37, %34
%40 = phi i32 [ 0, %37 ], [ %35, %34 ]
%41 = call i32 @write(i32 1, i8* %3, i32 1)
br label %21
}