#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 */

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
scalar_t__ print_yellow(int, char *, int);
scalar_t__ write(int, char *, int);
void sf_shild(int (*test_func)(void)) {
  int test_stdout;
  alarm(10);
  if ((test_stdout =
           open("./test_stdout.txt", O_RDWR | O_CREAT | O_TRUNC, 0755)) == -1 &&
      write(2, "shild's call to open returned -1\n", 33))
    exit(-1);
  if ((dup2(test_stdout, 1)) == -1 &&
      print_yellow(2, "shild's call to dup returned -1\n", 32))
    exit(-1);
  close(test_stdout);
  exit(test_func());
}


x86 assembly (clang, -O0):
<sf_shild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0xa,%edi
call   1060 <alarm@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
lea    0xe58(%rip),%rdi
mov    $0x1ed,%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e8 <sf_shild+0x78>
mov    $0x2,%edi
lea    0xe48(%rip),%rsi
mov    $0x21,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
je     11e8 <sf_shild+0x78>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1228 <sf_shild+0xb8>
mov    $0x2,%edi
lea    0xe2a(%rip),%rsi
mov    $0x20,%edx
call   1040 <print_yellow@plt>
cmp    $0x0,%rax
je     1228 <sf_shild+0xb8>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
call   *-0x8(%rbp)
mov    %eax,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<sf_shild>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0xa,%edi
call   1060 <alarm@plt>
mov    0x2e51(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
lea    0xe5e(%rip),%rdi
mov    $0x1ed,%edx
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ce <sf_shild+0x5e>
lea    0xe58(%rip),%rsi
mov    $0x2,%edi
mov    $0x21,%edx
call   1030 <write@plt>
test   %rax,%rax
jne    120a <sf_shild+0x9a>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11fa <sf_shild+0x8a>
lea    0xe4e(%rip),%rsi
mov    $0x2,%edi
mov    $0x20,%edx
call   1040 <print_yellow@plt>
test   %rax,%rax
jne    120a <sf_shild+0x9a>
mov    %ebp,%edi
call   1070 <close@plt>
call   *%rbx
mov    %eax,%edi
call   1090 <exit@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"./test_stdout.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [34 x i8] c"shild's call to open returned -1\0A\00"
@.str.2 = [33 x i8] c"shild's call to dup returned -1\0A\00"
define void @sf_shild(i32 ()* %0) {
%2 = alloca i32 ()*
%3 = alloca i32
store i32 ()* %0, i32 ()** %2
%4 = call i32 @alarm(i32 10)
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %9, i32 493)
store i32 %10, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = call i64 @write(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 33)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %12, %1
%18 = load i32, i32* %3
%19 = call i32 @dup2(i32 %18, i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = call i64 @print_yellow(i32 2, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 32)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %21, %17
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32 ()*, i32 ()** %2
%30 = call i32 %29()
%31 = call i32 @exit(i32 %30)
unreachable
}

LLVM IR (-O2):
@.str = [18 x i8] c"./test_stdout.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [34 x i8] c"shild's call to open returned -1\0A\00"
@.str.2 = [33 x i8] c"shild's call to dup returned -1\0A\00"
define void @sf_shild(i32 ()* nocapture %0) {
%2 = tail call i32 @alarm(i32 10)
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7, i32 493)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = tail call i64 @write(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 33)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %10, %1
%16 = tail call i32 @dup2(i32 %8, i32 1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = tail call i64 @print_yellow(i32 2, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 32)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %18, %15
%24 = tail call i32 @close(i32 %8)
%25 = tail call i32 %0()
%26 = tail call i32 @exit(i32 %25)
unreachable
}