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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, scalar_t__);
int time(int /*<<< orphan*/ *);
int main() {
  pid_t childpid;
  time_t start;
  time_t now;
  int i = 0;
  start = time(NULL);
  childpid = fork();
  if (childpid >= 0) {
    if (childpid == 0) {
      for (;;) {
        now = time(NULL);
        if (now - start > 30) {
          break;
        }
        i++;
      }
      exit(0);
    } else {
      printf("%d\n", childpid);
    }
  } else {
    perror("fork failed");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jl     11db <main+0x8b>
cmpq   $0x0,-0x8(%rbp)
jne    11c6 <main+0x76>
jmp    1191 <main+0x41>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
cmp    $0x1e,%eax
jle    11b1 <main+0x61>
jmp    11bf <main+0x6f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1191 <main+0x41>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1030 <printf@plt>
jmp    11f1 <main+0xa1>
lea    0xe22(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1195 <main+0x45>
je     1180 <main+0x30>
lea    0xe91(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1040 <time@plt>
sub    %ebx,%eax
cmp    $0x1f,%eax
jl     1180 <main+0x30>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [12 x i8] c"fork failed\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32 @time(i32* null)
store i32 %5, i32* %2
%6 = call i64 (...) @fork()
store i64 %6, i64* %1
%7 = load i64, i64* %1
%8 = icmp sge i64 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %9
br label %13
13: ; preds = %20, %12
%14 = call i32 @time(i32* null)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = load i32, i32* %2
%17 = sub nsw i32 %15, %16
%18 = icmp sgt i32 %17, 30
br i1 %18, label %19, label %20
19: ; preds = %13
br label %23
20: ; preds = %13
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %13
23: ; preds = %19
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %9
%26 = load i64, i64* %1
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %26)
br label %28
28: ; preds = %25
br label %32
29: ; preds = %0
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %28
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [12 x i8] c"fork failed\00"
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i64 (...) @fork()
%3 = icmp sgt i64 %2, -1
br i1 %3, label %4, label %14
4: ; preds = %0
%5 = icmp eq i64 %2, 0
br i1 %5, label %6, label %12
6: ; preds = %4, %6
%7 = tail call i32 @time(i32* null)
%8 = sub nsw i32 %7, %1
%9 = icmp sgt i32 %8, 30
br i1 %9, label %10, label %6
10: ; preds = %6
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %4
%13 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %2)
ret i32 0
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
}