#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ ungetc(char, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  char buffer[256];
  FILE *file = fopen("test1", "rb");
  printf("Current position is %lu\n", ftell(file));
  ungetc('+', file);
  printf("Current position if position is 0 when ungetc is called is %lu\n",
         ftell(file));
  fgets(buffer, 255, file);
  fputs(buffer, stdout);
  fclose(file);
  file = fopen("test1", "wb");
  fputs(argv[1], file);
  fseek(file, ftell(file), SEEK_SET);
  printf("\nCurrent position is %lu\n", ftell(file));
  fputs("crap\n", file);
  fclose(file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe66(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,%esi
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x118(%rbp),%rsi
mov    $0x2b,%edi
call   1080 <ungetc@plt>
mov    -0x118(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,%esi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0xff,%esi
call   1060 <fgets@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%rsi
call   1050 <fputs@plt>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdce(%rip),%rdi
lea    0xe10(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x118(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x118(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x118(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x118(%rbp),%rdi
call   1070 <ftell@plt>
mov    -0x120(%rbp),%rdi
mov    %eax,%esi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x118(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,%esi
lea    0xdad(%rip),%rdi
call   1040 <printf@plt>
mov    -0x118(%rbp),%rsi
lea    0xdb4(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r15
lea    0xe58(%rip),%r14
lea    0xe57(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <ftell@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x2b,%edi
mov    %rbx,%rsi
call   1080 <ungetc@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xff,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rsi
mov    %r12,%rdi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe22(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%r15),%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    0x2d93(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xdf0(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"test1\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"Current position is %lu\0A\00"
@.str.3 = [64 x i8] c"Current position if position is 0 when ungetc is called is %lu\0A\00"
@stdout = global i32* null
@.str.4 = [3 x i8] c"wb\00"
@SEEK_SET = global i32 0
@.str.5 = [26 x i8] c"\0ACurrent position is %lu\0A\00"
@.str.6 = [6 x i8] c"crap\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [256 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = call i32 @ftell(i32* %8)
%10 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = load i32*, i32** %6
%12 = call i32 @ungetc(i8 signext 43, i32* %11)
%13 = load i32*, i32** %6
%14 = call i32 @ftell(i32* %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i32 @fgets(i8* %16, i32 255, i32* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** @stdout
%21 = call i32 @fputs(i8* %19, i32* %20)
%22 = load i32*, i32** %6
%23 = call i32 @fclose(i32* %22)
%24 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i8**, i8*** %4
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32*, i32** %6
%29 = call i32 @fputs(i8* %27, i32* %28)
%30 = load i32*, i32** %6
%31 = load i32*, i32** %6
%32 = call i32 @ftell(i32* %31)
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %30, i32 %32, i32 %33)
%35 = load i32*, i32** %6
%36 = call i32 @ftell(i32* %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = load i32*, i32** %6
%39 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32* %38)
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"test1\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"Current position is %lu\0A\00"
@.str.3 = [64 x i8] c"Current position if position is 0 when ungetc is called is %lu\0A\00"
@stdout = global i32* null
@.str.4 = [3 x i8] c"wb\00"
@SEEK_SET = global i32 0
@.str.5 = [26 x i8] c"\0ACurrent position is %lu\0A\00"
@.str.6 = [6 x i8] c"crap\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @ftell(i32* %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = tail call i32 @ungetc(i8 signext 43, i32* %5)
%9 = tail call i32 @ftell(i32* %5)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = call i32 @fgets(i8* %4, i32 255, i32* %5)
%12 = load i32*, i32** @stdout
%13 = call i32 @fputs(i8* %4, i32* %12)
%14 = call i32 @fclose(i32* %5)
%15 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @fputs(i8* %17, i32* %15)
%19 = call i32 @ftell(i32* %15)
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %15, i32 %19, i32 %20)
%22 = call i32 @ftell(i32* %15)
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i64 5, i64 1, i32* %15)
%25 = call i32 @fclose(i32* %15)
ret i32 0
}