#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 */
struct timeval {
  unsigned int tv_sec;
};
typedef int pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, char **);
int sscanf(char *, char *, unsigned int *);
scalar_t__ strcmp(char *, char *);
scalar_t__ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  struct timeval start, now;
  pid_t child_root;
  unsigned int duration = 1, num_tasks = 0;
  if (argc > 1) {
    int i;
    for (i = 0; i < argc; i++) {
      if (strcmp(argv[i], "--help") == 0) {
        printf("%s [duration]\n", argv[0]);
        exit(EXIT_SUCCESS);
      }
      if (sscanf(argv[i], "%u", &duration) == 1) {
        printf("%u second duration\n", duration);
        break;
      }
    }
  }
  {
    char *word;
    while (scanf("%as", &word) < 1) {
    }
    free(word);
  }
  if (gettimeofday(&start, NULL))
    exit(EXIT_FAILURE);
  child_root = fork();
  if (child_root == -1)
    exit(EXIT_FAILURE);
  do {
    if (child_root == 0) {
      if (fork() == -1)
        break;
    } else {
      if (wait(NULL) > 0)
        num_tasks++;
    }
    if (gettimeofday(&now, NULL) && (errno != EINTR))
      break;
  } while ((now.tv_sec - start.tv_sec) <= duration);
  if (child_root != 0)
    printf("Forked %d tasks\n", num_tasks);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1269 <main+0xd9>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1264 <main+0xd4>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe1c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1216 <main+0x86>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xded(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1251 <main+0xc1>
mov    -0x28(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1264 <main+0xd4>
jmp    1256 <main+0xc6>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11c5 <main+0x35>
jmp    1269 <main+0xd9>
jmp    126e <main+0xde>
lea    0xdb8(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <scanf@plt>
cmp    $0x1,%eax
jge    128c <main+0xfc>
jmp    126e <main+0xde>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     12ba <main+0x12a>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12dc <main+0x14c>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12e1 <main+0x151>
cmpl   $0x0,-0x24(%rbp)
jne    1305 <main+0x175>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
jne    1300 <main+0x170>
jmp    1370 <main+0x1e0>
jmp    1326 <main+0x196>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
cmp    $0x0,%rax
jle    1321 <main+0x191>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1326 <main+0x196>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     135c <main+0x1cc>
mov    0x2c9c(%rip),%rax
mov    (%rax),%rax
mov    0x2c82(%rip),%rcx
cmp    (%rcx),%rax
je     135c <main+0x1cc>
jmp    1370 <main+0x1e0>
jmp    1361 <main+0x1d1>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jbe    12e1 <main+0x151>
cmpl   $0x0,-0x24(%rbp)
je     138b <main+0x1fb>
mov    -0x2c(%rbp),%esi
lea    0xcad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c36(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0x4(%rsp)
cmp    $0x2,%edi
jl     1228 <main+0x98>
mov    %rsi,%rbx
mov    %edi,%r13d
xor    %ebp,%ebp
lea    0xe46(%rip),%r14
lea    0xe55(%rip),%r15
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx,%rbp,8),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1200 <main+0x70>
mov    (%rbx,%rbp,8),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     1216 <main+0x86>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11d0 <main+0x40>
jmp    1228 <main+0x98>
mov    (%rbx),%rsi
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    131a <main+0x18a>
mov    0x4(%rsp),%esi
lea    0xdf8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdfe(%rip),%rbx
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <scanf@plt>
test   %eax,%eax
jle    1240 <main+0xb0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
jne    1276 <main+0xe6>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
jne    1284 <main+0xf4>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %eax,%r15d
mov    0x8(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    0x2d49(%rip),%r12
mov    0x2d32(%rip),%r13
xor    %ebp,%ebp
jmp    12bc <main+0x12c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10(%rsp),%eax
sub    %ebx,%eax
cmp    0x4(%rsp),%eax
ja     1305 <main+0x175>
test   %r15d,%r15d
je     12e0 <main+0x150>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %ecx,%ecx
test   %rax,%rax
setg   %cl
add    %ecx,%ebp
jmp    12ec <main+0x15c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x18a>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     12b0 <main+0x120>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     12b0 <main+0x120>
test   %r15d,%r15d
je     131a <main+0x18a>
lea    0xd20(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [7 x i8] c"--help\00"
@.str.1 = [15 x i8] c"%s [duration]\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [20 x i8] c"%u second duration\0A\00"
@.str.4 = [4 x i8] c"%as\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.5 = [17 x i8] c"Forked %d tasks\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %9
store i32 0, i32* %10
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %51
15: ; preds = %2
store i32 0, i32* %11
br label %16
16: ; preds = %47, %15
%17 = load i32, i32* %11
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %50
20: ; preds = %16
%21 = load i8**, i8*** %5
%22 = load i32, i32* %11
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %20
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 0
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %20
%36 = load i8**, i8*** %5
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @sscanf(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i32, i32* %9
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %44)
br label %50
46: ; preds = %35
br label %47
47: ; preds = %46
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %16
50: ; preds = %43, %16
br label %51
51: ; preds = %50, %2
br label %52
52: ; preds = %55, %51
%53 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %12)
%54 = icmp slt i32 %53, 1
br i1 %54, label %55, label %56
55: ; preds = %52
br label %52
56: ; preds = %52
%57 = load i8*, i8** %12
%58 = call i32 @free(i8* %57)
%59 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %56
%65 = call i32 (...) @fork()
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %64
br label %72
72: ; preds = %96, %71
%73 = load i32, i32* %8
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = call i32 (...) @fork()
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %79
78: ; preds = %75
br label %104
79: ; preds = %75
br label %87
80: ; preds = %72
%81 = call i64 @wait(i32* null)
%82 = icmp sgt i64 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* %10
%85 = add i32 %84, 1
store i32 %85, i32* %10
br label %86
86: ; preds = %83, %80
br label %87
87: ; preds = %86, %79
%88 = call i64 @gettimeofday(%struct.timeval* %7, i32* null)
%89 = icmp ne i64 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %87
%91 = load i64, i64* @errno
%92 = load i64, i64* @EINTR
%93 = icmp ne i64 %91, %92
br i1 %93, label %94, label %95
94: ; preds = %90
br label %104
95: ; preds = %90, %87
br label %96
96: ; preds = %95
%97 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%98 = load i32, i32* %97
%99 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%100 = load i32, i32* %99
%101 = sub i32 %98, %100
%102 = load i32, i32* %9
%103 = icmp ule i32 %101, %102
br i1 %103, label %72, label %104
104: ; preds = %96, %94, %78
%105 = load i32, i32* %8
%106 = icmp ne i32 %105, 0
br i1 %106, label %107, label %110
107: ; preds = %104
%108 = load i32, i32* %10
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %108)
br label %110
110: ; preds = %107, %104
%111 = load i32, i32* @EXIT_SUCCESS
%112 = call i32 @exit(i32 %111)
unreachable
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [7 x i8] c"--help\00"
@.str.1 = [15 x i8] c"%s [duration]\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [20 x i8] c"%u second duration\0A\00"
@.str.4 = [4 x i8] c"%as\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.5 = [17 x i8] c"Forked %d tasks\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i8*
%7 = bitcast %struct.timeval* %3 to i8*
%8 = bitcast %struct.timeval* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %34
11: ; preds = %2
%12 = zext i32 %0 to i64
br label %15
13: ; preds = %26
%14 = icmp eq i64 %30, %12
br i1 %14, label %34, label %15
15: ; preds = %11, %13
%16 = phi i64 [ 0, %11 ], [ %30, %13 ]
%17 = getelementptr inbounds i8*, i8** %1, i64 %16
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i8*, i8** %1
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_SUCCESS
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %15
%27 = load i8*, i8** %17
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%29 = icmp eq i32 %28, 1
%30 = add nuw nsw i64 %16, 1
br i1 %29, label %31, label %13
31: ; preds = %26
%32 = load i32, i32* %5
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %13, %31, %2
%35 = bitcast i8** %6 to i8*
br label %36
36: ; preds = %36, %34
%37 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %6)
%38 = icmp slt i32 %37, 1
br i1 %38, label %36, label %39
39: ; preds = %36
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
%42 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %39
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = call i32 (...) @fork()
%49 = icmp eq i32 %48, -1
br i1 %49, label %55, label %50
50: ; preds = %47
%51 = icmp eq i32 %48, 0
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%54 = load i32, i32* %53
br label %58
55: ; preds = %47
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %50, %76
%59 = phi i32 [ %69, %76 ], [ 0, %50 ]
br i1 %51, label %60, label %63
60: ; preds = %58
%61 = call i32 (...) @fork()
%62 = icmp eq i32 %61, -1
br i1 %62, label %84, label %68
63: ; preds = %58
%64 = call i64 @wait(i32* null)
%65 = icmp sgt i64 %64, 0
%66 = zext i1 %65 to i32
%67 = add i32 %59, %66
br label %68
68: ; preds = %63, %60
%69 = phi i32 [ %59, %60 ], [ %67, %63 ]
%70 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%71 = icmp eq i64 %70, 0
br i1 %71, label %76, label %72
72: ; preds = %68
%73 = load i64, i64* @errno
%74 = load i64, i64* @EINTR
%75 = icmp eq i64 %73, %74
br i1 %75, label %76, label %81
76: ; preds = %68, %72
%77 = load i32, i32* %52
%78 = sub i32 %77, %54
%79 = load i32, i32* %5
%80 = icmp ugt i32 %78, %79
br i1 %80, label %81, label %58
81: ; preds = %72, %76
br i1 %51, label %84, label %82
82: ; preds = %81
%83 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %69)
br label %84
84: ; preds = %60, %82, %81
%85 = load i32, i32* @EXIT_SUCCESS
%86 = call i32 @exit(i32 %85)
unreachable
}