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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
int main() {
  FILE *fp;
  fpos_t pos;
  int c;
  char buffer[256];
  fp = fopen("file.txt", "r");
  if (fp == NULL) {
    perror("Error in opening file");
    return (-1);
  }
  while (!feof(fp)) {
    c = getc(fp);
    printf("\nNext character is %c\n", c);
    if (c == '!') {
      ungetc('t', fp);
      if (fgetpos(fp, &pos) == -1) {
        printf("The current file position could not be got\n");
        exit(0);
      }
      fgets(buffer, 5, fp);
      fputs(buffer, stdout);
      fputs("\n", stdout);
      if (fsetpos(fp, &pos) == -1) {
        printf("The current file position could not be set\n");
        exit(0);
      }
      ungetc('+', fp);
      fgets(buffer, 9, fp);
      fputs(buffer, stdout);
      fputs("\n", stdout);
      fputs("Iteration done\n", stdout);
    } else {
      ungetc(c, fp);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fc <main+0x4c>
lea    0xe20(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <main+0x1b8>
jmp    1201 <main+0x51>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121f <main+0x6f>
jmp    1361 <main+0x1b1>
mov    -0x10(%rbp),%rdi
call   10d0 <getc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x21,-0x18(%rbp)
jne    1350 <main+0x1a0>
mov    -0x10(%rbp),%rsi
mov    $0x74,%edi
call   1080 <ungetc@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1040 <fgetpos@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xcf>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x5,%esi
call   1060 <fgets@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
lea    0xd7d(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   10b0 <fsetpos@plt>
cmp    $0xffffffff,%eax
jne    12e9 <main+0x139>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    $0x2b,%edi
call   1080 <ungetc@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x9,%esi
call   1060 <fgets@plt>
lea    -0x120(%rbp),%rdi
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
lea    0xd05(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
lea    0xd4a(%rip),%rdi
call   1050 <fputs@plt>
jmp    135c <main+0x1ac>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <ungetc@plt>
jmp    1201 <main+0x51>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe28(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     130b <main+0x14b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    12f9 <main+0x139>
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2dcb(%rip),%r14
lea    0xe1a(%rip),%r13
jmp    123a <main+0x7a>
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <ungetc@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    12f7 <main+0x137>
mov    %rbx,%rdi
call   10e0 <getc@plt>
mov    %eax,%ebp
lea    0xdd6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x21,%ebp
jne    1220 <main+0x60>
mov    $0x74,%edi
mov    %rbx,%rsi
call   1090 <ungetc@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <fgetpos@plt>
cmp    $0xffffffff,%eax
je     131e <main+0x15e>
mov    %r12,%rdi
mov    $0x5,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    (%r14),%esi
mov    %r12,%rdi
call   1060 <fputs@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   1060 <fputs@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <fsetpos@plt>
cmp    $0xffffffff,%eax
je     1327 <main+0x167>
mov    $0x2b,%edi
mov    %rbx,%rsi
call   1090 <ungetc@plt>
mov    %r12,%rdi
mov    $0x9,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    (%r14),%esi
mov    %r12,%rdi
call   1060 <fputs@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   1060 <fputs@plt>
mov    (%r14),%esi
lea    0xd4b(%rip),%rdi
call   1060 <fputs@plt>
jmp    122a <main+0x6a>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf9(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12f9 <main+0x139>
lea    0xd4e(%rip),%rdi
jmp    132e <main+0x16e>
lea    0xd1a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Error in opening file\00"
@.str.3 = [23 x i8] c"\0ANext character is %c\0A\00"
@.str.4 = [44 x i8] c"The current file position could not be got\0A\00"
@stdout = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [44 x i8] c"The current file position could not be set\0A\00"
@.str.7 = [16 x i8] c"Iteration done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %67
11: ; preds = %0
br label %12
12: ; preds = %65, %11
%13 = load i32*, i32** %2
%14 = call i32 @feof(i32* %13)
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %66
17: ; preds = %12
%18 = load i32*, i32** %2
%19 = call i32 @getc(i32* %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 33
br i1 %23, label %24, label %61
24: ; preds = %17
%25 = load i32*, i32** %2
%26 = call i32 @ungetc(i32 116, i32* %25)
%27 = load i32*, i32** %2
%28 = call i32 @fgetpos(i32* %27, i32* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %24
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = load i32*, i32** %2
%36 = call i32 @fgets(i8* %34, i32 5, i32* %35)
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%38 = load i32, i32* @stdout
%39 = call i32 @fputs(i8* %37, i32 %38)
%40 = load i32, i32* @stdout
%41 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = load i32*, i32** %2
%43 = call i32 @fsetpos(i32* %42, i32* %3)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %33
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %33
%49 = load i32*, i32** %2
%50 = call i32 @ungetc(i32 43, i32* %49)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%52 = load i32*, i32** %2
%53 = call i32 @fgets(i8* %51, i32 9, i32* %52)
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%55 = load i32, i32* @stdout
%56 = call i32 @fputs(i8* %54, i32 %55)
%57 = load i32, i32* @stdout
%58 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = load i32, i32* @stdout
%60 = call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %59)
br label %65
61: ; preds = %17
%62 = load i32, i32* %4
%63 = load i32*, i32** %2
%64 = call i32 @ungetc(i32 %62, i32* %63)
br label %65
65: ; preds = %61, %48
br label %12
66: ; preds = %12
store i32 0, i32* %1
br label %67
67: ; preds = %66, %9
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Error in opening file\00"
@.str.3 = [23 x i8] c"\0ANext character is %c\0A\00"
@stdout = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
@.str.7 = [16 x i8] c"Iteration done\0A\00"
@str = [43 x i8] c"The current file position could not be set\00"
@str.8 = [43 x i8] c"The current file position could not be got\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %48
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %48
12: ; preds = %7, %45
%13 = call i32 @getc(i32* %5)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %13, 33
br i1 %15, label %16, label %43
16: ; preds = %12
%17 = call i32 @ungetc(i32 116, i32* %5)
%18 = call i32 @fgetpos(i32* %5, i32* %1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.8, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %16
%24 = call i32 @fgets(i8* %4, i32 5, i32* %5)
%25 = load i32, i32* @stdout
%26 = call i32 @fputs(i8* %4, i32 %25)
%27 = load i32, i32* @stdout
%28 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = call i32 @fsetpos(i32* %5, i32* %1)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %23
%35 = call i32 @ungetc(i32 43, i32* %5)
%36 = call i32 @fgets(i8* %4, i32 9, i32* %5)
%37 = load i32, i32* @stdout
%38 = call i32 @fputs(i8* %4, i32 %37)
%39 = load i32, i32* @stdout
%40 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = load i32, i32* @stdout
%42 = call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %41)
br label %45
43: ; preds = %12
%44 = call i32 @ungetc(i32 %13, i32* %5)
br label %45
45: ; preds = %43, %34
%46 = call i32 @feof(i32* %5)
%47 = icmp eq i32 %46, 0
br i1 %47, label %12, label %48
48: ; preds = %45, %7, %10
%49 = phi i32 [ -1, %10 ], [ 0, %7 ], [ 0, %45 ]
ret i32 %49
}