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

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int sscanf(char *, char *, int *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(void) {
  int seconds;
  char line[120];
  char message[64];
  pid_t pid;
  while (1) {
    printf("Alarm () : ");
    if (fgets(line, sizeof(line), stdin) == NULL)
      exit(0);
    if (strlen(line) <= 1)
      continue;
    if (sscanf(line, "%d %64[^\n]", &seconds, message) < 2) {
      fprintf(stderr, "Bad command\n");
    } else {
      pid = fork();
      if (pid == 0) {
        sleep(seconds);
        printf("(%d) %s\n", seconds, message);
        exit(0);
      } else if (pid > 0) {
        signal(SIGCHLD, SIG_IGN);
        continue;
      } else {
        printf("fork() failed\n");
        exit(1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    $0x78,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11d8 <main+0x48>
xor    %edi,%edi
call   1090 <exit@plt>
lea    -0x80(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jg     11ef <main+0x5f>
jmp    11a2 <main+0x12>
lea    -0x80(%rbp),%rdi
lea    -0xc0(%rbp),%rcx
lea    0xe0b(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jge    122d <main+0x9d>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b2 <main+0x122>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0xc8(%rbp)
cmpq   $0x0,-0xc8(%rbp)
jne    1270 <main+0xe0>
mov    -0x8(%rbp),%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%esi
lea    -0xc0(%rbp),%rdx
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0xc8(%rbp)
jle    129a <main+0x10a>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
jmp    11a2 <main+0x12>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11a2 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e22(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x50(%rsp),%rdi
mov    $0x78,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11e4 <main+0x44>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0x50(%rsp),%rbx
lea    0xe10(%rip),%r14
lea    0xe15(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    1244 <main+0xa4>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x78,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11dd <main+0x3d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     1225 <main+0x85>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jle    1210 <main+0x70>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     128e <main+0xee>
jle    12b5 <main+0x115>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
jmp    1225 <main+0x85>
mov    0xc(%rsp),%edi
call   10b0 <sleep@plt>
mov    0xc(%rsp),%esi
lea    0xd82(%rip),%rdi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd71(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Alarm () : \00"
@stdin = global i32 0
@.str.1 = [11 x i8] c"%d %64[^\0A]\00"
@stderr = global i32 0
@.str.2 = [13 x i8] c"Bad command\0A\00"
@.str.3 = [9 x i8] c"(%d) %s\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.4 = [15 x i8] c"fork() failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [120 x i8]
%4 = alloca [64 x i8]
%5 = alloca i64
store i32 0, i32* %1
br label %6
6: ; preds = %0, %18, %41, %48
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [120 x i8], [120 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32* @fgets(i8* %8, i32 120, i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %6
%15 = getelementptr inbounds [120 x i8], [120 x i8]* %3, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
%17 = icmp sle i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %14
br label %6
19: ; preds = %14
%20 = getelementptr inbounds [120 x i8], [120 x i8]* %3, i64 0, i64 0
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%22 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32* %2, i8* %21)
%23 = icmp slt i32 %22, 2
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %48
27: ; preds = %19
%28 = call i64 (...) @fork()
store i64 %28, i64* %5
%29 = load i64, i64* %5
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load i32, i32* %2
%33 = call i32 @sleep(i32 %32)
%34 = load i32, i32* %2
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %35)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %27
%39 = load i64, i64* %5
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i32, i32* @SIGCHLD
%43 = load i32, i32* @SIG_IGN
%44 = call i32 @signal(i32 %42, i32 %43)
br label %6
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %24
br label %6
}

LLVM IR (-O2):
@.str = [12 x i8] c"Alarm () : \00"
@stdin = global i32 0
@.str.1 = [11 x i8] c"%d %64[^\0A]\00"
@stderr = global i32 0
@.str.2 = [13 x i8] c"Bad command\0A\00"
@.str.3 = [9 x i8] c"(%d) %s\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@str = [14 x i8] c"fork() failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [120 x i8]
%3 = alloca [64 x i8]
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [120 x i8], [120 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32* @fgets(i8* %5, i32 120, i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %37, %0
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %0, %37
%14 = call i32 @strlen(i8* %5)
%15 = icmp slt i32 %14, 2
br i1 %15, label %37, label %16
16: ; preds = %13
%17 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32* %1, i8* %6)
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %37
22: ; preds = %16
%23 = call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i32, i32* %1
%27 = call i32 @sleep(i32 %26)
%28 = load i32, i32* %1
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %28, i8* %6)
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %22
%32 = icmp sgt i64 %23, 0
br i1 %32, label %33, label %42
33: ; preds = %31
%34 = load i32, i32* @SIGCHLD
%35 = load i32, i32* @SIG_IGN
%36 = call i32 @signal(i32 %34, i32 %35)
br label %37
37: ; preds = %33, %19, %13
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* @stdin
%40 = call i32* @fgets(i8* %5, i32 120, i32 %39)
%41 = icmp eq i32* %40, null
br i1 %41, label %11, label %13
42: ; preds = %31
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
}