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

/* Variables and functions */
#define EAGAIN 129
#define EINTR 128
int SIGHUP;
int SIGQUIT;
int /*<<< orphan*/ dump_state();
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ prep_exit();
int /*<<< orphan*/ print_filename();
int /*<<< orphan*/ progressbar_error(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int sigtimedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timespec *);
int sigwaitinfo(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int);
void *info_thrd(void *arg) {
  sigset_t sigmask;
  struct timespec timespec = {.tv_sec = 1, .tv_nsec = 0};
  int sig, waiting = 0;
  sigemptyset(&sigmask);
  sigaddset(&sigmask, SIGQUIT);
  sigaddset(&sigmask, SIGHUP);
  while (1) {
    if (waiting)
      sig = sigtimedwait(&sigmask, NULL, &timespec);
    else
      sig = sigwaitinfo(&sigmask, NULL);
    if (sig == -1) {
      switch (errno) {
      case EAGAIN:
        waiting = 0;
      case EINTR:
        continue;
      default:
        do {
          progressbar_error("FATAL ERROR:"
                            "sigtimedwait/sigwaitinfo failed "
                            "because %s\n",
                            strerror(errno));
          do {
            prep_exit();
            exit(1);
          } while (0);
        } while (0);
      }
    }
    if (sig == SIGQUIT && !waiting) {
      print_filename();
      waiting = 1;
    } else
      dump_state();
  }
}


