#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 */
char EOF;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int close(int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
char getc(int /*<<< orphan*/ *);
int open(char *, int, ...);
int putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, size_t);
int strlen(char *);
int write(int, char *, size_t);
int main(int argc, char **argv) {
  int fd;
  int err, i;
  size_t len;
  char buffer[100];
  FILE *fp;
  char r = '\0';
  char *fname = "/home/kunal/Documents/secure-4995/cbfi/python-wrapper/"
                "playground/sample.txt";
  char *content = "Hello World";
  len = strlen(content) * sizeof(char);
  fd = open(fname, O_WRONLY | O_CREAT, 0640);
  err = write(fd, content, len);
  assert(err != -1);
  err = close(fd);
  fd = open(fname, O_RDONLY);
  assert(fd > -1);
  err = read(fd, buffer, len);
  assert(err == len);
  close(fd);
  for (i = 0; i < strlen(content); ++i)
    puts(content);
  fp = fopen("sample.txt", "a+");
  fseek(fp, SEEK_SET, 0);
  for (i = 0; i < 5; ++i) {
    r = getc(fp);
    assert(r != EOF);
    err = putc('a', fp);
    assert(err != EOF);
  }
  fclose(fp);
  return errno;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x99(%rbp)
lea    0xe19(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    0xe57(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    -0xb0(%rbp),%rdi
call   1060 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %rax,-0x28(%rbp)
mov    -0xa8(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x18(%rbp)
mov    -0xa8(%rbp),%rdi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0xb4(%rbp)
mov    -0xb0(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0xb4(%rbp),%eax
cmp    %ecx,%eax
jge    1310 <main+0x150>
mov    -0xb0(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12d1 <main+0x111>
lea    0xd2a(%rip),%rdi
lea    0xd3a(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
mov    0x2c88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <fseek@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    13c2 <main+0x202>
mov    -0x98(%rbp),%rdi
call   10e0 <getc@plt>
mov    %al,-0x99(%rbp)
movsbl -0x99(%rbp),%eax
mov    0x2c66(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x98(%rbp),%rsi
mov    $0x61,%edi
call   1090 <putc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2c36(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1348 <main+0x188>
mov    -0x98(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2bf3(%rip),%rax
mov    (%rax),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
lea    0xe79(%rip),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
movslq %eax,%r12
mov    0x2dcb(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe09(%rip),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
mov    $0x1a0,%edx
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <write@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   10a0 <assert@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   10a0 <assert@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %r12,%rdx
call   1080 <read@plt>
xor    %edi,%edi
cmp    %r12d,%eax
sete   %dil
call   10a0 <assert@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    12a7 <main+0xe7>
lea    0xdca(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <puts@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    %eax,%ebp
jl     1290 <main+0xd0>
lea    0xd93(%rip),%rdi
lea    0xda3(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   10e0 <getc@plt>
mov    0x2cf9(%rip),%rbp
xor    %edi,%edi
cmp    0x0(%rbp),%al
setne  %dil
call   10a0 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1090 <putc@plt>
movsbl 0x0(%rbp),%ecx
xor    %edi,%edi
cmp    %ecx,%eax
setne  %dil
call   10a0 <assert@plt>
mov    %rbx,%rdi
call   10e0 <getc@plt>
xor    %edi,%edi
cmp    0x0(%rbp),%al
setne  %dil
call   10a0 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1090 <putc@plt>
movsbl 0x0(%rbp),%ecx
xor    %edi,%edi
cmp    %ecx,%eax
setne  %dil
call   10a0 <assert@plt>
mov    %rbx,%rdi
call   10e0 <getc@plt>
xor    %edi,%edi
cmp    0x0(%rbp),%al
setne  %dil
call   10a0 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1090 <putc@plt>
movsbl 0x0(%rbp),%ecx
xor    %edi,%edi
cmp    %ecx,%eax
setne  %dil
call   10a0 <assert@plt>
mov    %rbx,%rdi
call   10e0 <getc@plt>
xor    %edi,%edi
cmp    0x0(%rbp),%al
setne  %dil
call   10a0 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1090 <putc@plt>
movsbl 0x0(%rbp),%ecx
xor    %edi,%edi
cmp    %ecx,%eax
setne  %dil
call   10a0 <assert@plt>
mov    %rbx,%rdi
call   10e0 <getc@plt>
xor    %edi,%edi
cmp    0x0(%rbp),%al
setne  %dil
call   10a0 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1090 <putc@plt>
movsbl 0x0(%rbp),%ecx
xor    %edi,%edi
cmp    %ecx,%eax
setne  %dil
call   10a0 <assert@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2bde(%rip),%rax
mov    (%rax),%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [76 x i8] c"/home/kunal/Documents/secure-4995/cbfi/python-wrapper/playground/sample.txt\00"
@.str.1 = [12 x i8] c"Hello World\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"sample.txt\00"
@.str.3 = [3 x i8] c"a+\00"
@SEEK_SET = global i32 0
@EOF = global i8 0
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca [100 x i8]
%11 = alloca i32*
%12 = alloca i8
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 0, i8* %12
store i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i8** %13
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %14
%15 = load i8*, i8** %14
%16 = call i32 @strlen(i8* %15)
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
store i64 %18, i64* %9
%19 = load i8*, i8** %13
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %22, i32 416)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = load i8*, i8** %14
%26 = load i64, i64* %9
%27 = call i32 @write(i32 %24, i8* %25, i64 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, -1
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 %33, i32* %7
%34 = load i8*, i8** %13
%35 = load i32, i32* @O_RDONLY
%36 = call i32 (i8*, i32, ...) @open(i8* %34, i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp sgt i32 %37, -1
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* %6
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%43 = load i64, i64* %9
%44 = call i32 @read(i32 %41, i8* %42, i64 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = load i64, i64* %9
%48 = icmp eq i64 %46, %47
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
store i32 0, i32* %8
br label %53
53: ; preds = %61, %2
%54 = load i32, i32* %8
%55 = load i8*, i8** %14
%56 = call i32 @strlen(i8* %55)
%57 = icmp slt i32 %54, %56
br i1 %57, label %58, label %64
58: ; preds = %53
%59 = load i8*, i8** %14
%60 = call i32 @puts(i8* %59)
br label %61
61: ; preds = %58
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8
br label %53
64: ; preds = %53
%65 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %65, i32** %11
%66 = load i32*, i32** %11
%67 = load i32, i32* @SEEK_SET
%68 = call i32 @fseek(i32* %66, i32 %67, i32 0)
store i32 0, i32* %8
br label %69
69: ; preds = %90, %64
%70 = load i32, i32* %8
%71 = icmp slt i32 %70, 5
br i1 %71, label %72, label %93
72: ; preds = %69
%73 = load i32*, i32** %11
%74 = call signext i8 @getc(i32* %73)
store i8 %74, i8* %12
%75 = load i8, i8* %12
%76 = sext i8 %75 to i32
%77 = load i8, i8* @EOF
%78 = sext i8 %77 to i32
%79 = icmp ne i32 %76, %78
%80 = zext i1 %79 to i32
%81 = call i32 @assert(i32 %80)
%82 = load i32*, i32** %11
%83 = call i32 @putc(i8 signext 97, i32* %82)
store i32 %83, i32* %7
%84 = load i32, i32* %7
%85 = load i8, i8* @EOF
%86 = sext i8 %85 to i32
%87 = icmp ne i32 %84, %86
%88 = zext i1 %87 to i32
%89 = call i32 @assert(i32 %88)
br label %90
90: ; preds = %72
%91 = load i32, i32* %8
%92 = add nsw i32 %91, 1
store i32 %92, i32* %8
br label %69
93: ; preds = %69
%94 = load i32*, i32** %11
%95 = call i32 @fclose(i32* %94)
%96 = load i32, i32* @errno
ret i32 %96
}

LLVM IR (-O2):
@.str = [76 x i8] c"/home/kunal/Documents/secure-4995/cbfi/python-wrapper/playground/sample.txt\00"
@.str.1 = [12 x i8] c"Hello World\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"sample.txt\00"
@.str.3 = [3 x i8] c"a+\00"
@SEEK_SET = global i32 0
@EOF = global i8 0
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%6 = sext i32 %5 to i64
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i32 %9, i32 416)
%11 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %6)
%12 = icmp ne i32 %11, -1
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i32 @close(i32 %10)
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = xor i32 %17, -1
%19 = lshr i32 %18, 31
%20 = tail call i32 @assert(i32 %19)
%21 = call i32 @read(i32 %17, i8* %4, i64 %6)
%22 = icmp eq i32 %21, %5
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
%25 = tail call i32 @close(i32 %17)
%26 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %2, %28
%29 = phi i32 [ %31, %28 ], [ 0, %2 ]
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%31 = add nuw nsw i32 %29, 1
%32 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp slt i32 %31, %32
br i1 %33, label %28, label %34
34: ; preds = %28, %2
%35 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @SEEK_SET
%37 = tail call i32 @fseek(i32* %35, i32 %36, i32 0)
%38 = tail call signext i8 @getc(i32* %35)
%39 = load i8, i8* @EOF
%40 = icmp ne i8 %38, %39
%41 = zext i1 %40 to i32
%42 = tail call i32 @assert(i32 %41)
%43 = tail call i32 @putc(i8 signext 97, i32* %35)
%44 = load i8, i8* @EOF
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %43, %45
%47 = zext i1 %46 to i32
%48 = tail call i32 @assert(i32 %47)
%49 = tail call signext i8 @getc(i32* %35)
%50 = load i8, i8* @EOF
%51 = icmp ne i8 %49, %50
%52 = zext i1 %51 to i32
%53 = tail call i32 @assert(i32 %52)
%54 = tail call i32 @putc(i8 signext 97, i32* %35)
%55 = load i8, i8* @EOF
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %54, %56
%58 = zext i1 %57 to i32
%59 = tail call i32 @assert(i32 %58)
%60 = tail call signext i8 @getc(i32* %35)
%61 = load i8, i8* @EOF
%62 = icmp ne i8 %60, %61
%63 = zext i1 %62 to i32
%64 = tail call i32 @assert(i32 %63)
%65 = tail call i32 @putc(i8 signext 97, i32* %35)
%66 = load i8, i8* @EOF
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %65, %67
%69 = zext i1 %68 to i32
%70 = tail call i32 @assert(i32 %69)
%71 = tail call signext i8 @getc(i32* %35)
%72 = load i8, i8* @EOF
%73 = icmp ne i8 %71, %72
%74 = zext i1 %73 to i32
%75 = tail call i32 @assert(i32 %74)
%76 = tail call i32 @putc(i8 signext 97, i32* %35)
%77 = load i8, i8* @EOF
%78 = sext i8 %77 to i32
%79 = icmp ne i32 %76, %78
%80 = zext i1 %79 to i32
%81 = tail call i32 @assert(i32 %80)
%82 = tail call signext i8 @getc(i32* %35)
%83 = load i8, i8* @EOF
%84 = icmp ne i8 %82, %83
%85 = zext i1 %84 to i32
%86 = tail call i32 @assert(i32 %85)
%87 = tail call i32 @putc(i8 signext 97, i32* %35)
%88 = load i8, i8* @EOF
%89 = sext i8 %88 to i32
%90 = icmp ne i32 %87, %89
%91 = zext i1 %90 to i32
%92 = tail call i32 @assert(i32 %91)
%93 = tail call i32 @fclose(i32* %35)
%94 = load i32, i32* @errno
ret i32 %94
}