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

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIGUSR2;
int /*<<< orphan*/ SIG_BLOCK;
scalar_t__ alloca(size_t);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ pthread_sigmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
scalar_t__ sigignore(int /*<<< orphan*/);
void *tf(void *arg) {
  if (sigignore(SIGUSR1) != 0) {
    puts("sigignore failed");
    exit(1);
  }
  sigset_t ss;
  sigemptyset(&ss);
  sigaddset(&ss, SIGUSR2);
  if (pthread_sigmask(SIG_BLOCK, &ss, NULL) != 0) {
    puts("1st run: sigmask failed");
    exit(1);
  }
  char **oldargv = (char **)arg;
  size_t n = 1;
  while (oldargv[n] != NULL)
    ++n;
  char **argv = (char **)alloca((n + 1) * sizeof(char *));
  for (n = 0; oldargv[n + 1] != NULL; ++n)
    argv[n] = oldargv[n + 1];
  argv[n++] = (char *)"--direct";
  argv[n] = NULL;
  execv(argv[0], argv);
  puts("execv failed");
  exit(1);
}


x86 assembly (clang, -O0):
<tf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1030 <sigignore@plt>
cmp    $0x0,%rax
je     11ba <tf+0x3a>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1090 <sigaddset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <pthread_sigmask@plt>
cmp    $0x0,%rax
je     120b <tf+0x8b>
lea    0xe15(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     123f <tf+0xbf>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    121b <tf+0x9b>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x3,%rdi
call   1060 <alloca@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmpq   $0x0,0x8(%rax,%rcx,8)
je     129a <tf+0x11a>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x8(%rax,%rcx,8),%rdx
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    125c <tf+0xdc>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
lea    0xd75(%rip),%rdx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <execv@plt>
lea    0xd53(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<tf>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1030 <sigignore@plt>
test   %rax,%rax
jne    1202 <tf+0x82>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <sigaddset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <pthread_sigmask@plt>
test   %rax,%rax
jne    120b <tf+0x8b>
mov    $0x8,%edi
cs nopw 0x0(%rax,%rax,1)
nop
cmpq   $0x0,(%rbx,%rdi,1)
lea    0x8(%rdi),%rdi
jne    11e0 <tf+0x60>
call   1060 <alloca@plt>
mov    0x8(%rbx),%rdx
test   %rdx,%rdx
jne    1214 <tf+0x94>
mov    $0x1,%edx
xor    %ecx,%ecx
jmp    1236 <tf+0xb6>
lea    0xdf7(%rip),%rdi
jmp    125b <tf+0xdb>
lea    0xdff(%rip),%rdi
jmp    125b <tf+0xdb>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
mov    %rdx,(%rax,%rcx,8)
mov    0x10(%rbx,%rcx,8),%rdx
add    $0x1,%rcx
test   %rdx,%rdx
jne    1220 <tf+0xa0>
lea    0x1(%rcx),%rdx
lea    0xdec(%rip),%rsi
mov    %rsi,(%rax,%rcx,8)
movq   $0x0,(%rax,%rdx,8)
mov    (%rax),%rdi
mov    %rax,%rsi
call   10a0 <execv@plt>
lea    0xdd7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@.str = [17 x i8] c"sigignore failed\00"
@SIGUSR2 = global i32 0
@SIG_BLOCK = global i32 0
@.str.1 = [24 x i8] c"1st run: sigmask failed\00"
@.str.2 = [9 x i8] c"--direct\00"
@.str.3 = [13 x i8] c"execv failed\00"
define i8* @tf(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8**
store i8* %0, i8** %2
%7 = load i32, i32* @SIGUSR1
%8 = call i64 @sigignore(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = call i32 @sigemptyset(i32* %3)
%15 = load i32, i32* @SIGUSR2
%16 = call i32 @sigaddset(i32* %3, i32 %15)
%17 = load i32, i32* @SIG_BLOCK
%18 = call i64 @pthread_sigmask(i32 %17, i32* %3, i32* null)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = load i8*, i8** %2
%25 = bitcast i8* %24 to i8**
store i8** %25, i8*** %4
store i64 1, i64* %5
br label %26
26: ; preds = %32, %23
%27 = load i8**, i8*** %4
%28 = load i64, i64* %5
%29 = getelementptr inbounds i8*, i8** %27, i64 %28
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i64, i64* %5
%34 = add i64 %33, 1
store i64 %34, i64* %5
br label %26
35: ; preds = %26
%36 = load i64, i64* %5
%37 = add i64 %36, 1
%38 = mul i64 %37, 8
%39 = call i64 @alloca(i64 %38)
%40 = inttoptr i64 %39 to i8**
store i8** %40, i8*** %6
store i64 0, i64* %5
br label %41
41: ; preds = %57, %35
%42 = load i8**, i8*** %4
%43 = load i64, i64* %5
%44 = add i64 %43, 1
%45 = getelementptr inbounds i8*, i8** %42, i64 %44
%46 = load i8*, i8** %45
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %60
48: ; preds = %41
%49 = load i8**, i8*** %4
%50 = load i64, i64* %5
%51 = add i64 %50, 1
%52 = getelementptr inbounds i8*, i8** %49, i64 %51
%53 = load i8*, i8** %52
%54 = load i8**, i8*** %6
%55 = load i64, i64* %5
%56 = getelementptr inbounds i8*, i8** %54, i64 %55
store i8* %53, i8** %56
br label %57
57: ; preds = %48
%58 = load i64, i64* %5
%59 = add i64 %58, 1
store i64 %59, i64* %5
br label %41
60: ; preds = %41
%61 = load i8**, i8*** %6
%62 = load i64, i64* %5
%63 = add i64 %62, 1
store i64 %63, i64* %5
%64 = getelementptr inbounds i8*, i8** %61, i64 %62
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %64
%65 = load i8**, i8*** %6
%66 = load i64, i64* %5
%67 = getelementptr inbounds i8*, i8** %65, i64 %66
store i8* null, i8** %67
%68 = load i8**, i8*** %6
%69 = getelementptr inbounds i8*, i8** %68, i64 0
%70 = load i8*, i8** %69
%71 = load i8**, i8*** %6
%72 = call i32 @execv(i8* %70, i8** %71)
%73 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
@.str = [17 x i8] c"sigignore failed\00"
@SIGUSR2 = global i32 0
@SIG_BLOCK = global i32 0
@.str.1 = [24 x i8] c"1st run: sigmask failed\00"
@.str.2 = [9 x i8] c"--direct\00"
@.str.3 = [13 x i8] c"execv failed\00"
define noalias i8* @tf(i8* nocapture %0) {
%2 = alloca i32
%3 = load i32, i32* @SIGUSR1
%4 = tail call i64 @sigignore(i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = bitcast i32* %2 to i8*
%11 = call i32 @sigemptyset(i32* %2)
%12 = load i32, i32* @SIGUSR2
%13 = call i32 @sigaddset(i32* %2, i32 %12)
%14 = load i32, i32* @SIG_BLOCK
%15 = call i64 @pthread_sigmask(i32 %14, i32* %2, i32* null)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %9
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %9
%21 = bitcast i8* %0 to i8**
br label %22
22: ; preds = %22, %20
%23 = phi i64 [ 1, %20 ], [ %27, %22 ]
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
%27 = add i64 %23, 1
br i1 %26, label %28, label %22
28: ; preds = %22
%29 = shl i64 %23, 3
%30 = add i64 %29, 8
%31 = call i64 @alloca(i64 %30)
%32 = inttoptr i64 %31 to i8**
%33 = getelementptr inbounds i8, i8* %0, i64 8
%34 = bitcast i8* %33 to i8**
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %46, label %37
37: ; preds = %28, %37
%38 = phi i8* [ %44, %37 ], [ %35, %28 ]
%39 = phi i64 [ %42, %37 ], [ 1, %28 ]
%40 = phi i64 [ %39, %37 ], [ 0, %28 ]
%41 = getelementptr inbounds i8*, i8** %32, i64 %40
store i8* %38, i8** %41
%42 = add i64 %39, 1
%43 = getelementptr inbounds i8*, i8** %21, i64 %42
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %37
46: ; preds = %37, %28
%47 = phi i64 [ 0, %28 ], [ %39, %37 ]
%48 = phi i64 [ 1, %28 ], [ %42, %37 ]
%49 = getelementptr inbounds i8*, i8** %32, i64 %47
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %49
%50 = getelementptr inbounds i8*, i8** %32, i64 %48
store i8* null, i8** %50
%51 = load i8*, i8** %32
%52 = call i32 @execv(i8* %51, i8** %32)
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
}