#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 */

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ time(long *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  long t, t2;
  int pid = 0;
  printf("Aberystwyth Multi-User Dungeon (Revision 5.21 BETA 5)\n(c) Copyright "
         "1987-1993, Alan Cox\n\n");
  close(0);
  close(1);
  ioctl(2, TIOCNOTTY, 0);
  close(2);
  setpgrp();
  if (fork() != 0)
    exit(1);
  open("server_log", O_WRONLY | O_CREAT | O_APPEND, 0600);
  dup(0);
  dup(0);
  while (1) {
    time(&t);
    argv[0] = "AberMUD 5.21 Beta1";
    pid = fork();
    if (pid == 0) {
      execvp("./server", &argv[0]);
      perror("./server");
      exit(1);
    } else if (pid != -1) {
      wait(NULL);
    }
    time(&t2);
    if (t2 - t < 10) {
      printf("Spawning too fast - error ??\n");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe1c(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10d0 <setpgrp@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%eax
je     123a <main+0x7a>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    0x2d6d(%rip),%rax
or     (%rax),%esi
lea    0xdfe(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
lea    -0x18(%rbp),%rdi
call   1070 <time@plt>
mov    -0x10(%rbp),%rax
lea    0xddd(%rip),%rcx
mov    %rcx,(%rax)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12c5 <main+0x105>
mov    -0x10(%rbp),%rsi
lea    0xdce(%rip),%rdi
call   10a0 <execvp@plt>
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0xffffffff,-0x24(%rbp)
je     12d8 <main+0x118>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
jmp    12dd <main+0x11d>
lea    -0x20(%rbp),%rdi
call   1070 <time@plt>
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
cmp    $0xa,%rax
jge    130e <main+0x14e>
lea    0xd82(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1274 <main+0xb4>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    $0x2,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   10d0 <setpgrp@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    12b8 <main+0xf8>
mov    0x2da1(%rip),%rax
mov    0x2dca(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d87(%rip),%rax
or     (%rax),%esi
lea    0xdbe(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
lea    0x10(%rsp),%r15
lea    0xda5(%rip),%r12
lea    0x8(%rsp),%rbx
jmp    1288 <main+0xc8>
nopl   (%rax)
mov    %rbx,%rdi
call   1070 <time@plt>
mov    0x8(%rsp),%rax
sub    0x10(%rsp),%rax
cmp    $0x9,%rax
jle    12ac <main+0xec>
mov    %r15,%rdi
call   1070 <time@plt>
mov    %r12,(%r14)
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0xb0>
test   %eax,%eax
je     12c2 <main+0x102>
xor    %edi,%edi
call   10c0 <wait@plt>
jmp    1270 <main+0xb0>
lea    0xdcd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd55(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
mov    %rbx,%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [90 x i8] c"Aberystwyth Multi-User Dungeon (Revision 5.21 BETA 5)\0A(c) Copyright 1987-1993, Alan Cox\0A\0A\00"
@TIOCNOTTY = global i32 0
@.str.1 = [11 x i8] c"server_log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [19 x i8] c"AberMUD 5.21 Beta1\00"
@.str.3 = [9 x i8] c"./server\00"
@.str.4 = [30 x i8] c"Spawning too fast - error ??\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%9 = call i32 @printf(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @close(i32 0)
%11 = call i32 @close(i32 1)
%12 = load i32, i32* @TIOCNOTTY
%13 = call i32 @ioctl(i32 2, i32 %12, i32 0)
%14 = call i32 @close(i32 2)
%15 = call i32 (...) @setpgrp()
%16 = call i32 (...) @fork()
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = load i32, i32* @O_APPEND
%25 = or i32 %23, %24
%26 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 384)
%27 = call i32 @dup(i32 0)
%28 = call i32 @dup(i32 0)
br label %29
29: ; preds = %20, %57
%30 = call i32 @time(i64* %6)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %32
%33 = call i32 (...) @fork()
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %29
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = call i32 @execvp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %38)
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = load i32, i32* %8
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @wait(i32* null)
br label %47
47: ; preds = %45, %42
br label %48
48: ; preds = %47
%49 = call i32 @time(i64* %7)
%50 = load i64, i64* %7
%51 = load i64, i64* %6
%52 = sub nsw i64 %50, %51
%53 = icmp slt i64 %52, 10
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %48
br label %29
}

LLVM IR (-O2):
@TIOCNOTTY = global i32 0
@.str.1 = [11 x i8] c"server_log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [19 x i8] c"AberMUD 5.21 Beta1\00"
@.str.3 = [9 x i8] c"./server\00"
@str = [89 x i8] c"Aberystwyth Multi-User Dungeon (Revision 5.21 BETA 5)\0A(c) Copyright 1987-1993, Alan Cox\0A\00"
@str.5 = [29 x i8] c"Spawning too fast - error ??\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([89 x i8], [89 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @close(i32 1)
%10 = load i32, i32* @TIOCNOTTY
%11 = tail call i32 @ioctl(i32 2, i32 %10, i32 0)
%12 = tail call i32 @close(i32 2)
%13 = tail call i32 (...) @setpgrp()
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_APPEND
%23 = or i32 %21, %22
%24 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 384)
%25 = tail call i32 @dup(i32 0)
%26 = tail call i32 @dup(i32 0)
br label %27
27: ; preds = %36, %18
%28 = call i32 @time(i64* %3)
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %1
%29 = call i32 (...) @fork()
switch i32 %29, label %34 [
i32 0, label %30
i32 -1, label %36
]
30: ; preds = %27
%31 = call i32 @execvp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %1)
%32 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = call i32 @wait(i32* null)
br label %36
36: ; preds = %27, %34
%37 = call i32 @time(i64* %4)
%38 = load i64, i64* %4
%39 = load i64, i64* %3
%40 = sub nsw i64 %38, %39
%41 = icmp slt i64 %40, 10
br i1 %41, label %42, label %27
42: ; preds = %36
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
}