#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;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_MUTEX_INITIALIZER;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, scalar_t__, scalar_t__);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int test1(int times) {
  struct timeval tv_start, tv_stop;
  gettimeofday(&tv_start, NULL);
  ;
  int i = times;
  while (i--) {
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_lock(&mutex);
    pthread_mutex_unlock(&mutex);
  }
  gettimeofday(&tv_stop, NULL);
  if (tv_stop.tv_usec < tv_start.tv_usec) {
    tv_stop.tv_usec += 1000 * 1000;
    tv_stop.tv_sec--;
  }
  printf("Count[%d] Elapsed[%ld.%ld]\n", times,
         tv_stop.tv_sec - tv_start.tv_sec, tv_stop.tv_usec - tv_start.tv_usec);
  ;
}


x86 assembly (clang, -O0):
<test1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x0,%eax
je     1195 <test1+0x55>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
lea    -0x30(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
jmp    115e <test1+0x1e>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11ca <test1+0x8a>
mov    -0x28(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x20(%rbp),%rdx
sub    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
sub    -0x18(%rbp),%rcx
lea    0xe1c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test1>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %r14d,%r14d
je     118c <test1+0x4c>
mov    0x2e7b(%rip),%r15
lea    0x8(%rsp),%rbx
mov    %r14d,%ebp
nopl   (%rax)
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   1060 <pthread_mutex_lock@plt>
mov    %rbx,%rdi
call   1050 <pthread_mutex_unlock@plt>
add    $0xffffffff,%ebp
jne    1170 <test1+0x30>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x18(%rsp),%rax
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
lea    0xf4240(%rsi),%rcx
xor    %edi,%edi
cmp    %rax,%rsi
setl   %dil
cmovge %rsi,%rcx
sub    %rdi,%rdx
sub    0x20(%rsp),%rdx
sub    %rax,%rcx
lea    0xe33(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [28 x i8] c"Count[%d] Elapsed[%ld.%ld]\0A\00"
define i32 @test1(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%9 = load i32, i32* %3
store i32 %9, i32* %6
br label %10
10: ; preds = %14, %1
%11 = load i32, i32* %6
%12 = add nsw i32 %11, -1
store i32 %12, i32* %6
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %15, i32* %7
%16 = call i32 @pthread_mutex_lock(i32* %7)
%17 = call i32 @pthread_mutex_unlock(i32* %7)
br label %10
18: ; preds = %10
%19 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp slt i64 %21, %23
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%27 = load i64, i64* %26
%28 = add nsw i64 %27, 1000000
store i64 %28, i64* %26
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%30 = load i64, i64* %29
%31 = add nsw i64 %30, -1
store i64 %31, i64* %29
br label %32
32: ; preds = %25, %18
%33 = load i32, i32* %3
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%37 = load i64, i64* %36
%38 = sub nsw i64 %35, %37
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%40 = load i64, i64* %39
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%42 = load i64, i64* %41
%43 = sub nsw i64 %40, %42
%44 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %33, i64 %38, i64 %43)
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [28 x i8] c"Count[%d] Elapsed[%ld.%ld]\0A\00"
define i32 @test1(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%8 = icmp eq i32 %0, 0
br i1 %8, label %18, label %9
9: ; preds = %1
%10 = bitcast i32* %4 to i8*
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ %0, %9 ], [ %13, %11 ]
%13 = add nsw i32 %12, -1
%14 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %14, i32* %4
%15 = call i32 @pthread_mutex_lock(i32* %4)
%16 = call i32 @pthread_mutex_unlock(i32* %4)
%17 = icmp eq i32 %13, 0
br i1 %17, label %18, label %11
18: ; preds = %11, %1
%19 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp slt i64 %21, %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%26 = load i64, i64* %25
%27 = add nsw i64 %21, 1000000
%28 = select i1 %24, i64 %27, i64 %21
%29 = sext i1 %24 to i64
%30 = add nsw i64 %26, %29
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%32 = load i64, i64* %31
%33 = sub nsw i64 %30, %32
%34 = sub nsw i64 %28, %23
%35 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %33, i64 %34)
ret i32 undef
}