#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 sigaction {
  scalar_t__ sa_handler;
};

/* Variables and functions */
int /*<<< orphan*/ SIGHUP;
scalar_t__ SIG_DFL;
scalar_t__ SIG_IGN;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     struct sigaction *);
int main(int argc, char *argv[]) {
  struct sigaction old_sa, new_sa;
  printf("sigaction (SIGHUP, NULL, &old_sa)...\n");
  if (sigaction(SIGHUP, NULL, &old_sa) < 0) {
    printf("cannot get signal action for SIGHUP: %m\n");
    exit(1);
  }
  if (old_sa.sa_handler != SIG_IGN && old_sa.sa_handler != SIG_DFL) {
    printf("SIGHUP action should be SIG_IGN, is %p\n",
           (void *)old_sa.sa_handler);
    exit(1);
  }
  printf("sigaction (SIGHUP, &new_sa, NULL)...\n");
  new_sa.sa_handler = SIG_DFL;
  if (sigaction(SIGHUP, &new_sa, NULL) < 0) {
    printf("cannot set signal action for SIGHUP: %m\n");
    exit(1);
  }
  printf("sigaction (SIGHUP, NULL, &old_sa)...\n");
  if (sigaction(SIGHUP, NULL, &old_sa) < 0) {
    printf("cannot get signal action for SIGHUP: %m\n");
    exit(1);
  }
  if (old_sa.sa_handler != SIG_DFL) {
    printf("SIGHUP action should be SIG_DFL, is %p\n",
           (void *)old_sa.sa_handler);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x18(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    118c <main+0x5c>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <main+0xa0>
mov    -0x18(%rbp),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <main+0xa0>
mov    -0x18(%rbp),%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1224 <main+0xf4>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x18(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    126a <main+0x13a>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
je     129a <main+0x16a>
mov    -0x18(%rbp),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xf79(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e96(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rdx
xor    %esi,%esi
call   1040 <sigaction@plt>
test   %rax,%rax
js     11e4 <main+0xa4>
mov    0x8(%rsp),%rsi
mov    0x2e67(%rip),%rax
mov    0x2e50(%rip),%r14
cmp    (%rax),%rsi
je     118a <main+0x4a>
cmp    (%r14),%rsi
jne    11ed <main+0xad>
lea    0xf11(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
js     11f6 <main+0xb6>
lea    0xf0f(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%edi
lea    0x8(%rsp),%rdx
xor    %esi,%esi
call   1040 <sigaction@plt>
test   %rax,%rax
js     11e4 <main+0xa4>
mov    0x8(%rsp),%rsi
cmp    (%r14),%rsi
jne    120e <main+0xce>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe15(%rip),%rdi
jmp    11fd <main+0xbd>
lea    0xe35(%rip),%rdi
jmp    1215 <main+0xd5>
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i64 }
@.str = [38 x i8] c"sigaction (SIGHUP, NULL, &old_sa)...\0A\00"
@SIGHUP = global i32 0
@.str.1 = [41 x i8] c"cannot get signal action for SIGHUP: %m\0A\00"
@SIG_IGN = global i64 0
@SIG_DFL = global i64 0
@.str.2 = [40 x i8] c"SIGHUP action should be SIG_IGN, is %p\0A\00"
@.str.3 = [38 x i8] c"sigaction (SIGHUP, &new_sa, NULL)...\0A\00"
@.str.4 = [41 x i8] c"cannot set signal action for SIGHUP: %m\0A\00"
@.str.5 = [40 x i8] c"SIGHUP action should be SIG_DFL, is %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @SIGHUP
%10 = call i64 @sigaction(i32 %9, %struct.sigaction* null, %struct.sigaction* %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @SIG_IGN
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %31
20: ; preds = %15
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i64, i64* @SIG_DFL
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i8*
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20, %15
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%33 = load i64, i64* @SIG_DFL
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
store i64 %33, i64* %34
%35 = load i32, i32* @SIGHUP
%36 = call i64 @sigaction(i32 %35, %struct.sigaction* %7, %struct.sigaction* null)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %31
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* @SIGHUP
%44 = call i64 @sigaction(i32 %43, %struct.sigaction* null, %struct.sigaction* %6)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i64, i64* @SIG_DFL
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
%56 = load i64, i64* %55
%57 = inttoptr i64 %56 to i8*
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %49
ret i32 0
}

LLVM IR (-O2):
%struct.sigaction = type { i64 }
@SIGHUP = global i32 0
@.str.1 = [41 x i8] c"cannot get signal action for SIGHUP: %m\0A\00"
@SIG_IGN = global i64 0
@SIG_DFL = global i64 0
@.str.2 = [40 x i8] c"SIGHUP action should be SIG_IGN, is %p\0A\00"
@.str.4 = [41 x i8] c"cannot set signal action for SIGHUP: %m\0A\00"
@.str.5 = [40 x i8] c"SIGHUP action should be SIG_DFL, is %p\0A\00"
@str.6 = [37 x i8] c"sigaction (SIGHUP, &new_sa, NULL)...\00"
@str.7 = [37 x i8] c"sigaction (SIGHUP, NULL, &old_sa)...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.7, i64 0, i64 0))
%8 = load i32, i32* @SIGHUP
%9 = call i64 @sigaction(i32 %8, %struct.sigaction* null, %struct.sigaction* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @SIG_IGN
%18 = icmp eq i64 %16, %17
%19 = load i64, i64* @SIG_DFL
%20 = icmp eq i64 %16, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %26, label %22
22: ; preds = %14
%23 = inttoptr i64 %16 to i8*
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %14
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0))
%28 = load i64, i64* @SIG_DFL
%29 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i64 %28, i64* %29
%30 = load i32, i32* @SIGHUP
%31 = call i64 @sigaction(i32 %30, %struct.sigaction* %4, %struct.sigaction* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.7, i64 0, i64 0))
%38 = load i32, i32* @SIGHUP
%39 = call i64 @sigaction(i32 %38, %struct.sigaction* null, %struct.sigaction* %3)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %36
%45 = load i64, i64* %15
%46 = load i64, i64* @SIG_DFL
%47 = icmp eq i64 %45, %46
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = inttoptr i64 %45 to i8*
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %44
ret i32 0
}