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

/* Variables and functions */
int rand();
int /*<<< orphan*/ writemsg(int /*<<< orphan*/ *, char *, char, int);
void *thread1_write(void *arg) {
  int i, loop = rand() % 5 + 1;
  FILE *fdst = (FILE *)arg;
  for (i = 1; i <= loop; i++) {
    writemsg(fdst, "Thread 1", 'a', 200);
  }
}


x86 assembly (clang, -O0):
<thread1_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1184 <thread1_write+0x64>
mov    -0x20(%rbp),%rdi
lea    0xe99(%rip),%rsi
mov    $0x61,%edx
mov    $0xc8,%ecx
call   1030 <writemsg@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1150 <thread1_write+0x30>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread1_write>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <rand@plt>
movslq %eax,%rbx
imul   $0x66666667,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
js     117a <thread1_write+0x5a>
add    $0x1,%ebx
lea    0xea9(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x61,%edx
mov    $0xc8,%ecx
call   1030 <writemsg@plt>
add    $0xffffffff,%ebx
jne    1160 <thread1_write+0x40>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"Thread 1\00"
define i8* @thread1_write(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
%7 = call i32 (...) @rand()
%8 = srem i32 %7, 5
%9 = add nsw i32 %8, 1
store i32 %9, i32* %5
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %6
store i32 1, i32* %4
br label %12
12: ; preds = %19, %1
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32*, i32** %6
%18 = call i32 @writemsg(i32* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8 signext 97, i32 200)
br label %19
19: ; preds = %16
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %12
22: ; preds = %12
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@.str = [9 x i8] c"Thread 1\00"
define noalias i8* @thread1_write(i8* %0) {
%2 = tail call i32 (...) @rand()
%3 = srem i32 %2, 5
%4 = bitcast i8* %0 to i32*
%5 = icmp slt i32 %3, 0
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = add nsw i32 %3, 1
br label %8
8: ; preds = %6, %8
%9 = phi i32 [ %11, %8 ], [ 1, %6 ]
%10 = tail call i32 @writemsg(i32* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8 signext 97, i32 200)
%11 = add nuw nsw i32 %9, 1
%12 = icmp eq i32 %9, %7
br i1 %12, label %13, label %8
13: ; preds = %8, %1
ret i8* undef
}