#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGSTOP;
scalar_t__ foreground_pgid();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__);
int /*<<< orphan*/ get_foreground_command(char *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ move_to_background(scalar_t__, int, char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
void stop_handler(int signo) {
  pid_t pgid = foreground_pgid();
  char command[4096];
  get_foreground_command(command);
  int len = strlen(command) + 1;
  kill(-(int)pgid, SIGSTOP);
  if (pgid > 0)
    move_to_background(pgid, 1, command, len);
  fprintf(stderr, "move job %d to background\n", pgid);
}


x86 assembly (clang, -O0):
<stop_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <foreground_pgid@plt>
mov    %rax,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
call   1050 <get_foreground_command@plt>
lea    -0x1010(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1014(%rbp)
mov    -0x10(%rbp),%rax
xor    %edi,%edi
sub    %eax,%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11d6 <stop_handler+0x76>
mov    -0x10(%rbp),%rdi
lea    -0x1010(%rbp),%rdx
mov    -0x1014(%rbp),%ecx
mov    $0x1,%esi
call   1060 <move_to_background@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe16(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stop_handler>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
xor    %eax,%eax
call   1030 <foreground_pgid@plt>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1050 <get_foreground_command@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %ebx,%edi
neg    %edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
test   %rbx,%rbx
jle    11b4 <stop_handler+0x54>
add    $0x1,%ebp
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%ecx
call   1060 <move_to_background@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SIGSTOP = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"move job %d to background\0A\00"
define void @stop_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [4096 x i8]
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i64 (...) @foreground_pgid()
store i64 %6, i64* %3
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = call i32 @get_foreground_command(i8* %7)
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
store i32 %11, i32* %5
%12 = load i64, i64* %3
%13 = trunc i64 %12 to i32
%14 = sub nsw i32 0, %13
%15 = load i32, i32* @SIGSTOP
%16 = call i32 @kill(i32 %14, i32 %15)
%17 = load i64, i64* %3
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %1
%20 = load i64, i64* %3
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i32 @move_to_background(i64 %20, i32 1, i8* %21, i32 %22)
br label %24
24: ; preds = %19, %1
%25 = load i32, i32* @stderr
%26 = load i64, i64* %3
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %26)
ret void
}

LLVM IR (-O2):
@SIGSTOP = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"move job %d to background\0A\00"
define void @stop_handler(i32 %0) {
%2 = alloca [4096 x i8]
%3 = tail call i64 (...) @foreground_pgid()
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = call i32 @get_foreground_command(i8* %4)
%6 = call i32 @strlen(i8* %4)
%7 = trunc i64 %3 to i32
%8 = sub nsw i32 0, %7
%9 = load i32, i32* @SIGSTOP
%10 = call i32 @kill(i32 %8, i32 %9)
%11 = icmp sgt i64 %3, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = add nsw i32 %6, 1
%14 = call i32 @move_to_background(i64 %3, i32 1, i8* %4, i32 %13)
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %3)
ret void
}