x86 assembly (clang, -O0):
<info_thrd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xe4d(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10c0 <sigaddset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   10c0 <sigaddset@plt>
cmpl   $0x0,-0x20(%rbp)
je     120e <info_thrd+0x6e>
lea    -0xc(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x18(%rbp),%rdx
call   1040 <sigtimedwait@plt>
mov    %eax,-0x1c(%rbp)
jmp    121e <info_thrd+0x7e>
lea    -0xc(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <sigwaitinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1295 <info_thrd+0xf5>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     125e <info_thrd+0xbe>
jmp    1244 <info_thrd+0xa4>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    1263 <info_thrd+0xc3>
jmp    1257 <info_thrd+0xb7>
movl   $0x0,-0x20(%rbp)
jmp    11eb <info_thrd+0x4b>
jmp    1268 <info_thrd+0xc8>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd89(%rip),%rdi
call   1070 <progressbar_error@plt>
mov    $0x0,%al
call   1030 <prep_exit@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    0x2d41(%rip),%rcx
cmp    (%rcx),%eax
jne    12c4 <info_thrd+0x124>
cmpl   $0x0,-0x20(%rbp)
jne    12c4 <info_thrd+0x124>
mov    $0x0,%al
call   1090 <print_filename@plt>
movl   $0x1,-0x20(%rbp)
jmp    12cb <info_thrd+0x12b>
mov    $0x0,%al
call   10b0 <dump_state@plt>
jmp    11eb <info_thrd+0x4b>

x86 assembly (clang, -O2):
<info_thrd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x1,(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e18(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   10c0 <sigaddset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10c0 <sigaddset@plt>
mov    %rsp,%r14
mov    0x2de2(%rip),%r12
xchg   %ax,%ax
xor    %ebp,%ebp
jmp    1207 <info_thrd+0x67>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   10b0 <dump_state@plt>
mov    %rbx,%rdi
xor    %esi,%esi
test   %ebp,%ebp
je     1240 <info_thrd+0xa0>
mov    %r14,%rdx
call   1040 <sigtimedwait@plt>
cmp    $0xffffffff,%eax
je     124a <info_thrd+0xaa>
cmp    (%r15),%eax
jne    1200 <info_thrd+0x60>
test   %ebp,%ebp
jne    1200 <info_thrd+0x60>
xor    %eax,%eax
call   1090 <print_filename@plt>
mov    $0x1,%ebp
jmp    1207 <info_thrd+0x67>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1060 <sigwaitinfo@plt>
cmp    $0xffffffff,%eax
jne    121d <info_thrd+0x7d>
mov    (%r12),%edi
cmp    $0x80,%edi
je     1207 <info_thrd+0x67>
cmp    $0x81,%edi
je     11f0 <info_thrd+0x50>
call   10a0 <strerror@plt>
lea    0xd96(%rip),%rdi
mov    %eax,%esi
call   1070 <progressbar_error@plt>
xor    %eax,%eax
call   1030 <prep_exit@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@__const.info_thrd.timespec = %struct.timespec { i32 1, i32 0 }
@SIGQUIT = global i32 0
@SIGHUP = global i32 0
@errno = global i32 0
@.str = [56 x i8] c"FATAL ERROR:sigtimedwait/sigwaitinfo failed because %s\0A\00"
define i8* @info_thrd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = bitcast %struct.timespec* %4 to i8*
store i32 0, i32* %6
%8 = call i32 @sigemptyset(i32* %3)
%9 = load i32, i32* @SIGQUIT
%10 = call i32 @sigaddset(i32* %3, i32 %9)
%11 = load i32, i32* @SIGHUP
%12 = call i32 @sigaddset(i32* %3, i32 %11)
br label %13
13: ; preds = %1, %26, %49
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @sigtimedwait(i32* %3, i32* null, %struct.timespec* %4)
store i32 %17, i32* %5
br label %20
18: ; preds = %13
%19 = call i32 @sigwaitinfo(i32* %3, i32* null)
store i32 %19, i32* %5
br label %20
20: ; preds = %18, %16
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %38
23: ; preds = %20
%24 = load i32, i32* @errno
switch i32 %24, label %27 [
i32 129, label %25
i32 128, label %26
]
25: ; preds = %23
store i32 0, i32* %6
br label %26
26: ; preds = %23, %25
br label %13
27: ; preds = %23
br label %28
28: ; preds = %27
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @progressbar_error(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28
%33 = call i32 (...) @prep_exit()
%34 = call i32 @exit(i32 1)
unreachable
35: ; No predecessors!
br label %36
36: ; preds = %35
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37, %20
%39 = load i32, i32* %5
%40 = load i32, i32* @SIGQUIT
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 (...) @print_filename()
store i32 1, i32* %6
br label %49
47: ; preds = %42, %38
%48 = call i32 (...) @dump_state()
br label %49
49: ; preds = %47, %45
br label %13
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@SIGQUIT = global i32 0
@SIGHUP = global i32 0
@errno = global i32 0
@.str = [56 x i8] c"FATAL ERROR:sigtimedwait/sigwaitinfo failed because %s\0A\00"
define noalias i8* @info_thrd(i8* nocapture readnone %0) {
%2 = alloca i32
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i64* %3 to i8*
store i64 1, i64* %3
%7 = call i32 @sigemptyset(i32* %2)
%8 = load i32, i32* @SIGQUIT
%9 = call i32 @sigaddset(i32* %2, i32 %8)
%10 = load i32, i32* @SIGHUP
%11 = call i32 @sigaddset(i32* %2, i32 %10)
br label %12
12: ; preds = %25, %1
%13 = phi i32 [ 0, %1 ], [ %26, %25 ]
%14 = icmp ne i32 %13, 0
br label %15
15: ; preds = %40, %12
br i1 %14, label %16, label %18
16: ; preds = %15
%17 = call i32 @sigtimedwait(i32* %2, i32* null, %struct.timespec* %4)
br label %20
18: ; preds = %15
%19 = call i32 @sigwaitinfo(i32* %2, i32* null)
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ %17, %16 ], [ %19, %18 ]
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = load i32, i32* @errno
switch i32 %24, label %27 [
i32 129, label %25
i32 128, label %40
]
25: ; preds = %23, %36
%26 = phi i32 [ 1, %36 ], [ 0, %23 ]
br label %12
27: ; preds = %23
%28 = call i32 @strerror(i32 %24)
%29 = call i32 @progressbar_error(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = call i32 (...) @prep_exit()
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = load i32, i32* @SIGQUIT
%34 = icmp ne i32 %21, %33
%35 = or i1 %34, %14
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 (...) @print_filename()
br label %25
38: ; preds = %32
%39 = call i32 (...) @dump_state()
br label %40
40: ; preds = %38, %23
br label %15
}