#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 *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct tm *localtime(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_timedlock(int /*<<< orphan*/ *, struct timespec *);
char *strerror(int);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int main(void) {
  int err;
  struct timespec tout;
  struct tm *tmp;
  char buf[64];
  pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  pthread_mutex_lock(&lock);
  printf("mutex is locked\n");
  clock_gettime(CLOCK_REALTIME, &tout);
  tmp = localtime(&tout.tv_sec);
  strftime(buf, sizeof(buf), "%r", tmp);
  printf("current time is %s\n", buf);
  tout.tv_sec += 10;
  err = pthread_mutex_timedlock(&lock, &tout);
  clock_gettime(CLOCK_REALTIME, &tout);
  tmp = localtime(&tout.tv_sec);
  strftime(buf, sizeof(buf), "%r", tmp);
  printf("the time is now %s\n", buf);
  if (err == 0)
    printf("mutex locked again!\n");
  else
    printf("can't lock mutex again: %s\n", strerror(err));
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
lea    -0x64(%rbp),%rdi
call   10a0 <pthread_mutex_lock@plt>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x10(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x40,%esi
lea    0xe2c(%rip),%rdx
call   1070 <strftime@plt>
lea    -0x60(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x10(%rbp)
lea    -0x64(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <pthread_mutex_timedlock@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x10(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x40,%esi
lea    0xdc9(%rip),%rdx
call   1070 <strftime@plt>
lea    -0x60(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    127c <main+0xfc>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1295 <main+0x115>
mov    -0x8(%rbp),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   10b0 <pthread_mutex_lock@plt>
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e0d(%rip),%r13
mov    0x0(%r13),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1050 <clock_gettime@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
lea    0xe15(%rip),%r12
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1080 <strftime@plt>
lea    0xdf9(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
addl   $0xa,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <pthread_mutex_timedlock@plt>
mov    %eax,%ebp
mov    0x0(%r13),%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>
lea    0xdc3(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
jne    1275 <main+0xe5>
lea    0xdee(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xda8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

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.3 = [20 x i8] c"the time is now %s\0A\00"
@.str.5 = [28 x i8] c"can't lock mutex again: %s\0A\00"
@str = [16 x i8] c"mutex is locked\00"
@str.6 = [20 x i8] c"mutex locked again!\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = bitcast %struct.timespec* %1 to i8*
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %7, i32* %3
%8 = call i32 @pthread_mutex_lock(i32* %3)
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @CLOCK_REALTIME
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %1)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%13 = call %struct.tm* @localtime(i32* %12)
%14 = call i32 @strftime(i8* %5, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %13)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%16 = load i32, i32* %12
%17 = add nsw i32 %16, 10
store i32 %17, i32* %12
%18 = call i32 @pthread_mutex_timedlock(i32* %3, %struct.timespec* %1)
%19 = load i32, i32* @CLOCK_REALTIME
%20 = call i32 @clock_gettime(i32 %19, %struct.timespec* %1)
%21 = call %struct.tm* @localtime(i32* %12)
%22 = call i32 @strftime(i8* %5, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%24 = icmp eq i32 %18, 0
br i1 %24, label %25, label %27
25: ; preds = %0
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %30
27: ; preds = %0
%28 = call i8* @strerror(i32 %18)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %25
%31 = call i32 @exit(i32 0)
unreachable
}