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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, int, int, unsigned int);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  unsigned int time = 0;
  int len = 0;
  FILE *f = fopen("./pid.me", "w+");
  if (!f) {
    printf("open pid.me file failed, exit.\n");
    return -1;
  }
  while (1) {
    len = fprintf(f, "I'm process %d from process %d, i'm %d seconds old\n",
                  getpid(), getppid(), time * 10);
    if (!len) {
      printf("Something wrong, write to file 0 bytes.\n");
      return -2;
    } else {
      printf("I'm process %d from process %d, i'm %d seconds old\n", getpid(),
             getppid(), time * 10);
      fflush(f);
    }
    time++;
    sleep(10);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe6c(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <main+0x59>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <main+0x101>
jmp    11ce <main+0x5e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
imul   $0xa,-0x8(%rbp),%r8d
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1228 <main+0xb8>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1271 <main+0x101>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
imul   $0xa,-0x8(%rbp),%ecx
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0xa,%edi
call   1090 <sleep@plt>
jmp    11ce <main+0x5e>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124d <main+0xcd>
mov    %rax,%r13
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xe4b(%rip),%rsi
mov    %r13,%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %r8d,%r8d
call   1060 <fprintf@plt>
mov    $0xfffffffe,%r14d
lea    0xe82(%rip),%r15
test   %eax,%eax
je     125a <main+0xda>
lea    0xe24(%rip),%r12
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
call   1070 <fflush@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
add    $0xa,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %ebp,%r8d
call   1060 <fprintf@plt>
test   %eax,%eax
jne    11f0 <main+0x70>
jmp    125a <main+0xda>
mov    $0xffffffff,%r14d
lea    0xde6(%rip),%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"./pid.me\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [32 x i8] c"open pid.me file failed, exit.\0A\00"
@.str.3 = [52 x i8] c"I'm process %d from process %d, i'm %d seconds old\0A\00"
@.str.4 = [41 x i8] c"Something wrong, write to file 0 bytes.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %34
10: ; preds = %0
br label %11
11: ; preds = %10, %30
%12 = load i32*, i32** %4
%13 = call i32 (...) @getpid()
%14 = call i32 (...) @getppid()
%15 = load i32, i32* %2
%16 = mul i32 %15, 10
%17 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %13, i32 %14, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %11
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 -2, i32* %1
br label %34
22: ; preds = %11
%23 = call i32 (...) @getpid()
%24 = call i32 (...) @getppid()
%25 = load i32, i32* %2
%26 = mul i32 %25, 10
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %24, i32 %26)
%28 = load i32*, i32** %4
%29 = call i32 @fflush(i32* %28)
br label %30
30: ; preds = %22
%31 = load i32, i32* %2
%32 = add i32 %31, 1
store i32 %32, i32* %2
%33 = call i32 @sleep(i32 10)
br label %11
34: ; preds = %20, %8
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [9 x i8] c"./pid.me\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.3 = [52 x i8] c"I'm process %d from process %d, i'm %d seconds old\0A\00"
@str = [31 x i8] c"open pid.me file failed, exit.\00"
@str.5 = [40 x i8] c"Something wrong, write to file 0 bytes.\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %22, label %3
3: ; preds = %0
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (...) @getppid()
%6 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %3, %8
%9 = phi i32 [ %19, %8 ], [ 0, %3 ]
%10 = phi i32 [ %15, %8 ], [ 0, %3 ]
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (...) @getppid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %11, i32 %12, i32 %9)
%14 = tail call i32 @fflush(i32* %1)
%15 = add i32 %10, 1
%16 = tail call i32 @sleep(i32 10)
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (...) @getppid()
%19 = mul i32 %15, 10
%20 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %8
22: ; preds = %8, %3, %0
%23 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @str.5, i64 0, i64 0), %3 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @str.5, i64 0, i64 0), %8 ]
%24 = phi i32 [ -1, %0 ], [ -2, %3 ], [ -2, %8 ]
%25 = tail call i32 @puts(i8* dereferenceable(1) %23)
ret i32 %24
}