#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);
    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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
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    15f1 <do_test+0x401>
mov    0x2c43(%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    13e0 <do_test+0x1f0>
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     1410 <do_test+0x220>
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    143a <do_test+0x24a>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xc7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
jmp    143f <do_test+0x24f>
jmp    1444 <do_test+0x254>
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    14a4 <do_test+0x2b4>
lea    0xc3c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
mov    -0x24(%rbp),%eax
mov    0x2b1a(%rip),%rcx
cmp    (%rcx),%eax
je     14d3 <do_test+0x2e3>
mov    -0x24(%rbp),%esi
lea    0xc27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
jmp    14d8 <do_test+0x2e8>
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     1503 <do_test+0x313>
lea    0xc5a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
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     1548 <do_test+0x358>
lea    0xbc7(%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     157c <do_test+0x38c>
cmpl   $0x186a0,-0x1c(%rbp)
jle    1594 <do_test+0x3a4>
lea    0xb9c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     15bf <do_test+0x3cf>
lea    0xb98(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_destroy@plt>
cmp    $0x0,%rax
je     15ea <do_test+0x3fa>
lea    0xb87(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f1 <do_test+0x401>
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     1366 <do_test+0x176>
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     1372 <do_test+0x182>
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%eax
jne    137e <do_test+0x18e>
mov    0x2cea(%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    1326 <do_test+0x136>
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     134c <do_test+0x15c>
add    $0xfff0bdc0,%eax
mov    %eax,0xc(%rsp)
add    $0x1,%esi
mov    %esi,0x8(%rsp)
cmp    $0x1,%esi
jg     1393 <do_test+0x1a3>
lea    0xd4e(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    127f <do_test+0x8f>
lea    0xcff(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xd5a(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xcff(%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     13f6 <do_test+0x206>
mov    0x2bf1(%rip),%rcx
cmp    (%rcx),%eax
jne    1402 <do_test+0x212>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     140e <do_test+0x21e>
lea    0xd5b(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xcd2(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xcde(%rip),%rdi
jmp    1385 <do_test+0x195>
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     144f <do_test+0x25f>
lea    0xcc0(%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     149a <do_test+0x2aa>
cmpl   $0x186a1,0xc(%rsp)
jge    149a <do_test+0x2aa>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     14a6 <do_test+0x2b6>
lea    0xcb1(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0xc7e(%rip),%rdi
jmp    127a <do_test+0x8a>
lea    0x4(%rsp),%rdi
call   1050 <pthread_mutex_destroy@plt>
test   %rax,%rax
je     14c1 <do_test+0x2d1>
lea    0xca4(%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 %170
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 %170
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 %170
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 %170
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 %170
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 %170
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 %170
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 %170
58: ; preds = %51
%59 = load i32, i32* @_SC_CLK_TCK
%60 = call i32 @sysconf(i32 %59)
store i32 %60, i32* %8
%61 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%63 = load i32, i32* %62
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 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* %4, i32 0, i32 1
%68 = load i32, i32* %67
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%70 = load i32, i32* %69
%71 = sub nsw i32 %70, %68
store i32 %71, i32* %69
%72 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%73 = load i32, i32* %72
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %58
%76 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%77 = load i32, i32* %76
%78 = add nsw i32 %77, 1000000
store i32 %78, i32* %76
%79 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%80 = load i32, i32* %79
%81 = sub nsw i32 %80, 1
store i32 %81, i32* %79
br label %82
82: ; preds = %75, %58
%83 = load i32, i32* %8
%84 = sdiv i32 1000000, %83
%85 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%86 = load i32, i32* %85
%87 = add nsw i32 %86, %84
store i32 %87, i32* %85
%88 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%89 = load i32, i32* %88
%90 = icmp sge i32 %89, 1000000
br i1 %90, label %91, label %98
91: ; preds = %82
%92 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%93 = load i32, i32* %92
%94 = sub nsw i32 %93, 1000000
store i32 %94, i32* %92
%95 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%96 = load i32, i32* %95
%97 = add nsw i32 %96, 1
store i32 %97, i32* %95
br label %98
98: ; preds = %91, %82
%99 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%100 = load i32, i32* %99
%101 = icmp slt i32 %100, 2
br i1 %101, label %102, label %108
102: ; preds = %98
%103 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%104 = load i32, i32* %103
%105 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%106 = load i32, i32* %105
%107 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i32 %104, i32 %106)
store i32 1, i32* %1
br label %170
108: ; preds = %98
br label %109
109: ; preds = %108
br label %110
110: ; preds = %109
%111 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%112 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%113 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%114 = load i32, i32* %113
%115 = add nsw i32 %114, 2
store i32 %115, i32* %113
%116 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%117 = load i32, i32* %116
%118 = add nsw i32 %117, 1000000000
store i32 %118, i32* %116
%119 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
store i32 %119, i32* %6
%120 = load i32, i32* %6
%121 = icmp eq i32 %120, 0
br i1 %121, label %122, label %124
122: ; preds = %110
%123 = 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 %170
124: ; preds = %110
%125 = load i32, i32* %6
%126 = load i32, i32* @EINVAL
%127 = icmp ne i32 %125, %126
br i1 %127, label %128, label %131
128: ; preds = %124
%129 = load i32, i32* %6
%130 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %129)
store i32 1, i32* %1
br label %170
131: ; preds = %124
br label %132
132: ; preds = %131
%133 = call i64 @pthread_mutex_unlock(i32* %2)
%134 = icmp ne i64 %133, 0
br i1 %134, label %135, label %137
135: ; preds = %132
%136 = 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 %170
137: ; preds = %132
%138 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%139 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%140 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%141 = load i32, i32* %140
%142 = add nsw i32 %141, 2
store i32 %142, i32* %140
%143 = call i32 @pthread_mutex_timedlock(i32* %2, %struct.timespec* %3)
%144 = icmp ne i32 %143, 0
br i1 %144, label %145, label %147
145: ; preds = %137
%146 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
br label %147
147: ; preds = %145, %137
%148 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%149 = call i32 @timersub(%struct.timeval* %5, %struct.timeval* %4, %struct.timeval* %5)
%150 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%151 = load i32, i32* %150
%152 = icmp sgt i32 %151, 0
br i1 %152, label %157, label %153
153: ; preds = %147
%154 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%155 = load i32, i32* %154
%156 = icmp sgt i32 %155, 100000
br i1 %156, label %157, label %159
157: ; preds = %153, %147
%158 = 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 %170
159: ; preds = %153
%160 = call i64 @pthread_mutex_unlock(i32* %2)
%161 = icmp ne i64 %160, 0
br i1 %161, label %162, label %164
162: ; preds = %159
%163 = 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 %170
164: ; preds = %159
%165 = call i64 @pthread_mutex_destroy(i32* %2)
%166 = icmp ne i64 %165, 0
br i1 %166, label %167, label %169
167: ; preds = %164
%168 = 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 %170
169: ; preds = %164
store i32 0, i32* %1
br label %170
170: ; preds = %169, %167, %162, %157, %135, %128, %122, %102, %55, %49, %38, %33, %28, %23, %17, %11
%171 = load i32, i32* %1
ret i32 %171
}

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 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 %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
}