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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t p;
  printf("hello, my cat\n");
  FILE *fp;
  fp = fopen("data.in", "r+");
  assert(fp);
  fseek(fp, 1, SEEK_SET);
  p = fork();
  if (p > 0) {
    long pos;
    printf("parent process: child pid:%d\n", p);
    pos = ftell(fp);
    printf("pos:%ld\n", pos);
    fgetc(fp);
    pos = ftell(fp);
    printf("pos:%ld\n", pos);
    wait(NULL);
    fclose(fp);
  } else if (p == 0) {
    long pos;
    sleep(3);
    printf("child process\n");
    pos = ftell(fp);
    printf("pos:%ld\n", pos);
    fclose(fp);
  } else {
    perror("fork error:");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3b(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1080 <fseek@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    128a <main+0xda>
mov    -0x10(%rbp),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12eb <main+0x13b>
cmpq   $0x0,-0x10(%rbp)
jne    12da <main+0x12a>
mov    $0x3,%edi
call   10b0 <sleep@plt>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12e6 <main+0x136>
lea    0xd6f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12eb <main+0x13b>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
lea    0xe29(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <assert@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1090 <fseek@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
jle    1263 <main+0xa3>
lea    0xdf4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
lea    0xdf9(%rip),%r14
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
jmp    1294 <main+0xd4>
jne    12a6 <main+0xe6>
mov    $0x3,%edi
call   10c0 <sleep@plt>
lea    0xdd6(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
lea    0xd9f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd85(%rip),%rdi
call   10b0 <perror@plt>
jmp    129c <main+0xdc>

LLVM IR (-O0):
@.str = [15 x i8] c"hello, my cat\0A\00"
@.str.1 = [8 x i8] c"data.in\00"
@.str.2 = [3 x i8] c"r+\00"
@SEEK_SET = global i32 0
@.str.3 = [30 x i8] c"parent process: child pid:%d\0A\00"
@.str.4 = [9 x i8] c"pos:%ld\0A\00"
@.str.5 = [15 x i8] c"child process\0A\00"
@.str.6 = [12 x i8] c"fork error:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = call i32 @assert(i32* %8)
%10 = load i32*, i32** %3
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @fseek(i32* %10, i32 1, i32 %11)
%13 = call i64 (...) @fork()
store i64 %13, i64* %2
%14 = load i64, i64* %2
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %0
%17 = load i64, i64* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64 %17)
%19 = load i32*, i32** %3
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %21)
%23 = load i32*, i32** %3
%24 = call i32 @fgetc(i32* %23)
%25 = load i32*, i32** %3
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %4
%27 = load i64, i64* %4
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %27)
%29 = call i32 @wait(i32* null)
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
br label %47
32: ; preds = %0
%33 = load i64, i64* %2
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = call i32 @sleep(i32 3)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32*, i32** %3
%39 = call i64 @ftell(i32* %38)
store i64 %39, i64* %5
%40 = load i64, i64* %5
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %40)
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
br label %46
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %46
46: ; preds = %44, %35
br label %47
47: ; preds = %46, %16
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"data.in\00"
@.str.2 = [3 x i8] c"r+\00"
@SEEK_SET = global i32 0
@.str.3 = [30 x i8] c"parent process: child pid:%d\0A\00"
@.str.4 = [9 x i8] c"pos:%ld\0A\00"
@.str.6 = [12 x i8] c"fork error:\00"
@str = [14 x i8] c"hello, my cat\00"
@str.7 = [14 x i8] c"child process\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%3 = tail call i32 @assert(i32* %2)
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %2, i32 1, i32 %4)
%6 = tail call i64 (...) @fork()
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %0
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64 %6)
%10 = tail call i64 @ftell(i32* %2)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %10)
%12 = tail call i32 @fgetc(i32* %2)
%13 = tail call i64 @ftell(i32* %2)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %13)
%15 = tail call i32 @wait(i32* null)
%16 = tail call i32 @fclose(i32* %2)
br label %27
17: ; preds = %0
%18 = icmp eq i64 %6, 0
br i1 %18, label %19, label %25
19: ; preds = %17
%20 = tail call i32 @sleep(i32 3)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%22 = tail call i64 @ftell(i32* %2)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %22)
%24 = tail call i32 @fclose(i32* %2)
br label %27
25: ; preds = %17
%26 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %19, %25, %8
ret i32 0
}