#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 {
  scalar_t__ tv_usec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int *malloc(int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
void *threadFn2(void *in_arg) {
  struct timeval *end = (struct timeval *)in_arg;
  struct timeval begin;
  struct timeval ignored0, ignored1;
  gettimeofday(&begin, NULL);
  for (int i = 0; i < 1000; i++) {
    gettimeofday(&ignored0, NULL);
    int *ptr = malloc(sizeof(int));
    *ptr = i;
    free(ptr);
    gettimeofday(&ignored1, NULL);
  }
  gettimeofday(end, NULL);
  while (begin.tv_usec > end->tv_usec) {
    end->tv_usec += 1000;
    end->tv_sec -= 1;
  }
  end->tv_sec -= begin.tv_sec;
  end->tv_usec -= begin.tv_usec;
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<threadFn2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3e8,-0x4c(%rbp)
jge    11bd <threadFn2+0x7d>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x4c(%rbp),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1168 <threadFn2+0x28>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jle    11fe <threadFn2+0xbe>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x3e8,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,0x8(%rax)
jmp    11ca <threadFn2+0x8a>
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
sub    %edx,%ecx
mov    %ecx,0x8(%rax)
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threadFn2>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
lea    0x28(%rsp),%r15
lea    0x18(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %ebx,(%rax)
mov    %rax,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    1170 <threadFn2+0x30>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x8(%rsp),%rcx
mov    (%r14),%rsi
mov    0x8(%r14),%edi
cmp    %rcx,%rsi
jge    120a <threadFn2+0xca>
lea    0x3e8(%rsi),%rax
cmp    %rax,%rcx
cmovg  %rcx,%rax
sub    %rsi,%rax
xor    %ebx,%ebx
add    $0xfffffffffffffc18,%rax
setne  %bl
sub    %rbx,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mul    %rdx
shr    $0x4,%rdx
add    %rbx,%rdx
imul   $0x3e8,%rdx,%rax
add    %rax,%rsi
add    $0x3e8,%rsi
not    %edx
add    %edx,%edi
sub    0x10(%rsp),%edi
mov    %edi,0x8(%r14)
sub    %rcx,%rsi
mov    %rsi,(%r14)
xor    %edi,%edi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <pthread_exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
define i8* @threadFn2(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to %struct.timeval*
store %struct.timeval* %11, %struct.timeval** %4
%12 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 0, i32* %8
br label %13
13: ; preds = %24, %1
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 1000
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%18 = call i32* @malloc(i32 4)
store i32* %18, i32** %9
%19 = load i32, i32* %8
%20 = load i32*, i32** %9
store i32 %19, i32* %20
%21 = load i32*, i32** %9
%22 = call i32 @free(i32* %21)
%23 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
br label %24
24: ; preds = %16
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %13
27: ; preds = %13
%28 = load %struct.timeval*, %struct.timeval** %4
%29 = call i32 @gettimeofday(%struct.timeval* %28, i32* null)
br label %30
30: ; preds = %37, %27
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%32 = load i64, i64* %31
%33 = load %struct.timeval*, %struct.timeval** %4
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = icmp sgt i64 %32, %35
br i1 %36, label %37, label %46
37: ; preds = %30
%38 = load %struct.timeval*, %struct.timeval** %4
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = add nsw i64 %40, 1000
store i64 %41, i64* %39
%42 = load %struct.timeval*, %struct.timeval** %4
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = sub nsw i32 %44, 1
store i32 %45, i32* %43
br label %30
46: ; preds = %30
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.timeval*, %struct.timeval** %4
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = sub nsw i32 %51, %48
store i32 %52, i32* %50
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%54 = load i64, i64* %53
%55 = load %struct.timeval*, %struct.timeval** %4
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 0
%57 = load i64, i64* %56
%58 = sub nsw i64 %57, %54
store i64 %58, i64* %56
%59 = call i32 @pthread_exit(i32* null)
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define noalias i8* @threadFn2(i8* nocapture %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
br label %36
9: ; preds = %36
%10 = bitcast i8* %0 to %struct.timeval*
%11 = tail call i32 @gettimeofday(%struct.timeval* %10, i32* null)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%13 = load i64, i64* %12
%14 = bitcast i8* %0 to i64*
%15 = load i64, i64* %14
%16 = icmp slt i64 %15, %13
%17 = getelementptr inbounds i8, i8* %0, i64 8
%18 = bitcast i8* %17 to i32*
%19 = load i32, i32* %18
br i1 %16, label %20, label %44
20: ; preds = %9
%21 = add i64 %15, 1000
%22 = call i64 @llvm.smax.i64(i64 %13, i64 %21)
%23 = add i64 %22, -1000
%24 = sub i64 %23, %15
%25 = icmp ne i64 %24, 0
%26 = zext i1 %25 to i64
%27 = sub i64 %24, %26
%28 = udiv i64 %27, 1000
%29 = add nuw nsw i64 %28, %26
%30 = mul i64 %29, 1000
%31 = add i64 %15, %30
%32 = add i64 %31, 1000
%33 = trunc i64 %29 to i32
%34 = xor i32 %33, -1
%35 = add i32 %19, %34
br label %44
36: ; preds = %1, %36
%37 = phi i32 [ 0, %1 ], [ %42, %36 ]
%38 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%39 = tail call i32* @malloc(i32 4)
store i32 %37, i32* %39
%40 = tail call i32 @free(i32* %39)
%41 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%42 = add nuw nsw i32 %37, 1
%43 = icmp eq i32 %42, 1000
br i1 %43, label %9, label %36
44: ; preds = %9, %20
%45 = phi i32 [ %35, %20 ], [ %19, %9 ]
%46 = phi i64 [ %32, %20 ], [ %15, %9 ]
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%48 = load i32, i32* %47
%49 = sub nsw i32 %45, %48
store i32 %49, i32* %18
%50 = sub nsw i64 %46, %13
store i64 %50, i64* %14
%51 = tail call i32 @pthread_exit(i32* null)
ret i8* undef
}