#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*/ *);
int 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;
  }
  err = pthread_mutex_init(&m, &a);
  if (err != 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);
    if (clk_tck <= 0) {
      clk_tck = 100;
    }
    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    1602 <do_test+0x412>
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    1602 <do_test+0x412>
lea    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10f0 <pthread_mutex_init@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1289 <do_test+0x99>
lea    0xdb7(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
lea    -0x28(%rbp),%rdi
call   1100 <pthread_mutexattr_destroy@plt>
cmp    $0x0,%rax
je     12b4 <do_test+0xc4>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
lea    -0x8(%rbp),%rdi
call   1110 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
je     12df <do_test+0xef>
lea    0xd8c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
lea    -0x8(%rbp),%rdi
call   1080 <pthread_mutex_trylock@plt>
cmp    $0x0,%rax
jne    130a <do_test+0x11a>
lea    0xd73(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
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    135f <do_test+0x16f>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
mov    -0x24(%rbp),%eax
mov    0x2c67(%rip),%rcx
cmp    (%rcx),%eax
je     138e <do_test+0x19e>
mov    -0x24(%rbp),%esi
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     13b0 <do_test+0x1c0>
movl   $0x64,-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    13f1 <do_test+0x201>
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     1421 <do_test+0x231>
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    144b <do_test+0x25b>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xc6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
jmp    1450 <do_test+0x260>
jmp    1455 <do_test+0x265>
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    14b5 <do_test+0x2c5>
lea    0xc2b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
mov    -0x24(%rbp),%eax
mov    0x2b09(%rip),%rcx
cmp    (%rcx),%eax
je     14e4 <do_test+0x2f4>
mov    -0x24(%rbp),%esi
lea    0xc16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
jmp    14e9 <do_test+0x2f9>
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     1514 <do_test+0x324>
lea    0xc49(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
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     1559 <do_test+0x369>
lea    0xbb6(%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     158d <do_test+0x39d>
cmpl   $0x186a0,-0x1c(%rbp)
jle    15a5 <do_test+0x3b5>
lea    0xb8b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     15d0 <do_test+0x3e0>
lea    0xb87(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_destroy@plt>
cmp    $0x0,%rax
je     15fb <do_test+0x40b>
lea    0xb76(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1602 <do_test+0x412>
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    127a <do_test+0x8a>
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    127a <do_test+0x8a>
lea    0x4(%rsp),%rdi
lea    0x14(%rsp),%rsi
call   10f0 <pthread_mutex_init@plt>
test   %eax,%eax
je     124c <do_test+0x5c>
lea    0xde5(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0x14(%rsp),%rdi
call   1100 <pthread_mutexattr_destroy@plt>
test   %rax,%rax
je     1264 <do_test+0x74>
lea    0xddf(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0x4(%rsp),%rdi
call   1110 <pthread_mutex_lock@plt>
test   %rax,%rax
je     128c <do_test+0x9c>
lea    0xde0(%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     136e <do_test+0x17e>
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     137a <do_test+0x18a>
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%eax
jne    1386 <do_test+0x196>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
test   %eax,%eax
mov    $0x64,%ebx
cmovg  %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    132e <do_test+0x13e>
add    $0xf4240,%ecx
add    $0xffffffff,%esi
mov    %esi,0x8(%rsp)
mov    $0xf4240,%eax
xor    %edx,%edx
div    %ebx
add    %ecx,%eax
mov    %eax,0xc(%rsp)
cmp    $0xf4240,%eax
jl     1354 <do_test+0x164>
add    $0xfff0bdc0,%eax
mov    %eax,0xc(%rsp)
add    $0x1,%esi
mov    %esi,0x8(%rsp)
cmp    $0x1,%esi
jg     139b <do_test+0x1ab>
lea    0xd46(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    127f <do_test+0x8f>
lea    0xcf7(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xd52(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xcf7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    127f <do_test+0x8f>
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     13fe <do_test+0x20e>
mov    0x2be9(%rip),%rcx
cmp    (%rcx),%eax
jne    140a <do_test+0x21a>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     1416 <do_test+0x226>
lea    0xd53(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xcca(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xcd6(%rip),%rdi
jmp    138d <do_test+0x19d>
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     1457 <do_test+0x267>
lea    0xcb8(%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     14a2 <do_test+0x2b2>
cmpl   $0x186a1,0xc(%rsp)
jge    14a2 <do_test+0x2b2>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     14ae <do_test+0x2be>
lea    0xca9(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xc76(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0x4(%rsp),%rdi
call   1050 <pthread_mutex_destroy@plt>
test   %rax,%rax
je     14c9 <do_test+0x2d9>
lea    0xc9c(%rip),%rdi
jmp    127a <do_test+0x8a>
xor    %eax,%eax
jmp    1284 <do_test+0x94>

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 %174
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 %174
19: ; preds = %13
%20 = call i32 @pthread_mutex_init(i32* %2, i32* %7)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = 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 %174
25: ; preds = %19
%26 = call i64 @pthread_mutexattr_destroy(i32* %7)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = 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 %174
30: ; preds = %25
%31 = call i64 @pthread_mutex_lock(i32* %2)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = 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 %174
35: ; preds = %30
%36 = call i64 @pthread_mutex_trylock(i32* %2)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = 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 %174
40: ; preds = %35
%41 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%42 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = add nsw i32 %44, 2
store i32 %45, i32* %43
%46 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = 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 %174
51: ; preds = %40
%52 = load i32, i32* %6
%53 = load i32, i32* @ETIMEDOUT
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* %6
%57 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %56)
store i32 1, i32* %1
br label %174
58: ; preds = %51
%59 = load i32, i32* @_SC_CLK_TCK
%60 = call i32 @sysconf(i32 %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp sle i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58
store i32 100, i32* %8
br label %64
64: ; preds = %63, %58
%65 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%69 = load i32, i32* %68
%70 = sub nsw i32 %69, %67
store i32 %70, i32* %68
%71 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%72 = load i32, i32* %71
%73 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%74 = load i32, i32* %73
%75 = sub nsw i32 %74, %72
store i32 %75, i32* %73
%76 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%77 = load i32, i32* %76
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %86
79: ; preds = %64
%80 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%81 = load i32, i32* %80
%82 = add nsw i32 %81, 1000000
store i32 %82, i32* %80
%83 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%84 = load i32, i32* %83
%85 = sub nsw i32 %84, 1
store i32 %85, i32* %83
br label %86
86: ; preds = %79, %64
%87 = load i32, i32* %8
%88 = sdiv i32 1000000, %87
%89 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%90 = load i32, i32* %89
%91 = add nsw i32 %90, %88
store i32 %91, i32* %89
%92 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%93 = load i32, i32* %92
%94 = icmp sge i32 %93, 1000000
br i1 %94, label %95, label %102
95: ; preds = %86
%96 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%97 = load i32, i32* %96
%98 = sub nsw i32 %97, 1000000
store i32 %98, i32* %96
%99 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%100 = load i32, i32* %99
%101 = add nsw i32 %100, 1
store i32 %101, i32* %99
br label %102
102: ; preds = %95, %86
%103 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%104 = load i32, i32* %103
%105 = icmp slt i32 %104, 2
br i1 %105, label %106, label %112
106: ; preds = %102
%107 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%108 = load i32, i32* %107
%109 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%110 = load i32, i32* %109
%111 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i32 %108, i32 %110)
store i32 1, i32* %1
br label %174
112: ; preds = %102
br label %113
113: ; preds = %112
br label %114
114: ; preds = %113
%115 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%116 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%117 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%118 = load i32, i32* %117
%119 = add nsw i32 %118, 2
store i32 %119, i32* %117
%120 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%121 = load i32, i32* %120
%122 = add nsw i32 %121, 1000000000
store i32 %122, i32* %120
%123 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
store i32 %123, i32* %6
%124 = load i32, i32* %6
%125 = icmp eq i32 %124, 0
br i1 %125, label %126, label %128
126: ; preds = %114
%127 = 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 %174
128: ; preds = %114
%129 = load i32, i32* %6
%130 = load i32, i32* @EINVAL
%131 = icmp ne i32 %129, %130
br i1 %131, label %132, label %135
132: ; preds = %128
%133 = load i32, i32* %6
%134 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %133)
store i32 1, i32* %1
br label %174
135: ; preds = %128
br label %136
136: ; preds = %135
%137 = call i64 @pthread_mutex_unlock(i32* %2)
%138 = icmp ne i64 %137, 0
br i1 %138, label %139, label %141
139: ; preds = %136
%140 = 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 %174
141: ; preds = %136
%142 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%143 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%144 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%145 = load i32, i32* %144
%146 = add nsw i32 %145, 2
store i32 %146, i32* %144
%147 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
%148 = icmp ne i32 %147, 0
br i1 %148, label %149, label %151
149: ; preds = %141
%150 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
br label %151
151: ; preds = %149, %141
%152 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%153 = call i32 @timersub(%struct.timeval* %5, %struct.timeval* %4, %struct.timeval* %5)
%154 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%155 = load i32, i32* %154
%156 = icmp sgt i32 %155, 0
br i1 %156, label %161, label %157
157: ; preds = %151
%158 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%159 = load i32, i32* %158
%160 = icmp sgt i32 %159, 100000
br i1 %160, label %161, label %163
161: ; preds = %157, %151
%162 = 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 %174
163: ; preds = %157
%164 = call i64 @pthread_mutex_unlock(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 ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0))
store i32 1, i32* %1
br label %174
168: ; preds = %163
%169 = call i64 @pthread_mutex_destroy(i32* %2)
%170 = icmp ne i64 %169, 0
br i1 %170, label %171, label %173
171: ; preds = %168
%172 = 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 %174
173: ; preds = %168
store i32 0, i32* %1
br label %174
174: ; preds = %173, %171, %166, %161, %139, %132, %126, %106, %55, %49, %38, %33, %28, %23, %17, %11
%175 = load i32, i32* %1
ret i32 %175
}

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 %142
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 %142
21: ; preds = %15
%22 = call i32 @pthread_mutex_init(i32* %1, i32* %5)
%23 = icmp eq i32 %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 %142
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 %142
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 %142
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 %142
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 %142
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 %142
56: ; preds = %51
%57 = load i32, i32* @_SC_CLK_TCK
%58 = call i32 @sysconf(i32 %57)
%59 = icmp slt i32 %58, 1
%60 = select i1 %59, i32 100, i32 %58
%61 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%63 = load i32, i32* %62
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%65 = load i32, i32* %64
%66 = sub nsw i32 %65, %63
store i32 %66, i32* %64
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%68 = load i32, i32* %67
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%70 = load i32, i32* %69
%71 = sub nsw i32 %70, %68
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %56
%74 = add nsw i32 %71, 1000000
%75 = add nsw i32 %66, -1
store i32 %75, i32* %64
br label %76
76: ; preds = %73, %56
%77 = phi i32 [ %75, %73 ], [ %66, %56 ]
%78 = phi i32 [ %74, %73 ], [ %71, %56 ]
%79 = udiv i32 1000000, %60
%80 = add nsw i32 %78, %79
store i32 %80, i32* %69
%81 = icmp sgt i32 %80, 999999
br i1 %81, label %82, label %85
82: ; preds = %76
%83 = add nsw i32 %80, -1000000
store i32 %83, i32* %69
%84 = add nsw i32 %77, 1
store i32 %84, i32* %64
br label %85
85: ; preds = %76, %82
%86 = phi i32 [ %83, %82 ], [ %80, %76 ]
%87 = phi i32 [ %84, %82 ], [ %77, %76 ]
%88 = icmp slt i32 %87, 2
br i1 %88, label %89, label %91
89: ; preds = %85
%90 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i32 %87, i32 %86)
br label %142
91: ; preds = %85
%92 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%93 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%94 = load i32, i32* %44
%95 = add nsw i32 %94, 2
store i32 %95, i32* %44
%96 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%97 = load i32, i32* %96
%98 = add nsw i32 %97, 1000000000
store i32 %98, i32* %96
%99 = call i32 @pthread_mutex_timedlock(i32* %1, %struct.timespec* %2)
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %103
101: ; preds = %91
%102 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0))
br label %142
103: ; preds = %91
%104 = load i32, i32* @EINVAL
%105 = icmp eq i32 %99, %104
br i1 %105, label %108, label %106
106: ; preds = %103
%107 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %99)
br label %142
108: ; preds = %103
%109 = call i64 @pthread_mutex_unlock(i32* %1)
%110 = icmp eq i64 %109, 0
br i1 %110, label %113, label %111
111: ; preds = %108
%112 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0))
br label %142
113: ; preds = %108
%114 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%115 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%116 = load i32, i32* %44
%117 = add nsw i32 %116, 2
store i32 %117, i32* %44
%118 = call i32 @pthread_mutex_timedlock(i32* %1, %struct.timespec* %2)
%119 = icmp eq i32 %118, 0
br i1 %119, label %122, label %120
120: ; preds = %113
%121 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
br label %122
122: ; preds = %120, %113
%123 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%124 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %3, %struct.timeval* %4)
%125 = load i32, i32* %64
%126 = icmp sgt i32 %125, 0
%127 = load i32, i32* %69
%128 = icmp sgt i32 %127, 100000
%129 = select i1 %126, i1 true, i1 %128
br i1 %129, label %130, label %132
130: ; preds = %122
%131 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.13, i64 0, i64 0))
br label %142
132: ; preds = %122
%133 = call i64 @pthread_mutex_unlock(i32* %1)
%134 = icmp eq i64 %133, 0
br i1 %134, label %137, label %135
135: ; preds = %132
%136 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0))
br label %142
137: ; preds = %132
%138 = call i64 @pthread_mutex_destroy(i32* %1)
%139 = icmp eq i64 %138, 0
br i1 %139, label %142, label %140
140: ; preds = %137
%141 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.15, i64 0, i64 0))
br label %142
142: ; preds = %89, %137, %140, %135, %130, %111, %106, %101, %54, %49, %39, %34, %29, %24, %19, %13
%143 = phi i32 [ 1, %13 ], [ 1, %19 ], [ 1, %24 ], [ 1, %29 ], [ 1, %34 ], [ 1, %39 ], [ 1, %49 ], [ 1, %54 ], [ 1, %101 ], [ 1, %106 ], [ 1, %111 ], [ 1, %130 ], [ 1, %135 ], [ 1, %140 ], [ 1, %89 ], [ 0, %137 ]
ret i32 %143
}