#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 {
  int sa_flags;
  int /*<<< orphan*/ sa_handler;
};
typedef int /*<<< orphan*/ __sighandler_t;

/* Variables and functions */
int /*<<< orphan*/ SIG_ERR;
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int sigaction(int, struct sigaction *, struct sigaction *);
__sighandler_t __signal(int sig, __sighandler_t handler, int flags) {
  int ret;
  struct sigaction action, oaction;
  memset(&action, 0, sizeof(struct sigaction));
  action.sa_handler = handler;
  action.sa_flags = flags;
  ret = sigaction(sig, &action, &oaction);
  return (ret == -1) ? SIG_ERR : oaction.sa_handler;
}


x86 assembly (clang, -O0):
<__signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sigaction@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    117b <__signal+0x5b>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1181 <__signal+0x61>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__signal>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %ebp,0xc(%rsp)
mov    %ebx,0x8(%rsp)
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
mov    %r15,%rsi
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    116b <__signal+0x4b>
mov    0x2e77(%rip),%rax
jmp    1170 <__signal+0x50>
lea    0x14(%rsp),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sigaction = type { i32, i32 }
@SIG_ERR = global i32 0
define i32 @__signal(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sigaction
%5 = alloca %struct.sigaction
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = bitcast %struct.sigaction* %5 to i8*
%8 = call i32 @memset(%struct.sigaction* %4, i32 0, i32 8)
%9 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i32 %1, i32* %9
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i32 %2, i32* %10
%11 = call i32 @sigaction(i32 %0, %struct.sigaction* %4, %struct.sigaction* %5)
%12 = icmp eq i32 %11, -1
%13 = load i32, i32* @SIG_ERR
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 1
%15 = load i32, i32* %14
%16 = select i1 %12, i32 %13, i32 %15
ret i32 %16
}