#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 {
  int tv_sec;
  int tv_usec;
};
struct timespec {
  int tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ pthread_mutexattr_t;
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int EINVAL;
int ETIMEDOUT;
int /*<<< orphan*/ PTHREAD_MUTEX_NORMAL;
int /*<<< orphan*/ TIMEVAL_TO_TIMESPEC(struct timeval *, struct timespec *);
int /*<<< orphan*/ _SC_CLK_TCK;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ pthread_mutex_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_timedlock(int /*<<< orphan*/ *, struct timespec *);
scalar_t__ pthread_mutex_trylock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_unlock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_init(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
int do_test(void) {
  pthread_mutex_t m;
  struct timespec ts;
  struct timeval tv;
  struct timeval tv2;
  int err;
  pthread_mutexattr_t a;
  if (pthread_mutexattr_init(&a) != 0) {
    puts("mutexattr_init failed");
    return 1;
  }
  if (pthread_mutexattr_settype(&a, PTHREAD_MUTEX_NORMAL) != 0) {
    puts("mutexattr_settype failed");
    return 1;
  }
  if (pthread_mutex_init(&m, &a) != 0) {
    puts("mutex_init failed");
    return 1;
  }
  if (pthread_mutexattr_destroy(&a) != 0) {
    puts("mutexattr_destroy failed");
    return 1;
  }
  if (pthread_mutex_lock(&m) != 0) {
    puts("mutex_lock failed");
    return 1;
  }
  if (pthread_mutex_trylock(&m) == 0) {
    puts("mutex_trylock succeeded");
    return 1;
  }
  gettimeofday(&tv, NULL);
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_sec += 2;
  err = pthread_mutex_timedlock(&m, &ts);
  if (err == 0) {
    puts("timedlock succeeded");
    return 1;
  } else if (err != ETIMEDOUT) {
    printf("timedlock error != ETIMEDOUT: %d\n", err);
    return 1;
  } else {
    int clk_tck = sysconf(_SC_CLK_TCK);
    gettimeofday(&tv2, NULL);
    tv2.tv_sec -= tv.tv_sec;
    tv2.tv_usec -= tv.tv_usec;
    if (tv2.tv_usec < 0) {
      tv2.tv_usec += 1000000;
      tv2.tv_sec -= 1;
    }
    tv2.tv_usec += 1000000 / clk_tck;
    if (tv2.tv_usec >= 1000000) {
      tv2.tv_usec -= 1000000;
      ++tv2.tv_sec;
    }
    if (tv2.tv_sec < 2) {
      printf("premature timeout: %ld.%06ld difference\n", tv2.tv_sec,
             tv2.tv_usec);
      return 1;
    }
  }
  (void)gettimeofday(&tv, NULL);
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_sec += 2;
  ts.tv_nsec += 1000000000;
  err = pthread_mutex_timedlock(&m, &ts);
  if (err == 0) {
    puts("2nd timedlock succeeded");
    return 1;
  } else if (err != EINVAL) {
    printf("2nd timedlock error != EINVAL: %d\n", err);
    return 1;
  }
  if (pthread_mutex_unlock(&m) != 0) {
    puts("mutex_unlock failed");
    return 1;
  }
  (void)gettimeofday(&tv, NULL);
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_sec += 2;
  if (pthread_mutex_timedlock(&m, &ts) != 0) {
    puts("3rd timedlock failed");
  }
  (void)gettimeofday(&tv2, NULL);
  timersub(&tv2, &tv, &tv2);
  if (tv2.tv_sec > 0 || tv2.tv_usec > 100000) {
    puts("3rd timedlock didn't return right away");
    return 1;
  }
  if (pthread_mutex_unlock(&m) != 0) {
    puts("final mutex_unlock failed");
    return 1;
  }
  if (pthread_mutex_destroy(&m) != 0) {
    puts("mutex_destroy failed");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x28(%rbp),%rdi
call   10c0 <pthread_mutexattr_init@plt>
cmp    $0x0,%rax
je     1223 <do_test+0x33>
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1060 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
je     1257 <do_test+0x67>
lea    0xdd0(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10f0 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     1286 <do_test+0x96>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x28(%rbp),%rdi
call   1100 <pthread_mutexattr_destroy@plt>
cmp    $0x0,%rax
je     12b1 <do_test+0xc1>
lea    0xda1(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x8(%rbp),%rdi
call   1110 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
je     12dc <do_test+0xec>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x8(%rbp),%rdi
call   1080 <pthread_mutex_trylock@plt>
cmp    $0x0,%rax
jne    1307 <do_test+0x117>
lea    0xd76(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10e0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <pthread_mutex_timedlock@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    135c <do_test+0x16c>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
mov    -0x24(%rbp),%eax
mov    0x2c6a(%rip),%rcx
cmp    (%rcx),%eax
je     138b <do_test+0x19b>
mov    -0x24(%rbp),%esi
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13dd <do_test+0x1ed>
mov    -0x1c(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    $0xf4240,%eax
cltd
idivl  -0x2c(%rbp)
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xf4240,-0x1c(%rbp)
jl     140d <do_test+0x21d>
mov    -0x1c(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x2,-0x20(%rbp)
jge    1437 <do_test+0x247>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xc82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
jmp    143c <do_test+0x24c>
jmp    1441 <do_test+0x251>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10e0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <pthread_mutex_timedlock@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    14a1 <do_test+0x2b1>
lea    0xc3f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
mov    -0x24(%rbp),%eax
mov    0x2b1d(%rip),%rcx
cmp    (%rcx),%eax
je     14d0 <do_test+0x2e0>
mov    -0x24(%rbp),%esi
lea    0xc2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
jmp    14d5 <do_test+0x2e5>
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     1500 <do_test+0x310>
lea    0xc5d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10e0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <pthread_mutex_timedlock@plt>
cmp    $0x0,%eax
je     1545 <do_test+0x355>
lea    0xbca(%rip),%rdi
call   1030 <puts@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
lea    -0x20(%rbp),%rdx
lea    -0x18(%rbp),%rsi
mov    %rdx,%rdi
call   10d0 <timersub@plt>
cmpl   $0x0,-0x20(%rbp)
jg     1579 <do_test+0x389>
cmpl   $0x186a0,-0x1c(%rbp)
jle    1591 <do_test+0x3a1>
lea    0xb9f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     15bc <do_test+0x3cc>
lea    0xb9b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_destroy@plt>
cmp    $0x0,%rax
je     15e7 <do_test+0x3f7>
lea    0xb8a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <do_test+0x3fe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x14(%rsp),%rdi
call   10c0 <pthread_mutexattr_init@plt>
test   %rax,%rax
je     120f <do_test+0x1f>
lea    0xdf3(%rip),%rdi
jmp    127b <do_test+0x8b>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdi
call   1060 <pthread_mutexattr_settype@plt>
test   %rax,%rax
je     1230 <do_test+0x40>
lea    0xde8(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0x4(%rsp),%rdi
lea    0x14(%rsp),%rsi
call   10f0 <pthread_mutex_init@plt>
test   %rax,%rax
je     124d <do_test+0x5d>
lea    0xde4(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0x14(%rsp),%rdi
call   1100 <pthread_mutexattr_destroy@plt>
test   %rax,%rax
je     1265 <do_test+0x75>
lea    0xdde(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0x4(%rsp),%rdi
call   1110 <pthread_mutex_lock@plt>
test   %rax,%rax
je     128d <do_test+0x9d>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0x4(%rsp),%rdi
call   1080 <pthread_mutex_trylock@plt>
test   %rax,%rax
je     1367 <do_test+0x177>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x18(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   10e0 <TIMEVAL_TO_TIMESPEC@plt>
addl   $0x2,0x18(%rsp)
lea    0x4(%rsp),%rdi
mov    %r14,%rsi
call   1090 <pthread_mutex_timedlock@plt>
test   %eax,%eax
je     1373 <do_test+0x183>
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%eax
jne    137f <do_test+0x18f>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    0x8(%rsp),%esi
sub    0x20(%rsp),%esi
mov    0xc(%rsp),%ecx
mov    %esi,0x8(%rsp)
sub    0x24(%rsp),%ecx
jns    1327 <do_test+0x137>
add    $0xf4240,%ecx
add    $0xffffffff,%esi
mov    %esi,0x8(%rsp)
mov    $0xf4240,%eax
xor    %edx,%edx
idiv   %ebx
add    %ecx,%eax
mov    %eax,0xc(%rsp)
cmp    $0xf4240,%eax
jl     134d <do_test+0x15d>
add    $0xfff0bdc0,%eax
mov    %eax,0xc(%rsp)
add    $0x1,%esi
mov    %esi,0x8(%rsp)
cmp    $0x1,%esi
jg     1394 <do_test+0x1a4>
lea    0xd4d(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1280 <do_test+0x90>
lea    0xcfe(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0xd59(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0xcfe(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1280 <do_test+0x90>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x18(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   10e0 <TIMEVAL_TO_TIMESPEC@plt>
addl   $0x2,0x18(%rsp)
addl   $0x3b9aca00,0x1c(%rsp)
3b
lea    0x4(%rsp),%rdi
mov    %r14,%rsi
call   1090 <pthread_mutex_timedlock@plt>
test   %eax,%eax
je     13f7 <do_test+0x207>
mov    0x2bf0(%rip),%rcx
cmp    (%rcx),%eax
jne    1403 <do_test+0x213>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     140f <do_test+0x21f>
lea    0xd5a(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0xcd1(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0xcdd(%rip),%rdi
jmp    1386 <do_test+0x196>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x18(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   10e0 <TIMEVAL_TO_TIMESPEC@plt>
addl   $0x2,0x18(%rsp)
lea    0x4(%rsp),%rdi
mov    %r14,%rsi
call   1090 <pthread_mutex_timedlock@plt>
test   %eax,%eax
je     1450 <do_test+0x260>
lea    0xcbf(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x20(%rsp),%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
call   10d0 <timersub@plt>
cmpl   $0x0,0x8(%rsp)
jg     149b <do_test+0x2ab>
cmpl   $0x186a1,0xc(%rsp)
jge    149b <do_test+0x2ab>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     14a7 <do_test+0x2b7>
lea    0xcb0(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0xc7d(%rip),%rdi
jmp    127b <do_test+0x8b>
lea    0x4(%rsp),%rdi
call   1050 <pthread_mutex_destroy@plt>
test   %rax,%rax
je     14c2 <do_test+0x2d2>
lea    0xca3(%rip),%rdi
jmp    127b <do_test+0x8b>
xor    %eax,%eax
jmp    1285 <do_test+0x95>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@.str = [22 x i8] c"mutexattr_init failed\00"
@PTHREAD_MUTEX_NORMAL = global i32 0
@.str.1 = [25 x i8] c"mutexattr_settype failed\00"
@.str.2 = [18 x i8] c"mutex_init failed\00"
@.str.3 = [25 x i8] c"mutexattr_destroy failed\00"
@.str.4 = [18 x i8] c"mutex_lock failed\00"
@.str.5 = [24 x i8] c"mutex_trylock succeeded\00"
@.str.6 = [20 x i8] c"timedlock succeeded\00"
@ETIMEDOUT = global i32 0
@.str.7 = [34 x i8] c"timedlock error != ETIMEDOUT: %d\0A\00"
@_SC_CLK_TCK = global i32 0
@.str.8 = [41 x i8] c"premature timeout: %ld.%06ld difference\0A\00"
@.str.9 = [24 x i8] c"2nd timedlock succeeded\00"
@EINVAL = global i32 0
@.str.10 = [35 x i8] c"2nd timedlock error != EINVAL: %d\0A\00"
@.str.11 = [20 x i8] c"mutex_unlock failed\00"
@.str.12 = [21 x i8] c"3rd timedlock failed\00"
@.str.13 = [39 x i8] c"3rd timedlock didn't return right away\00"
@.str.14 = [26 x i8] c"final mutex_unlock failed\00"
@.str.15 = [21 x i8] c"mutex_destroy failed\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = call i64 @pthread_mutexattr_init(i32* %7)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %169
13: ; preds = %0
%14 = load i32, i32* @PTHREAD_MUTEX_NORMAL
%15 = call i64 @pthread_mutexattr_settype(i32* %7, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %169
19: ; preds = %13
%20 = call i64 @pthread_mutex_init(i32* %2, i32* %7)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %169
24: ; preds = %19
%25 = call i64 @pthread_mutexattr_destroy(i32* %7)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %169
29: ; preds = %24
%30 = call i64 @pthread_mutex_lock(i32* %2)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %169
34: ; preds = %29
%35 = call i64 @pthread_mutex_trylock(i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %169
39: ; preds = %34
%40 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%41 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%43 = load i32, i32* %42
%44 = add nsw i32 %43, 2
store i32 %44, i32* %42
%45 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %169
50: ; preds = %39
%51 = load i32, i32* %6
%52 = load i32, i32* @ETIMEDOUT
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* %6
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %55)
store i32 1, i32* %1
br label %169
57: ; preds = %50
%58 = load i32, i32* @_SC_CLK_TCK
%59 = call i32 @sysconf(i32 %58)
store i32 %59, i32* %8
%60 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%64 = load i32, i32* %63
%65 = sub nsw i32 %64, %62
store i32 %65, i32* %63
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%67 = load i32, i32* %66
%68 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%69 = load i32, i32* %68
%70 = sub nsw i32 %69, %67
store i32 %70, i32* %68
%71 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%72 = load i32, i32* %71
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %81
74: ; preds = %57
%75 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%76 = load i32, i32* %75
%77 = add nsw i32 %76, 1000000
store i32 %77, i32* %75
%78 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%79 = load i32, i32* %78
%80 = sub nsw i32 %79, 1
store i32 %80, i32* %78
br label %81
81: ; preds = %74, %57
%82 = load i32, i32* %8
%83 = sdiv i32 1000000, %82
%84 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%85 = load i32, i32* %84
%86 = add nsw i32 %85, %83
store i32 %86, i32* %84
%87 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%88 = load i32, i32* %87
%89 = icmp sge i32 %88, 1000000
br i1 %89, label %90, label %97
90: ; preds = %81
%91 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%92 = load i32, i32* %91
%93 = sub nsw i32 %92, 1000000
store i32 %93, i32* %91
%94 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%95 = load i32, i32* %94
%96 = add nsw i32 %95, 1
store i32 %96, i32* %94
br label %97
97: ; preds = %90, %81
%98 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%99 = load i32, i32* %98
%100 = icmp slt i32 %99, 2
br i1 %100, label %101, label %107
101: ; preds = %97
%102 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%103 = load i32, i32* %102
%104 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%105 = load i32, i32* %104
%106 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i32 %103, i32 %105)
store i32 1, i32* %1
br label %169
107: ; preds = %97
br label %108
108: ; preds = %107
br label %109
109: ; preds = %108
%110 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%111 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%112 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%113 = load i32, i32* %112
%114 = add nsw i32 %113, 2
store i32 %114, i32* %112
%115 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%116 = load i32, i32* %115
%117 = add nsw i32 %116, 1000000000
store i32 %117, i32* %115
%118 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
store i32 %118, i32* %6
%119 = load i32, i32* %6
%120 = icmp eq i32 %119, 0
br i1 %120, label %121, label %123
121: ; preds = %109
%122 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %169
123: ; preds = %109
%124 = load i32, i32* %6
%125 = load i32, i32* @EINVAL
%126 = icmp ne i32 %124, %125
br i1 %126, label %127, label %130
127: ; preds = %123
%128 = load i32, i32* %6
%129 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %128)
store i32 1, i32* %1
br label %169
130: ; preds = %123
br label %131
131: ; preds = %130
%132 = call i64 @pthread_mutex_unlock(i32* %2)
%133 = icmp ne i64 %132, 0
br i1 %133, label %134, label %136
134: ; preds = %131
%135 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0))
store i32 1, i32* %1
br label %169
136: ; preds = %131
%137 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%138 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%139 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%140 = load i32, i32* %139
%141 = add nsw i32 %140, 2
store i32 %141, i32* %139
%142 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
%143 = icmp ne i32 %142, 0
br i1 %143, label %144, label %146
144: ; preds = %136
%145 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
br label %146
146: ; preds = %144, %136
%147 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%148 = call i32 @timersub(%struct.timeval* %5, %struct.timeval* %4, %struct.timeval* %5)
%149 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%150 = load i32, i32* %149
%151 = icmp sgt i32 %150, 0
br i1 %151, label %156, label %152
152: ; preds = %146
%153 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%154 = load i32, i32* %153
%155 = icmp sgt i32 %154, 100000
br i1 %155, label %156, label %158
156: ; preds = %152, %146
%157 = call i32 @puts(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.13, i64 0, i64 0))
store i32 1, i32* %1
br label %169
158: ; preds = %152
%159 = call i64 @pthread_mutex_unlock(i32* %2)
%160 = icmp ne i64 %159, 0
br i1 %160, label %161, label %163
161: ; preds = %158
%162 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0))
store i32 1, i32* %1
br label %169
163: ; preds = %158
%164 = call i64 @pthread_mutex_destroy(i32* %2)
%165 = icmp ne i64 %164, 0
br i1 %165, label %166, label %168
166: ; preds = %163
%167 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.15, i64 0, i64 0))
store i32 1, i32* %1
br label %169
168: ; preds = %163
store i32 0, i32* %1
br label %169
169: ; preds = %168, %166, %161, %156, %134, %127, %121, %101, %54, %48, %37, %32, %27, %22, %17, %11
%170 = load i32, i32* %1
ret i32 %170
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@.str = [22 x i8] c"mutexattr_init failed\00"
@PTHREAD_MUTEX_NORMAL = global i32 0
@.str.1 = [25 x i8] c"mutexattr_settype failed\00"
@.str.2 = [18 x i8] c"mutex_init failed\00"
@.str.3 = [25 x i8] c"mutexattr_destroy failed\00"
@.str.4 = [18 x i8] c"mutex_lock failed\00"
@.str.5 = [24 x i8] c"mutex_trylock succeeded\00"
@.str.6 = [20 x i8] c"timedlock succeeded\00"
@ETIMEDOUT = global i32 0
@.str.7 = [34 x i8] c"timedlock error != ETIMEDOUT: %d\0A\00"
@_SC_CLK_TCK = global i32 0
@.str.8 = [41 x i8] c"premature timeout: %ld.%06ld difference\0A\00"
@.str.9 = [24 x i8] c"2nd timedlock succeeded\00"
@EINVAL = global i32 0
@.str.10 = [35 x i8] c"2nd timedlock error != EINVAL: %d\0A\00"
@.str.11 = [20 x i8] c"mutex_unlock failed\00"
@.str.12 = [21 x i8] c"3rd timedlock failed\00"
@.str.13 = [39 x i8] c"3rd timedlock didn't return right away\00"
@.str.14 = [26 x i8] c"final mutex_unlock failed\00"
@.str.15 = [21 x i8] c"mutex_destroy failed\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = bitcast %struct.timespec* %2 to i8*
%8 = bitcast %struct.timeval* %3 to i8*
%9 = bitcast %struct.timeval* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = call i64 @pthread_mutexattr_init(i32* %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %0
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %140
15: ; preds = %0
%16 = load i32, i32* @PTHREAD_MUTEX_NORMAL
%17 = call i64 @pthread_mutexattr_settype(i32* %5, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %140
21: ; preds = %15
%22 = call i64 @pthread_mutex_init(i32* %1, i32* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %140
26: ; preds = %21
%27 = call i64 @pthread_mutexattr_destroy(i32* %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %140
31: ; preds = %26
%32 = call i64 @pthread_mutex_lock(i32* %1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %140
36: ; preds = %31
%37 = call i64 @pthread_mutex_trylock(i32* %1)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %140
41: ; preds = %36
%42 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%43 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 2
store i32 %46, i32* %44
%47 = call i32 @pthread_mutex_timedlock(i32* %1, %struct.timespec* %2)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %140
51: ; preds = %41
%52 = load i32, i32* @ETIMEDOUT
%53 = icmp eq i32 %47, %52
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %47)
br label %140
56: ; preds = %51
%57 = load i32, i32* @_SC_CLK_TCK
%58 = call i32 @sysconf(i32 %57)
%59 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%61 = load i32, i32* %60
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%63 = load i32, i32* %62
%64 = sub nsw i32 %63, %61
store i32 %64, i32* %62
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%68 = load i32, i32* %67
%69 = sub nsw i32 %68, %66
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %56
%72 = add nsw i32 %69, 1000000
%73 = add nsw i32 %64, -1
store i32 %73, i32* %62
br label %74
74: ; preds = %71, %56
%75 = phi i32 [ %73, %71 ], [ %64, %56 ]
%76 = phi i32 [ %72, %71 ], [ %69, %56 ]
%77 = sdiv i32 1000000, %58
%78 = add nsw i32 %76, %77
store i32 %78, i32* %67
%79 = icmp sgt i32 %78, 999999
br i1 %79, label %80, label %83
80: ; preds = %74
%81 = add nsw i32 %78, -1000000
store i32 %81, i32* %67
%82 = add nsw i32 %75, 1
store i32 %82, i32* %62
br label %83
83: ; preds = %74, %80
%84 = phi i32 [ %81, %80 ], [ %78, %74 ]
%85 = phi i32 [ %82, %80 ], [ %75, %74 ]
%86 = icmp slt i32 %85, 2
br i1 %86, label %87, label %89
87: ; preds = %83
%88 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i32 %85, i32 %84)
br label %140
89: ; preds = %83
%90 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%91 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%92 = load i32, i32* %44
%93 = add nsw i32 %92, 2
store i32 %93, i32* %44
%94 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%95 = load i32, i32* %94
%96 = add nsw i32 %95, 1000000000
store i32 %96, i32* %94
%97 = call i32 @pthread_mutex_timedlock(i32* %1, %struct.timespec* %2)
%98 = icmp eq i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %89
%100 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0))
br label %140
101: ; preds = %89
%102 = load i32, i32* @EINVAL
%103 = icmp eq i32 %97, %102
br i1 %103, label %106, label %104
104: ; preds = %101
%105 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %97)
br label %140
106: ; preds = %101
%107 = call i64 @pthread_mutex_unlock(i32* %1)
%108 = icmp eq i64 %107, 0
br i1 %108, label %111, label %109
109: ; preds = %106
%110 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0))
br label %140
111: ; preds = %106
%112 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%113 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%114 = load i32, i32* %44
%115 = add nsw i32 %114, 2
store i32 %115, i32* %44
%116 = call i32 @pthread_mutex_timedlock(i32* %1, %struct.timespec* %2)
%117 = icmp eq i32 %116, 0
br i1 %117, label %120, label %118
118: ; preds = %111
%119 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
br label %120
120: ; preds = %118, %111
%121 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%122 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %3, %struct.timeval* %4)
%123 = load i32, i32* %62
%124 = icmp sgt i32 %123, 0
%125 = load i32, i32* %67
%126 = icmp sgt i32 %125, 100000
%127 = select i1 %124, i1 true, i1 %126
br i1 %127, label %128, label %130
128: ; preds = %120
%129 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.13, i64 0, i64 0))
br label %140
130: ; preds = %120
%131 = call i64 @pthread_mutex_unlock(i32* %1)
%132 = icmp eq i64 %131, 0
br i1 %132, label %135, label %133
133: ; preds = %130
%134 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0))
br label %140
135: ; preds = %130
%136 = call i64 @pthread_mutex_destroy(i32* %1)
%137 = icmp eq i64 %136, 0
br i1 %137, label %140, label %138
138: ; preds = %135
%139 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.15, i64 0, i64 0))
br label %140
140: ; preds = %87, %135, %138, %133, %128, %109, %104, %99, %54, %49, %39, %34, %29, %24, %19, %13
%141 = phi i32 [ 1, %13 ], [ 1, %19 ], [ 1, %24 ], [ 1, %29 ], [ 1, %34 ], [ 1, %39 ], [ 1, %49 ], [ 1, %54 ], [ 1, %99 ], [ 1, %104 ], [ 1, %109 ], [ 1, %128 ], [ 1, %133 ], [ 1, %138 ], [ 1, %87 ], [ 0, %135 ]
ret i32 %141
}