#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*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int sscanf(char *, char *, int *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  char line[128];
  int seconds;
  pid_t pid;
  char message[64];
  while (1) {
    printf("Alarm> ");
    if (fgets(line, sizeof(line), stdin) == NULL)
      exit(1);
    if (strlen(line) <= 1)
      continue;
    system("date");
    if (sscanf(line, "%d %64[^\n]", &seconds, message) < 2) {
      fprintf(stderr, "Bad command\n");
    } else {
      pid = fork();
      if (pid == (pid_t)-1) {
        fprintf(stderr, "Fork failed\n");
        exit(1);
      }
      if (pid == (pid_t)0) {
        sleep(seconds);
        system("date");
        printf("(%d) %s\n", seconds, message);
        exit(0);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11e5 <main+0x55>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jg     11ff <main+0x6f>
jmp    11a9 <main+0x19>
lea    0xe02(%rip),%rdi
call   1040 <system@plt>
lea    -0x90(%rbp),%rdi
lea    -0xe0(%rbp),%rcx
lea    0xded(%rip),%rsi
lea    -0x94(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jge    124f <main+0xbf>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12d6 <main+0x146>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0xffffffffffffffff,-0xa0(%rbp)
ff
jne    128a <main+0xfa>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0xa0(%rbp)
jne    12d1 <main+0x141>
mov    -0x94(%rbp),%edi
call   10a0 <sleep@plt>
lea    0xd5e(%rip),%rdi
call   1040 <system@plt>
mov    -0x94(%rbp),%esi
lea    -0xe0(%rbp),%rdx
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    12d6 <main+0x146>
jmp    11a9 <main+0x19>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e32(%rip),%r15
mov    (%r15),%edx
lea    0x50(%rsp),%rdi
mov    $0x80,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11d7 <main+0x47>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0x50(%rsp),%rbx
lea    0xe1d(%rip),%r14
lea    0xe23(%rip),%r12
lea    0xc(%rsp),%r13
lea    0x10(%rsp),%rbp
jmp    1234 <main+0xa4>
cs nopw 0x0(%rax,%rax,1)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1070 <fprintf@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11cd <main+0x3d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jl     1215 <main+0x85>
lea    0xdc0(%rip),%rdi
call   1040 <system@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jle    1200 <main+0x70>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1294 <main+0x104>
cmp    $0xffffffffffffffff,%rax
jne    1215 <main+0x85>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0xc(%rsp),%edi
call   10a0 <sleep@plt>
lea    0xd64(%rip),%rdi
call   1040 <system@plt>
mov    0xc(%rsp),%esi
lea    0xd7e(%rip),%rdi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

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