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

/* Variables and functions */
int EXIT_SUCCESS;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int dup(int);
int errno;
scalar_t__ fork();
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd1, fd2, fd3;
  if ((fd1 = open("./fich1", O_RDWR | O_CREAT | O_TRUNC, 0600)) == -1) {
    perror("./fich1");
    return errno;
  }
  if (write(fd1, "abcde", strlen("abcde")) == -1) {
    perror("./fich1");
    return errno;
  }
  if (fork() == 0) {
    if ((fd2 = open("./fich1", O_RDWR)) == -1) {
      perror("./fich1");
      return errno;
    }
    if (write(fd1, "123", strlen("123")) == -1) {
      perror("./fich1");
      return errno;
    }
    if (write(fd2, "45", strlen("45")) == -1) {
      perror("./fich1");
      return errno;
    }
    close(fd2);
  } else {
    fd3 = dup(fd1);
    if (lseek(fd3, 0, SEEK_SET) == -1) {
      perror("./fich1");
      return errno;
    }
    if (write(fd3, "fg", strlen("fg")) == -1) {
      perror("./fich1");
      return errno;
    }
    if (write(fd1, "hi", strlen("hi")) == -1) {
      perror("./fich1");
      return errno;
    }
    wait(NULL);
    close(fd1);
    close(fd3);
    return EXIT_SUCCESS;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11f6 <main+0x66>
lea    0xe20(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    0xe05(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xdf4(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    123f <main+0xaf>
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    132f <main+0x19f>
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1290 <main+0x100>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    0xd71(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xd60(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12d9 <main+0x149>
lea    0xd3d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0xd2c(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xd1b(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1322 <main+0x192>
lea    0xcf4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    142a <main+0x29a>
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2c7c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1373 <main+0x1e3>
lea    0xca3(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    0xc95(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xc84(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    13bc <main+0x22c>
lea    0xc5a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xc4f(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xc3e(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1405 <main+0x275>
lea    0xc11(%rip),%rdi
call   1090 <perror@plt>
mov    0x2bcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    142a <main+0x29a>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    0x2b93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e3d(%rip),%r14
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe49(%rip),%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
mov    %eax,%ebx
lea    0xe33(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1290 <main+0x100>
mov    %ebx,%edi
call   1050 <dup@plt>
mov    %eax,%r14d
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
lea    0xde6(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
lea    0xdc4(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2d2a(%rip),%rax
jmp    1306 <main+0x176>
mov    (%r14),%esi
lea    0xd66(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
mov    %eax,%ebp
lea    0xd5f(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
lea    0xd43(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x163>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    1308 <main+0x178>
lea    0xd06(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"./fich1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"abcde\00"
@.str.2 = [4 x i8] c"123\00"
@.str.3 = [3 x i8] c"45\00"
@SEEK_SET = global i32 0
@.str.4 = [3 x i8] c"fg\00"
@.str.5 = [3 x i8] c"hi\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9, i32 384)
store i32 %10, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @errno
store i32 %14, i32* %1
br label %85
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @write(i32 %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @errno
store i32 %22, i32* %1
br label %85
23: ; preds = %15
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %52
26: ; preds = %23
%27 = load i32, i32* @O_RDWR
%28 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 %28, i32* %3
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @errno
store i32 %32, i32* %1
br label %85
33: ; preds = %26
%34 = load i32, i32* %2
%35 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @write(i32 %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @errno
store i32 %40, i32* %1
br label %85
41: ; preds = %33
%42 = load i32, i32* %3
%43 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @write(i32 %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%48 = load i32, i32* @errno
store i32 %48, i32* %1
br label %85
49: ; preds = %41
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
br label %85
52: ; preds = %23
%53 = load i32, i32* %2
%54 = call i32 @dup(i32 %53)
store i32 %54, i32* %4
%55 = load i32, i32* %4
%56 = load i32, i32* @SEEK_SET
%57 = call i32 @lseek(i32 %55, i32 0, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%61 = load i32, i32* @errno
store i32 %61, i32* %1
br label %85
62: ; preds = %52
%63 = load i32, i32* %4
%64 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @write(i32 %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%69 = load i32, i32* @errno
store i32 %69, i32* %1
br label %85
70: ; preds = %62
%71 = load i32, i32* %2
%72 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%73 = call i32 @write(i32 %71, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %72)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%77 = load i32, i32* @errno
store i32 %77, i32* %1
br label %85
78: ; preds = %70
%79 = call i32 @wait(i32* null)
%80 = load i32, i32* %2
%81 = call i32 @close(i32 %80)
%82 = load i32, i32* %4
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* @EXIT_SUCCESS
store i32 %84, i32* %1
br label %85
85: ; preds = %12, %20, %30, %38, %46, %59, %67, %75, %78, %49
%86 = load i32, i32* %1
ret i32 %86
}

LLVM IR (-O2):
@.str = [8 x i8] c"./fich1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"abcde\00"
@.str.2 = [4 x i8] c"123\00"
@.str.3 = [3 x i8] c"45\00"
@SEEK_SET = global i32 0
@.str.4 = [3 x i8] c"fg\00"
@.str.5 = [3 x i8] c"hi\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5, i32 384)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @errno
br label %71
11: ; preds = %0
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @errno
br label %71
18: ; preds = %11
%19 = tail call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %18
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @errno
br label %71
28: ; preds = %21
%29 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @errno
br label %71
35: ; preds = %28
%36 = tail call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%41 = load i32, i32* @errno
br label %71
42: ; preds = %35
%43 = tail call i32 @close(i32 %23)
br label %71
44: ; preds = %18
%45 = tail call i32 @dup(i32 %6)
%46 = load i32, i32* @SEEK_SET
%47 = tail call i32 @lseek(i32 %45, i32 0, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%51 = load i32, i32* @errno
br label %71
52: ; preds = %44
%53 = tail call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%54 = tail call i32 @write(i32 %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%58 = load i32, i32* @errno
br label %71
59: ; preds = %52
%60 = tail call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%61 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %60)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%65 = load i32, i32* @errno
br label %71
66: ; preds = %59
%67 = tail call i32 @wait(i32* null)
%68 = tail call i32 @close(i32 %6)
%69 = tail call i32 @close(i32 %45)
%70 = load i32, i32* @EXIT_SUCCESS
br label %71
71: ; preds = %42, %66, %63, %56, %49, %39, %32, %25, %15, %8
%72 = phi i32 [ %10, %8 ], [ %17, %15 ], [ %27, %25 ], [ %34, %32 ], [ %41, %39 ], [ 0, %42 ], [ %51, %49 ], [ %58, %56 ], [ %65, %63 ], [ %70, %66 ]
ret i32 %72
}