#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);
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
};

/* Variables and functions */
scalar_t__ SIG_ERR;
scalar_t__ sigaction(int, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
void *lsignal(int s, void (*a)(int)) {
  struct sigaction sa, osa;
  sa.sa_handler = a;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(s, &sa, &osa) != 0)
    return ((void *)SIG_ERR);
  return ((void *)osa.sa_handler);
}


x86 assembly (clang, -O0):
<lsignal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
je     1179 <lsignal+0x59>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1181 <lsignal+0x61>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsignal>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebp
mov    %rsi,0x8(%rsp)
lea    0x18(%rsp),%rdi
call   1040 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x20(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <sigaction@plt>
test   %rax,%rax
je     1160 <lsignal+0x40>
mov    0x2e80(%rip),%rbx
mov    (%rbx),%rax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { void (i32)*, i64, i32 }
@SIG_ERR = global i64 0
define i8* @lsignal(i32 %0, void (i32)* %1) {
%3 = alloca i8*
%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 i32 @sigemptyset(i32* %10)
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i64 0, i64* %12
%13 = load i32, i32* %4
%14 = call i64 @sigaction(i32 %13, %struct.sigaction* %6, %struct.sigaction* %7)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i64, i64* @SIG_ERR
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %3
br label %23
19: ; preds = %2
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%21 = load void (i32)*, void (i32)** %20
%22 = bitcast void (i32)* %21 to i8*
store i8* %22, i8** %3
br label %23
23: ; preds = %19, %16
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
%struct.sigaction = type { void (i32)*, i64, i32 }
@SIG_ERR = global i64 0
define i8* @lsignal(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 i32 @sigemptyset(i32* %8)
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
store i64 0, i64* %10
%11 = call i64 @sigaction(i32 %0, %struct.sigaction* %3, %struct.sigaction* %4)
%12 = icmp eq i64 %11, 0
%13 = load i64, i64* @SIG_ERR
%14 = inttoptr i64 %13 to i8*
%15 = bitcast %struct.sigaction* %4 to i8**
%16 = load i8*, i8** %15
%17 = select i1 %12, i8* %16, i8* %14
ret i8* %17
}