#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 */
char *get_random_message();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
int /*<<< orphan*/ write_to_file(int /*<<< orphan*/ *, char *);
void loop(FILE *fp, int delay) {
  const int MICRO_SECOND = 1000000;
  struct timeval tv;
  printf("[INFO]: Opened file now starting to write log messages\n");
  printf("******************************************************\n");
  for (;;) {
    char *message = get_random_message();
    printf("%s\n", message);
    write_to_file(fp, message);
    if (delay >= MICRO_SECOND) {
      tv.tv_sec = delay / 1000000;
      tv.tv_usec = 0;
    } else {
      tv.tv_sec = 0;
      tv.tv_usec = delay;
    }
    select(0, NULL, NULL, NULL, &tv);
  }
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xf4240,-0x10(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xecd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <get_random_message@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xee8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <write_to_file@plt>
cmpl   $0xf4240,-0xc(%rbp)
jl     11c3 <loop+0x83>
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
jmp    11d0 <loop+0x90>
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1060 <select@plt>
jmp    1172 <loop+0x32>

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r12
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
lea    0xed2(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
imul   $0x431bde83,%rax,%rbx
shr    $0x32,%rbx
xor    %eax,%eax
cmp    $0xf4240,%r15d
cmovl  %eax,%ebx
cmovge %eax,%r15d
lea    0x8(%rsp),%r14
nopl   (%rax)
xor    %eax,%eax
call   1040 <get_random_message@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1050 <write_to_file@plt>
mov    %ebx,0x8(%rsp)
mov    %r15d,0xc(%rsp)
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1060 <select@plt>
jmp    1190 <loop+0x50>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [56 x i8] c"[INFO]: Opened file now starting to write log messages\0A\00"
@.str.1 = [56 x i8] c"******************************************************\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @loop(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i8*
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 1000000, i32* %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %28, %2
%11 = call i8* (...) @get_random_message()
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = load i32*, i32** %3
%15 = load i8*, i8** %7
%16 = call i32 @write_to_file(i32* %14, i8* %15)
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 1000000
br i1 %18, label %19, label %24
19: ; preds = %10
%20 = load i32, i32* %4
%21 = sdiv i32 %20, 1000000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 0, i32* %23
br label %28
24: ; preds = %10
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 0, i32* %25
%26 = load i32, i32* %4
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 %26, i32* %27
br label %28
28: ; preds = %24, %19
%29 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %6)
br label %10
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@str = [55 x i8] c"[INFO]: Opened file now starting to write log messages\00"
@str.3 = [55 x i8] c"******************************************************\00"
define void @loop(i32* %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.3, i64 0, i64 0))
%7 = icmp sgt i32 %1, 999999
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%10 = udiv i32 %1, 1000000
%11 = select i1 %7, i32 %10, i32 0
%12 = select i1 %7, i32 0, i32 %1
br label %13
13: ; preds = %13, %2
%14 = call i8* (...) @get_random_message()
%15 = call i32 @puts(i8* dereferenceable(1) %14)
%16 = call i32 @write_to_file(i32* %0, i8* %14)
store i32 %11, i32* %8
store i32 %12, i32* %9
%17 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %3)
br label %13
}