#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 *threadFn(void *in_arg) {
  struct timeval *end = (struct timeval *)in_arg;
  struct timeval begin;
  gettimeofday(&begin, NULL);
  for (int i = 0; i < 1000; i++) {
    int *ptr = malloc(sizeof(int));
    *ptr = i;
    free(ptr);
  }
  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):
<threadFn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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,-0x2c(%rbp)
cmpl   $0x3e8,-0x2c(%rbp)
jge    11a3 <threadFn+0x63>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1168 <threadFn+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    11e4 <threadFn+0xa4>
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    11b0 <threadFn+0x70>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threadFn>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
nopl   0x0(%rax,%rax,1)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %ebx,(%rax)
mov    %rax,%rdi
call   1030 <free@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    1160 <threadFn+0x20>
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    11e2 <threadFn+0xa2>
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    0x10(%rsp),%edi
mov    %edi,0x8(%r14)
sub    %rcx,%rsi
mov    %rsi,(%r14)
xor    %edi,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1050 <pthread_exit@plt>

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

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