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

/* Variables and functions */
scalar_t__ ETIMEDOUT;
int PTHREAD_CANCEL_DEFERRED;
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ TIMEVAL_TO_TIMESPEC(struct timeval *, struct timespec *);
scalar_t__ errno;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, scalar_t__);
scalar_t__ pthread_setcanceltype(int, int *);
int /*<<< orphan*/ puts(char *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int do_test(void) {
  sem_t s;
  if (sem_init(&s, 0, 0) == -1) {
    puts("sem_init failed");
    return 1;
  }
  struct timeval tv;
  if (gettimeofday(&tv, NULL) != 0) {
    puts("gettimeofday failed");
    return 1;
  }
  struct timespec ts;
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_sec -= 86400;
  int type_before;
  if (pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &type_before) != 0) {
    puts("first pthread_setcanceltype failed");
    return 1;
  }
  errno = 0;
  if (TEMP_FAILURE_RETRY(sem_timedwait(&s, &ts)) != -1) {
    puts("sem_timedwait succeeded");
    return 1;
  }
  if (errno != ETIMEDOUT) {
    printf("sem_timedwait return errno = %d instead of ETIMEDOUT\n", errno);
    return 1;
  }
  int type_after;
  if (pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &type_after) != 0) {
    puts("second pthread_setcanceltype failed");
    return 1;
  }
  if (type_after != PTHREAD_CANCEL_DEFERRED) {
    puts("sem_timedwait changed cancellation type");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11b6 <do_test+0x36>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
cmp    $0x0,%rax
je     11e5 <do_test+0x65>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x18(%rbp),%eax
sub    $0x15180,%eax
mov    %eax,-0x18(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   1080 <pthread_setcanceltype@plt>
cmp    $0x0,%rax
je     1231 <do_test+0xb1>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
mov    0x2da8(%rip),%rax
movq   $0x0,(%rax)
lea    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1274 <do_test+0xf4>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
mov    0x2d65(%rip),%rax
mov    (%rax),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
je     12b0 <do_test+0x130>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rsi
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <pthread_setcanceltype@plt>
cmp    $0x0,%rax
je     12e4 <do_test+0x164>
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
mov    -0x20(%rbp),%eax
mov    0x2ce2(%rip),%rcx
cmp    (%rcx),%eax
je     130e <do_test+0x18e>
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <do_test+0x195>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11b4 <do_test+0x34>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
test   %rax,%rax
je     11bd <do_test+0x3d>
lea    0xe5e(%rip),%rdi
jmp    1220 <do_test+0xa0>
lea    0xe45(%rip),%rdi
jmp    1220 <do_test+0xa0>
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
addl   $0xfffeae80,(%rsp)
mov    0x2df8(%rip),%rbx
mov    (%rbx),%edi
lea    0x14(%rsp),%rsi
call   1080 <pthread_setcanceltype@plt>
test   %rax,%rax
je     11f2 <do_test+0x72>
lea    0xe34(%rip),%rdi
jmp    1220 <do_test+0xa0>
mov    0x2de7(%rip),%r14
movq   $0x0,(%r14)
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1232 <do_test+0xb2>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%rsi
mov    0x2d9c(%rip),%rax
cmp    (%rax),%rsi
jne    125b <do_test+0xdb>
mov    (%rbx),%edi
lea    0xc(%rsp),%rsi
call   1080 <pthread_setcanceltype@plt>
test   %rax,%rax
je     1269 <do_test+0xe9>
lea    0xe3c(%rip),%rdi
jmp    1220 <do_test+0xa0>
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
jmp    1225 <do_test+0xa5>
mov    0xc(%rsp),%ecx
xor    %eax,%eax
cmp    (%rbx),%ecx
je     122a <do_test+0xaa>
lea    0xe3f(%rip),%rdi
jmp    1220 <do_test+0xa0>

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.timespec = type { i32 }
@.str = [16 x i8] c"sem_init failed\00"
@.str.1 = [20 x i8] c"gettimeofday failed\00"
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str.2 = [35 x i8] c"first pthread_setcanceltype failed\00"
@errno = global i64 0
@.str.3 = [24 x i8] c"sem_timedwait succeeded\00"
@ETIMEDOUT = global i64 0
@.str.4 = [54 x i8] c"sem_timedwait return errno = %d instead of ETIMEDOUT\0A\00"
@.str.5 = [36 x i8] c"second pthread_setcanceltype failed\00"
@.str.6 = [40 x i8] c"sem_timedwait changed cancellation type\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
%7 = call i32 @sem_init(i32* %2, i32 0, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %52
11: ; preds = %0
%12 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %52
16: ; preds = %11
%17 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %4)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = sub nsw i32 %19, 86400
store i32 %20, i32* %18
%21 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%22 = call i64 @pthread_setcanceltype(i32 %21, i32* %5)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %52
26: ; preds = %16
store i64 0, i64* @errno
%27 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %4)
%28 = call i32 @TEMP_FAILURE_RETRY(i32 %27)
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %52
32: ; preds = %26
%33 = load i64, i64* @errno
%34 = load i64, i64* @ETIMEDOUT
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i64 %37)
store i32 1, i32* %1
br label %52
39: ; preds = %32
%40 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%41 = call i64 @pthread_setcanceltype(i32 %40, i32* %6)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %52
45: ; preds = %39
%46 = load i32, i32* %6
%47 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %52
51: ; preds = %45
store i32 0, i32* %1
br label %52
52: ; preds = %51, %49, %43, %36, %30, %24, %14, %9
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.timespec = type { i32 }
@.str = [16 x i8] c"sem_init failed\00"
@.str.1 = [20 x i8] c"gettimeofday failed\00"
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str.2 = [35 x i8] c"first pthread_setcanceltype failed\00"
@errno = global i64 0
@.str.3 = [24 x i8] c"sem_timedwait succeeded\00"
@ETIMEDOUT = global i64 0
@.str.4 = [54 x i8] c"sem_timedwait return errno = %d instead of ETIMEDOUT\0A\00"
@.str.5 = [36 x i8] c"second pthread_setcanceltype failed\00"
@.str.6 = [40 x i8] c"sem_timedwait changed cancellation type\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = call i32 @sem_init(i32* %1, i32 0, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %59
11: ; preds = %0
%12 = bitcast %struct.timeval* %2 to i8*
%13 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %57
17: ; preds = %11
%18 = bitcast %struct.timespec* %3 to i8*
%19 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %2, %struct.timespec* %3)
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, -86400
store i32 %22, i32* %20
%23 = bitcast i32* %4 to i8*
%24 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%25 = call i64 @pthread_setcanceltype(i32 %24, i32* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %17
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %55
29: ; preds = %17
store i64 0, i64* @errno
%30 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %3)
%31 = call i32 @TEMP_FAILURE_RETRY(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %55
35: ; preds = %29
%36 = load i64, i64* @errno
%37 = load i64, i64* @ETIMEDOUT
%38 = icmp eq i64 %36, %37
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i64 %36)
br label %55
41: ; preds = %35
%42 = bitcast i32* %5 to i8*
%43 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%44 = call i64 @pthread_setcanceltype(i32 %43, i32* %5)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = load i32, i32* %5
%48 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%49 = icmp eq i32 %47, %48
br i1 %49, label %53, label %50
50: ; preds = %46, %41
%51 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), %41 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), %46 ]
%52 = call i32 @puts(i8* dereferenceable(1) %51)
br label %53
53: ; preds = %50, %46
%54 = phi i32 [ 0, %46 ], [ 1, %50 ]
br label %55
55: ; preds = %53, %39, %33, %27
%56 = phi i32 [ 1, %27 ], [ 1, %33 ], [ 1, %39 ], [ %54, %53 ]
br label %57
57: ; preds = %55, %15
%58 = phi i32 [ 1, %15 ], [ %56, %55 ]
br label %59
59: ; preds = %57, %9
%60 = phi i32 [ 1, %9 ], [ %58, %57 ]
ret i32 %60
}