#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;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ alarm(int);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int sigemptyset(int /*<<< orphan*/ *);
int sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ sigwait(int /*<<< orphan*/ *, int *);
int main(void) {
  sigset_t newmask, pendingset;
  int sig;
  struct timeval tv_ref, tv_cur;
  if (sigemptyset(&newmask) == -1 || sigemptyset(&pendingset) == -1) {
    printf("Error in sigemptyset()\n");
    return 2;
  }
  if (sigaddset(&newmask, SIGALRM) == -1) {
    perror("Error in sigaddset()\n");
    return 2;
  }
  if (sigprocmask(SIG_SETMASK, &newmask, NULL) == -1) {
    printf("Error in sigprocmask()\n");
    return 2;
  }
  if (gettimeofday(&tv_ref, NULL) != 0) {
    printf("Failed to get time of day\n");
    return 2;
  }
  alarm(3);
  if (sigwait(&newmask, &sig) != 0) {
    printf("Error in sigwait()\n");
    return 2;
  }
  if (gettimeofday(&tv_cur, NULL) != 0) {
    printf("Failed to get time of day\n");
    return 2;
  }
  if (tv_cur.tv_sec - tv_ref.tv_sec < 2) {
    printf("The operation lasted less than 3 seconds!\n");
    return 1;
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1070 <sigemptyset@plt>
cmp    $0xffffffff,%eax
je     11b3 <main+0x33>
lea    -0xc(%rbp),%rdi
call   1070 <sigemptyset@plt>
cmp    $0xffffffff,%eax
jne    11cb <main+0x4b>
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1306 <main+0x186>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   10a0 <sigaddset@plt>
cmp    $0xffffffff,%eax
jne    11fe <main+0x7e>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1306 <main+0x186>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0xb5>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1306 <main+0x186>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     1264 <main+0xe4>
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1306 <main+0x186>
mov    $0x3,%edi
call   1060 <alarm@plt>
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <sigwait@plt>
cmp    $0x0,%rax
je     129d <main+0x11d>
lea    0xdd5(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1306 <main+0x186>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     12cc <main+0x14c>
lea    0xd8b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1306 <main+0x186>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
cmp    $0x2,%eax
jge    12f3 <main+0x173>
lea    0xd93(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x186>
lea    0xda6(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
lea    0xc(%rsp),%rdi
call   1070 <sigemptyset@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0x72>
lea    0x24(%rsp),%rdi
call   1070 <sigemptyset@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0x72>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   10a0 <sigaddset@plt>
cmp    $0xffffffff,%eax
je     126c <main+0xec>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
je     1208 <main+0x88>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     1211 <main+0x91>
lea    0xe6f(%rip),%rdi
jmp    11f9 <main+0x79>
lea    0xe97(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%eax
add    $0x28,%rsp
ret
lea    0xe6a(%rip),%rdi
jmp    11f9 <main+0x79>
mov    $0x3,%edi
call   1060 <alarm@plt>
lea    0xc(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1090 <sigwait@plt>
test   %rax,%rax
je     1238 <main+0xb8>
lea    0xe16(%rip),%rdi
jmp    11f9 <main+0x79>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
jne    11e9 <main+0x69>
mov    0x10(%rsp),%eax
sub    0x18(%rsp),%eax
cmp    $0x1,%eax
jg     127a <main+0xfa>
lea    0xdc5(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x28,%rsp
ret
lea    0xd8d(%rip),%rdi
call   1080 <perror@plt>
jmp    11fe <main+0x7e>
lea    0xd95(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [24 x i8] c"Error in sigemptyset()\0A\00"
@SIGALRM = global i32 0
@.str.1 = [22 x i8] c"Error in sigaddset()\0A\00"
@SIG_SETMASK = global i32 0
@.str.2 = [24 x i8] c"Error in sigprocmask()\0A\00"
@.str.3 = [27 x i8] c"Failed to get time of day\0A\00"
@.str.4 = [20 x i8] c"Error in sigwait()\0A\00"
@.str.5 = [43 x i8] c"The operation lasted less than 3 seconds!\0A\00"
@.str.6 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
store i32 0, i32* %1
%7 = call i32 @sigemptyset(i32* %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = call i32 @sigemptyset(i32* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9, %0
%13 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %53
14: ; preds = %9
%15 = load i32, i32* @SIGALRM
%16 = call i32 @sigaddset(i32* %2, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %53
20: ; preds = %14
%21 = load i32, i32* @SIG_SETMASK
%22 = call i32 @sigprocmask(i32 %21, i32* %2, i32* null)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %53
26: ; preds = %20
%27 = call i64 @gettimeofday(%struct.timeval* %5, i32* null)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %53
31: ; preds = %26
%32 = call i32 @alarm(i32 3)
%33 = call i64 @sigwait(i32* %2, i32* %4)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %53
37: ; preds = %31
%38 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %53
42: ; preds = %37
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%46 = load i32, i32* %45
%47 = sub nsw i32 %44, %46
%48 = icmp slt i32 %47, 2
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %53
51: ; preds = %42
%52 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %53
53: ; preds = %51, %49, %40, %35, %29, %24, %18, %12
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@SIGALRM = global i32 0
@.str.1 = [22 x i8] c"Error in sigaddset()\0A\00"
@SIG_SETMASK = global i32 0
@str = [12 x i8] c"Test PASSED\00"
@str.7 = [42 x i8] c"The operation lasted less than 3 seconds!\00"
@str.9 = [19 x i8] c"Error in sigwait()\00"
@str.10 = [26 x i8] c"Failed to get time of day\00"
@str.11 = [23 x i8] c"Error in sigprocmask()\00"
@str.12 = [23 x i8] c"Error in sigemptyset()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast %struct.timeval* %4 to i8*
%10 = bitcast %struct.timeval* %5 to i8*
%11 = call i32 @sigemptyset(i32* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = call i32 @sigemptyset(i32* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13, %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.12, i64 0, i64 0))
br label %57
18: ; preds = %13
%19 = load i32, i32* @SIGALRM
%20 = call i32 @sigaddset(i32* %1, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %57
24: ; preds = %18
%25 = load i32, i32* @SIG_SETMASK
%26 = call i32 @sigprocmask(i32 %25, i32* %1, i32* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
br label %57
30: ; preds = %24
%31 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.10, i64 0, i64 0))
br label %57
35: ; preds = %30
%36 = call i32 @alarm(i32 3)
%37 = call i64 @sigwait(i32* %1, i32* %3)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
br label %57
41: ; preds = %35
%42 = call i64 @gettimeofday(%struct.timeval* %5, i32* null)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.10, i64 0, i64 0))
br label %57
46: ; preds = %41
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%50 = load i32, i32* %49
%51 = sub nsw i32 %48, %50
%52 = icmp slt i32 %51, 2
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.7, i64 0, i64 0))
br label %57
55: ; preds = %46
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %57
57: ; preds = %55, %53, %44, %39, %33, %28, %22, %16
%58 = phi i32 [ 2, %16 ], [ 2, %22 ], [ 2, %28 ], [ 2, %33 ], [ 2, %39 ], [ 2, %44 ], [ 1, %53 ], [ 0, %55 ]
ret i32 %58
}