#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;
};
struct timespec {
  int dummy;
};

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


x86 assembly (clang, -O0):
<threadFn3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3e8,-0x3c(%rbp)
jge    11cf <threadFn3+0x7f>
mov    $0x5,%edi
lea    -0x30(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    $0x5,%edi
lea    -0x38(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1178 <threadFn3+0x28>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jle    1210 <threadFn3+0xc0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,0x8(%rax)
jmp    11dc <threadFn3+0x8c>
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   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threadFn3>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    $0x5,%edi
mov    %r15,%rsi
call   1040 <clock_gettime@plt>
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %ebx,(%rax)
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x5,%edi
mov    %r12,%rsi
call   1040 <clock_gettime@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    1180 <threadFn3+0x30>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x18(%rsp),%rcx
mov    (%r14),%rsi
mov    0x8(%r14),%edi
cmp    %rcx,%rsi
jge    121c <threadFn3+0xcc>
lea    0xf4240(%rsi),%rax
cmp    %rax,%rcx
cmovg  %rcx,%rax
sub    %rsi,%rax
xor    %ebx,%ebx
add    $0xfffffffffff0bdc0,%rax
setne  %bl
sub    %rbx,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
add    %rbx,%rdx
imul   $0xf4240,%rdx,%rax
add    %rax,%rsi
add    $0xf4240,%rsi
not    %edx
add    %edx,%edi
sub    0x20(%rsp),%edi
mov    %edi,0x8(%r14)
sub    %rcx,%rsi
mov    %rsi,(%r14)
xor    %edi,%edi
call   1060 <pthread_exit@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.timespec = type { i32 }
define i8* @threadFn3(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%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 @clock_gettime(i32 5, %struct.timespec* %6)
%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 @clock_gettime(i32 5, %struct.timespec* %7)
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, 1000000
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 }
%struct.timespec = type { i32 }
define noalias i8* @threadFn3(i8* nocapture %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.timespec* %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 = 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, 1000000
%22 = call i64 @llvm.smax.i64(i64 %13, i64 %21)
%23 = add i64 %22, -1000000
%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, 1000000
%29 = add nuw nsw i64 %28, %26
%30 = mul i64 %29, 1000000
%31 = add i64 %15, %30
%32 = add i64 %31, 1000000
%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 @clock_gettime(i32 5, %struct.timespec* %3)
%39 = call i32* @malloc(i32 4)
store i32 %37, i32* %39
%40 = call i32 @free(i32* %39)
%41 = call i32 @clock_gettime(i32 5, %struct.timespec* %4)
%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 = call i32 @pthread_exit(i32* null)
ret i8* undef
}