#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ wait(int /*<<< orphan*/ *);
void sighandler(int sig) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork()");
    exit(1);
  }
  if (!pid) {
    execl("/usr/bin/less", "less", "/etc/services", NULL);
    perror("exec()");
    exit(1);
  } else {
    if (wait(NULL) < 0)
      perror("wait()");
  }
}


x86 assembly (clang, -O0):
<sighandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1184 <sighandler+0x34>
lea    0xe8b(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11c2 <sighandler+0x72>
lea    0xe72(%rip),%rdi
lea    0xe74(%rip),%rsi
lea    0xe72(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <execl@plt>
lea    0xe70(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
cmp    $0x0,%rax
jge    11e1 <sighandler+0x91>
lea    0xe4e(%rip),%rdi
call   1030 <perror@plt>
jmp    11e6 <sighandler+0x96>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sighandler>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     1179 <sighandler+0x29>
je     1182 <sighandler+0x32>
xor    %edi,%edi
call   1060 <wait@plt>
test   %rax,%rax
js     116c <sighandler+0x1c>
pop    %rax
ret
lea    0xeb7(%rip),%rdi
pop    %rax
jmp    1030 <perror@plt>
lea    0xe80(%rip),%rdi
jmp    11a5 <sighandler+0x55>
lea    0xe7e(%rip),%rdi
lea    0xe80(%rip),%rsi
lea    0xe7e(%rip),%rdx
xor    %ecx,%ecx
call   1050 <execl@plt>
lea    0xe7e(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [14 x i8] c"/usr/bin/less\00"
@.str.2 = [5 x i8] c"less\00"
@.str.3 = [14 x i8] c"/etc/services\00"
@.str.4 = [7 x i8] c"exec()\00"
@.str.5 = [7 x i8] c"wait()\00"
define void @sighandler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* null)
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = call i64 @wait(i32* null)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %21
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [14 x i8] c"/usr/bin/less\00"
@.str.2 = [5 x i8] c"less\00"
@.str.3 = [14 x i8] c"/etc/services\00"
@.str.4 = [7 x i8] c"exec()\00"
@.str.5 = [7 x i8] c"wait()\00"
define void @sighandler(i32 %0) {
%2 = tail call i32 (...) @fork()
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = icmp eq i32 %2, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* null)
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = tail call i64 @wait(i32* null)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %13, %16
ret void
}