#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 */
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
unsigned char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/,
                    int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *tmpfile();
int main(int argc, char **argv) {
  FILE *f = tmpfile();
  unsigned char *data;
  assert(f);
  printf("Saving some chars with fputc.\n");
  for (int i = 0; i < 10; i++) {
    fputc(i, f);
  }
  fflush(f);
  printf("MMapping the tmp file\n");
  data = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(f), 0);
  assert(data && data != MAP_FAILED);
  printf("Updating mmaped region\n");
  data[0] = 100;
  data[5] = 105;
  printf("Saving some chars with fputc.\n");
  for (int i = 10; i < 100; i++) {
    fputc(i, f);
  }
  printf("Updating mmaped region\n");
  data[7] = 107;
  printf("MUnmapping the file\n");
  munmap(data, 10);
  printf("Checking the file\n");
  rewind(f);
  for (int i = 0; i < 100; i++) {
    int ch = fgetc(f);
    if (i == 0)
      assert(ch == 100);
    else if (i == 5)
      assert(ch == 105);
    else if (i == 7)
      assert(ch == 107);
    else
      assert(ch == i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <tmpfile@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1090 <assert@plt>
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    1203 <main+0x63>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11df <main+0x3f>
mov    -0x18(%rbp),%rdi
call   10b0 <fflush@plt>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    0x2da8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <fileno@plt>
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0xa,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x31(%rbp)
je     1284 <main+0xe4>
mov    -0x20(%rbp),%rax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
lea    0xd9e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x64,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x69,0x5(%rax)
lea    0xd4d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xa,-0x28(%rbp)
cmpl   $0x64,-0x28(%rbp)
jge    12e3 <main+0x143>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12bf <main+0x11f>
lea    0xd4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x6b,0x7(%rax)
lea    0xd50(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   10c0 <munmap@plt>
lea    0xd4b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jge    13d3 <main+0x233>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1363 <main+0x1c3>
cmpl   $0x64,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
jmp    13c0 <main+0x220>
cmpl   $0x5,-0x2c(%rbp)
jne    1383 <main+0x1e3>
cmpl   $0x69,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
jmp    13bb <main+0x21b>
cmpl   $0x7,-0x2c(%rbp)
jne    13a3 <main+0x203>
cmpl   $0x6b,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
jmp    13b6 <main+0x216>
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
jmp    13bb <main+0x21b>
jmp    13c0 <main+0x220>
jmp    13c5 <main+0x225>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    132d <main+0x18d>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
call   1080 <tmpfile@plt>
mov    %rax,%rbx
mov    %ebx,%edi
call   1090 <assert@plt>
lea    0xe56(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x3,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x4,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x5,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x6,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x7,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x8,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    $0x9,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    %rbx,%rdi
call   10b0 <fflush@plt>
lea    0xdaa(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d96(%rip),%rax
mov    0x2d67(%rip),%rcx
mov    (%rcx),%r15d
or     (%rax),%r15d
mov    0x2d72(%rip),%rax
mov    (%rax),%r12d
mov    %rbx,%rdi
call   10a0 <fileno@plt>
mov    $0xa,%ebp
xor    %edi,%edi
mov    $0xa,%esi
mov    %r15d,%edx
mov    %r12d,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r15
test   %rax,%rax
setne  %al
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%r15
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1090 <assert@plt>
lea    0xd6f(%rip),%rdi
call   1030 <puts@plt>
movb   $0x64,(%r15)
movb   $0x69,0x5(%r15)
mov    %r14,%rdi
call   1030 <puts@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
add    $0x1,%ebp
cmp    $0x64,%ebp
jne    12e0 <main+0x140>
lea    0xd3b(%rip),%rdi
call   1030 <puts@plt>
movb   $0x6b,0x7(%r15)
lea    0xd41(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    $0xa,%esi
call   10c0 <munmap@plt>
lea    0xd3c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <rewind@plt>
xor    %ebp,%ebp
jmp    135a <main+0x1ba>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x6b,%ecx
xor    %edi,%edi
cmp    %ecx,%eax
sete   %dil
call   1090 <assert@plt>
add    $0x1,%ebp
cmp    $0x64,%ebp
je     1394 <main+0x1f4>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
test   %ebp,%ebp
je     1380 <main+0x1e0>
cmp    $0x7,%ebp
je     1340 <main+0x1a0>
cmp    $0x5,%ebp
jne    1390 <main+0x1f0>
mov    $0x69,%ecx
jmp    1345 <main+0x1a5>
nopw   0x0(%rax,%rax,1)
mov    $0x64,%ecx
jmp    1345 <main+0x1a5>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ecx
jmp    1345 <main+0x1a5>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Saving some chars with fputc.\0A\00"
@.str.1 = [23 x i8] c"MMapping the tmp file\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"Updating mmaped region\0A\00"
@.str.3 = [21 x i8] c"MUnmapping the file\0A\00"
@.str.4 = [19 x i8] c"Checking the file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32* (...) @tmpfile()
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = ptrtoint i32* %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %8
br label %17
17: ; preds = %24, %2
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 10
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* %8
%22 = load i32*, i32** %6
%23 = call i32 @fputc(i32 %21, i32* %22)
br label %24
24: ; preds = %20
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %17
27: ; preds = %17
%28 = load i32*, i32** %6
%29 = call i32 @fflush(i32* %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %31, %32
%34 = load i32, i32* @MAP_SHARED
%35 = load i32*, i32** %6
%36 = call i32 @fileno(i32* %35)
%37 = call i8* @mmap(i32* null, i32 10, i32 %33, i32 %34, i32 %36, i32 0)
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %44
40: ; preds = %27
%41 = load i8*, i8** %7
%42 = load i8*, i8** @MAP_FAILED
%43 = icmp ne i8* %41, %42
br label %44
44: ; preds = %40, %27
%45 = phi i1 [ false, %27 ], [ %43, %40 ]
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 0
store i8 100, i8* %50
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i64 5
store i8 105, i8* %52
%53 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 10, i32* %9
br label %54
54: ; preds = %61, %44
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 100
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = load i32, i32* %9
%59 = load i32*, i32** %6
%60 = call i32 @fputc(i32 %58, i32* %59)
br label %61
61: ; preds = %57
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %54
64: ; preds = %54
%65 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 7
store i8 107, i8* %67
%68 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%69 = load i8*, i8** %7
%70 = call i32 @munmap(i8* %69, i32 10)
%71 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%72 = load i32*, i32** %6
%73 = call i32 @rewind(i32* %72)
store i32 0, i32* %10
br label %74
74: ; preds = %112, %64
%75 = load i32, i32* %10
%76 = icmp slt i32 %75, 100
br i1 %76, label %77, label %115
77: ; preds = %74
%78 = load i32*, i32** %6
%79 = call i32 @fgetc(i32* %78)
store i32 %79, i32* %11
%80 = load i32, i32* %10
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %87
82: ; preds = %77
%83 = load i32, i32* %11
%84 = icmp eq i32 %83, 100
%85 = zext i1 %84 to i32
%86 = call i32 @assert(i32 %85)
br label %111
87: ; preds = %77
%88 = load i32, i32* %10
%89 = icmp eq i32 %88, 5
br i1 %89, label %90, label %95
90: ; preds = %87
%91 = load i32, i32* %11
%92 = icmp eq i32 %91, 105
%93 = zext i1 %92 to i32
%94 = call i32 @assert(i32 %93)
br label %110
95: ; preds = %87
%96 = load i32, i32* %10
%97 = icmp eq i32 %96, 7
br i1 %97, label %98, label %103
98: ; preds = %95
%99 = load i32, i32* %11
%100 = icmp eq i32 %99, 107
%101 = zext i1 %100 to i32
%102 = call i32 @assert(i32 %101)
br label %109
103: ; preds = %95
%104 = load i32, i32* %11
%105 = load i32, i32* %10
%106 = icmp eq i32 %104, %105
%107 = zext i1 %106 to i32
%108 = call i32 @assert(i32 %107)
br label %109
109: ; preds = %103, %98
br label %110
110: ; preds = %109, %90
br label %111
111: ; preds = %110, %82
br label %112
112: ; preds = %111
%113 = load i32, i32* %10
%114 = add nsw i32 %113, 1
store i32 %114, i32* %10
br label %74
115: ; preds = %74
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@str.5 = [22 x i8] c"MMapping the tmp file\00"
@str.7 = [30 x i8] c"Saving some chars with fputc.\00"
@str.8 = [23 x i8] c"Updating mmaped region\00"
@str.9 = [20 x i8] c"MUnmapping the file\00"
@str.10 = [18 x i8] c"Checking the file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* (...) @tmpfile()
%4 = ptrtoint i32* %3 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
%8 = tail call i32 @fputc(i32 0, i32* %3)
%9 = tail call i32 @fputc(i32 1, i32* %3)
%10 = tail call i32 @fputc(i32 2, i32* %3)
%11 = tail call i32 @fputc(i32 3, i32* %3)
%12 = tail call i32 @fputc(i32 4, i32* %3)
%13 = tail call i32 @fputc(i32 5, i32* %3)
%14 = tail call i32 @fputc(i32 6, i32* %3)
%15 = tail call i32 @fputc(i32 7, i32* %3)
%16 = tail call i32 @fputc(i32 8, i32* %3)
%17 = tail call i32 @fputc(i32 9, i32* %3)
%18 = tail call i32 @fflush(i32* %3)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.5, i64 0, i64 0))
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i32 @fileno(i32* %3)
%25 = tail call i8* @mmap(i32* null, i32 10, i32 %22, i32 %23, i32 %24, i32 0)
%26 = icmp ne i8* %25, null
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp ne i8* %25, %27
%29 = select i1 %26, i1 %28, i1 false
%30 = zext i1 %29 to i32
%31 = tail call i32 @assert(i32 %30)
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
store i8 100, i8* %25
%33 = getelementptr inbounds i8, i8* %25, i64 5
store i8 105, i8* %33
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
br label %42
35: ; preds = %42
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%37 = getelementptr inbounds i8, i8* %25, i64 7
store i8 107, i8* %37
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%39 = tail call i32 @munmap(i8* %25, i32 10)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0))
%41 = tail call i32 @rewind(i32* %3)
br label %48
42: ; preds = %2, %42
%43 = phi i32 [ 10, %2 ], [ %45, %42 ]
%44 = tail call i32 @fputc(i32 %43, i32* %3)
%45 = add nuw nsw i32 %43, 1
%46 = icmp eq i32 %45, 100
br i1 %46, label %35, label %42
47: ; preds = %54
ret i32 0
48: ; preds = %35, %54
%49 = phi i32 [ 0, %35 ], [ %59, %54 ]
%50 = tail call i32 @fgetc(i32* %3)
switch i32 %49, label %53 [
i32 0, label %54
i32 5, label %51
i32 7, label %52
]
51: ; preds = %48
br label %54
52: ; preds = %48
br label %54
53: ; preds = %48
br label %54
54: ; preds = %48, %51, %53, %52
%55 = phi i32 [ 105, %51 ], [ %49, %53 ], [ 107, %52 ], [ 100, %48 ]
%56 = icmp eq i32 %50, %55
%57 = zext i1 %56 to i32
%58 = tail call i32 @assert(i32 %57)
%59 = add nuw nsw i32 %49, 1
%60 = icmp eq i32 %59, 100
br i1 %60, label %47, label %48
}