#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*/ _exit(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int main() {
  FILE *fp, *fp2;
  int id = fork();
  if (id != 0) {
    fp = fopen("test.txt", "w+");
    if (fp != NULL)
      printf("O pai abriu\n");
    else
      exit(-1);
    fprintf(fp, "This is testing for fprintf...\n");
    fputs("This is testing for fputs...\n", fp);
    fclose(fp);
  } else {
    fp2 = fopen("test.txt", "w+");
    if (fp != NULL)
      printf("O filho abriu\n");
    else
      _exit(-1);
    fprintf(fp2, "2This is testing for fprintf...\n");
    fputs("2This is testing for fputs...\n", fp2);
    fclose(fp2);
    _exit(0);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     120e <main+0x8e>
lea    0xe56(%rip),%rdi
lea    0xe58(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d6 <main+0x56>
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
jmp    11e0 <main+0x60>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe4f(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
jmp    127b <main+0xfb>
lea    0xdeb(%rip),%rdi
lea    0xded(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1241 <main+0xc1>
lea    0xde2(%rip),%rdi
call   1050 <printf@plt>
jmp    124b <main+0xcb>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
lea    0xe7d(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1060 <fopen@plt>
test   %ebp,%ebp
jne    119d <main+0x2d>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11af <main+0x3f>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe94(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4a(%rip),%rdi
mov    $0x1f,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0xe51(%rip),%rdi
mov    $0x1d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [13 x i8] c"O pai abriu\0A\00"
@.str.3 = [32 x i8] c"This is testing for fprintf...\0A\00"
@.str.4 = [30 x i8] c"This is testing for fputs...\0A\00"
@.str.5 = [15 x i8] c"O filho abriu\0A\00"
@.str.6 = [33 x i8] c"2This is testing for fprintf...\0A\00"
@.str.7 = [31 x i8] c"2This is testing for fputs...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %0
%9 = 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* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %16
14: ; preds = %8
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %12
%17 = load i32*, i32** %2
%18 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32*, i32** %2
%20 = call i32 @fputs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32* %19)
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
br label %39
23: ; preds = %0
%24 = 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* %24, i32** %3
%25 = load i32*, i32** %2
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %31
29: ; preds = %23
%30 = call i32 @_exit(i32 -1)
unreachable
31: ; preds = %27
%32 = load i32*, i32** %3
%33 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%34 = load i32*, i32** %3
%35 = call i32 @fputs(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32* %34)
%36 = load i32*, i32** %3
%37 = call i32 @fclose(i32* %36)
%38 = call i32 @_exit(i32 0)
unreachable
39: ; preds = %16
%40 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.3 = [32 x i8] c"This is testing for fprintf...\0A\00"
@.str.4 = [30 x i8] c"This is testing for fputs...\0A\00"
@str = [12 x i8] c"O pai abriu\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
%3 = 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))
br i1 %2, label %14, label %4
4: ; preds = %0
%5 = icmp eq i32* %3, null
br i1 %5, label %12, label %6
6: ; preds = %4
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%8 = tail call i64 @fwrite(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 31, i64 1, i32* %3)
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 29, i64 1, i32* %3)
%10 = tail call i32 @fclose(i32* %3)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %4
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = tail call i32 @_exit(i32 -1)
unreachable
}