#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 /*<<< orphan*/ time_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strftime(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int *);
void gather_data(int fd) {
  pid_t pid = fork();
  int status;
  time_t now;
  char timestamp[1024];
  if (pid == 0) {
    if (dup2(fd, 1) < 0) {
      perror("unable to dup2()");
      exit(1);
    }
    now = time(0);
    strftime(timestamp, 1024, "%a, %d %b %y %T %z", localtime(&now));
    printf("%s\n", timestamp);
    execl("perfmon", "perfmon", 0);
    exit(1);
  } else if (pid < 0) {
    perror("unable to fork()");
    exit(1);
  }
  (void)wait(&status);
}


x86 assembly (clang, -O0):
<gather_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    125a <gather_data+0xba>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    11f1 <gather_data+0x51>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,-0x18(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x18(%rbp),%rdi
call   1030 <localtime@plt>
mov    -0x428(%rbp),%rdi
mov    %eax,%ecx
mov    $0x400,%esi
lea    0xdea(%rip),%rdx
call   1070 <strftime@plt>
lea    -0x420(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   1050 <printf@plt>
lea    0xde2(%rip),%rsi
xor    %edx,%edx
mov    %rsi,%rdi
call   10a0 <execl@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    127b <gather_data+0xdb>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1280 <gather_data+0xe0>
lea    -0x14(%rbp),%rdi
call   10b0 <wait@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gather_data>:
push   %rbx
sub    $0x410,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     11cb <gather_data+0x2b>
js     11e5 <gather_data+0x45>
lea    0x10(%rsp),%rdi
call   10b0 <wait@plt>
add    $0x410,%rsp
pop    %rbx
ret
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
test   %rax,%rax
jns    11fb <gather_data+0x5b>
lea    0xe1d(%rip),%rdi
jmp    11ec <gather_data+0x4c>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xdfa(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %eax,%ecx
call   1070 <strftime@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0xde7(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execl@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"unable to dup2()\00"
@.str.1 = [19 x i8] c"%a, %d %b %y %T %z\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [8 x i8] c"perfmon\00"
@.str.4 = [17 x i8] c"unable to fork()\00"
define void @gather_data(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i32 %0, i32* %2
%7 = call i64 (...) @fork()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %1
%11 = load i32, i32* %2
%12 = call i64 @dup2(i32 %11, i32 1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = call i32 @time(i32 0)
store i32 %18, i32* %5
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%20 = call i32 @localtime(i32* %5)
%21 = call i32 @strftime(i8* %19, i32 1024, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 0)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %1
%27 = load i64, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = call i32 @wait(i32* %4)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"unable to dup2()\00"
@.str.1 = [19 x i8] c"%a, %d %b %y %T %z\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [8 x i8] c"perfmon\00"
@.str.4 = [17 x i8] c"unable to fork()\00"
define void @gather_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = tail call i64 (...) @fork()
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = icmp eq i64 %5, 0
br i1 %9, label %10, label %23
10: ; preds = %1
%11 = tail call i64 @dup2(i32 %0, i32 1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32 @time(i32 0)
store i32 %17, i32* %3
%18 = call i32 @localtime(i32* %3)
%19 = call i32 @strftime(i8* %8, i32 1024, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%21 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 0)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %1
%24 = icmp slt i64 %5, 0
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %23
%29 = call i32 @wait(i32* %2)
ret void
}