#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 tm {
  int dummy;
};
struct timespec {
  int tv_sec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ PTHREAD_MUTEX_INITIALIZER;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
struct tm *localtime(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_timedlock(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int main(int argc, char *argv[]) {
  char buf[64];
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_mutex_lock(&mutex);
  printf("mutex is locked !\n");
  struct timespec ts;
  clock_gettime(CLOCK_REALTIME, &ts);
  struct tm *tmp = localtime(&ts.tv_sec);
  strftime(buf, sizeof(buf), "%r", tmp);
  printf("current time is %s\n", buf);
  ts.tv_sec += 10;
  int err = pthread_mutex_timedlock(&mutex, &ts);
  clock_gettime(CLOCK_REALTIME, &ts);
  tmp = localtime(&ts.tv_sec);
  strftime(buf, sizeof(buf), "%r", tmp);
  printf("current time is %s\n", buf);
  if (err == 0) {
    printf("mutex locked again !\n");
  } else {
    printf("can't lock mutex again!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
lea    -0x54(%rbp),%rdi
call   1080 <pthread_mutex_lock@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x58(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x60(%rbp),%rcx
mov    $0x40,%esi
lea    0xe47(%rip),%rdx
call   1070 <strftime@plt>
lea    -0x50(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x58(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x58(%rbp)
lea    -0x54(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1060 <pthread_mutex_timedlock@plt>
mov    %eax,-0x64(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x58(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x60(%rbp),%rcx
mov    $0x40,%esi
lea    0xde4(%rip),%rdx
call   1070 <strftime@plt>
lea    -0x50(%rbp),%rsi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x64(%rbp)
jne    1263 <main+0x103>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1271 <main+0x111>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <pthread_mutex_lock@plt>
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e2d(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1050 <clock_gettime@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
lea    0xe36(%rip),%r12
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1080 <strftime@plt>
lea    0xe1a(%rip),%r13
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
addl   $0xa,0x8(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <pthread_mutex_timedlock@plt>
mov    %eax,%ebp
mov    (%r14),%edi
mov    %rbx,%rsi
call   1050 <clock_gettime@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
mov    %r15,%rdi
mov    $0x40,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1080 <strftime@plt>
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
lea    0xdfd(%rip),%rax
lea    0xdde(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.tm = type { i32 }
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [19 x i8] c"mutex is locked !\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [3 x i8] c"%r\00"
@.str.2 = [20 x i8] c"current time is %s\0A\00"
@.str.3 = [22 x i8] c"mutex locked again !\0A\00"
@.str.4 = [25 x i8] c"can't lock mutex again!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [64 x i8]
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca %struct.tm*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %11, i32* %7
%12 = call i32 @pthread_mutex_lock(i32* %7)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %8)
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%17 = call %struct.tm* @localtime(i32* %16)
store %struct.tm* %17, %struct.tm** %9
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%19 = load %struct.tm*, %struct.tm** %9
%20 = call i32 @strftime(i8* %18, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 10
store i32 %25, i32* %23
%26 = call i32 @pthread_mutex_timedlock(i32* %7, %struct.timespec* %8)
store i32 %26, i32* %10
%27 = load i32, i32* @CLOCK_REALTIME
%28 = call i32 @clock_gettime(i32 %27, %struct.timespec* %8)
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%30 = call %struct.tm* @localtime(i32* %29)
store %struct.tm* %30, %struct.tm** %9
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%32 = load %struct.tm*, %struct.tm** %9
%33 = call i32 @strftime(i8* %31, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %32)
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %2
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %42
40: ; preds = %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %38
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.tm = type { i32 }
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.1 = [3 x i8] c"%r\00"
@.str.2 = [20 x i8] c"current time is %s\0A\00"
@str = [18 x i8] c"mutex is locked !\00"
@str.5 = [24 x i8] c"can't lock mutex again!\00"
@str.6 = [21 x i8] c"mutex locked again !\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %8, i32* %4
%9 = call i32 @pthread_mutex_lock(i32* %4)
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%11 = bitcast %struct.timespec* %5 to i8*
%12 = load i32, i32* @CLOCK_REALTIME
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %5)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%15 = call %struct.tm* @localtime(i32* %14)
%16 = call i32 @strftime(i8* %6, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%18 = load i32, i32* %14
%19 = add nsw i32 %18, 10
store i32 %19, i32* %14
%20 = call i32 @pthread_mutex_timedlock(i32* %4, %struct.timespec* %5)
%21 = load i32, i32* @CLOCK_REALTIME
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %5)
%23 = call %struct.tm* @localtime(i32* %14)
%24 = call i32 @strftime(i8* %6, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %23)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%26 = icmp eq i32 %20, 0
%27 = select i1 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0)
%28 = call i32 @puts(i8* dereferenceable(1) %27)
ret i32 0
}