#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 {
  void (*sa_handler)(int);
  int /*<<< orphan*/ sa_flags;
  int /*<<< orphan*/ sa_mask;
};

/* Variables and functions */
int /*<<< orphan*/ SA_RESTART;
void SIG_ERR(int);
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
scalar_t__ sigemptyset(int /*<<< orphan*/ *);
void (*putty_signal(int sig, void (*func)(int)))(int) {
  struct sigaction sa;
  struct sigaction old;
  sa.sa_handler = func;
  if (sigemptyset(&sa.sa_mask) < 0)
    return SIG_ERR;
  sa.sa_flags = SA_RESTART;
  if (sigaction(sig, &sa, &old) < 0)
    return SIG_ERR;
  return old.sa_handler;
}


x86 assembly (clang, -O0):
<putty_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
call   1040 <sigemptyset@plt>
cmp    $0x0,%rax
jge    115e <putty_signal+0x3e>
mov    0x2e83(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119c <putty_signal+0x7c>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1194 <putty_signal+0x74>
mov    0x2e4d(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119c <putty_signal+0x7c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putty_signal>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
mov    %rsi,0x8(%rsp)
lea    0x14(%rsp),%rdi
call   1040 <sigemptyset@plt>
mov    0x2e9a(%rip),%rbx
test   %rax,%rax
js     116b <putty_signal+0x4b>
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebp,%edi
call   1030 <sigaction@plt>
test   %rax,%rax
js     116b <putty_signal+0x4b>
mov    0x18(%rsp),%rbx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i32, i32 }
@SA_RESTART = global i32 0
define void (i32)* @putty_signal(i32 %0, void (i32)* %1) {
%3 = alloca void (i32)*
%4 = alloca i32
%5 = alloca void (i32)*
%6 = alloca %struct.sigaction
%7 = alloca %struct.sigaction
store i32 %0, i32* %4
store void (i32)* %1, void (i32)** %5
%8 = load void (i32)*, void (i32)** %5
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store void (i32)* %8, void (i32)** %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
%11 = call i64 @sigemptyset(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store void (i32)* @SIG_ERR, void (i32)** %3
br label %24
14: ; preds = %2
%15 = load i32, i32* @SA_RESTART
%16 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* %4
%18 = call i64 @sigaction(i32 %17, %struct.sigaction* %6, %struct.sigaction* %7)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store void (i32)* @SIG_ERR, void (i32)** %3
br label %24
21: ; preds = %14
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%23 = load void (i32)*, void (i32)** %22
store void (i32)* %23, void (i32)** %3
br label %24
24: ; preds = %21, %20, %13
%25 = load void (i32)*, void (i32)** %3
ret void (i32)* %25
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i32, i32 }
@SA_RESTART = global i32 0
define void (i32)* @putty_signal(i32 %0, void (i32)* %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store void (i32)* %1, void (i32)** %7
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
%9 = call i64 @sigemptyset(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %2
%12 = load i32, i32* @SA_RESTART
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
%18 = load void (i32)*, void (i32)** %17
br label %19
19: ; preds = %11, %2, %16
%20 = phi void (i32)* [ %18, %16 ], [ @SIG_ERR, %2 ], [ @SIG_ERR, %11 ]
ret void (i32)* %20
}