#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int readnb_1(int /*<<< orphan*/ *, void *, int);
int readnb_2(int /*<<< orphan*/ *, void *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  char *filename = NULL;
  int bytes = 0, bytesread1 = 0, bytesread2 = 0;
  FILE *fp = NULL;
  void *buffn1 = NULL;
  void *buffn2 = NULL;
  char arr[] = "abcdefgh";
  if (argc > 2) {
    filename = argv[1];
    bytes = atoi(argv[2]);
    printf("Reading %d bytes from %s\n", bytes, filename);
  } else {
    printf("Arguments required: filename, bytes to read\n");
    return 0;
  }
  fp = fopen(filename, "r");
  buffn1 = (void *)malloc(bytes + 1);
  buffn2 = (void *)malloc(bytes + 1);
  if (!fp) {
    printf("Could not open file: %s\n", filename);
    return 1;
  }
  bytesread1 = readnb_1(fp, buffn1, bytes);
  rewind(fp);
  bytesread2 = readnb_2(fp, buffn2, bytes);
  fclose(fp);
  printf("%s\n", (char *)buffn1);
  printf("%s\n", (char *)buffn2);
  printf("Read %d, bytes from %s\n", bytesread1, filename);
  printf("Read %d, bytes from %s\n", bytesread2, filename);
  if (bytesread1 != bytesread2) {
    printf("Read wrong bytes: %d, %d\n", bytesread1, bytesread2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0xe2e(%rip),%rax
mov    %rax,-0x49(%rbp)
mov    0xe2c(%rip),%al
mov    %al,-0x41(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    121f <main+0x9f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1239 <main+0xb9>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1341 <main+0x1c1>
mov    -0x18(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1294 <main+0x114>
mov    -0x18(%rbp),%rsi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1341 <main+0x1c1>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <readnb_1@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10a0 <readnb_2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%rsi
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rsi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     133a <main+0x1ba>
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jl     1287 <main+0xf7>
mov    0x8(%rsi),%r12
mov    0x10(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r13d
lea    0xe45(%rip),%rdi
xor    %r15d,%r15d
mov    %eax,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe49(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0x1(%r13),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rbx,%rbx
je     1298 <main+0x108>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1070 <readnb_1@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
call   10b0 <readnb_2@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%r14
mov    %r14,%rdi
mov    0x4(%rsp),%ebx
mov    %ebx,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %r13d,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %r13d,%ebx
je     12af <main+0x11f>
lea    0xdd7(%rip),%rdi
xor    %r15d,%r15d
mov    %ebx,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12af <main+0x11f>
lea    0xdd9(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
jmp    12af <main+0x11f>
lea    0xd7d(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.arr = [9 x i8] c"abcdefgh\00"
@.str = [26 x i8] c"Reading %d bytes from %s\0A\00"
@.str.1 = [45 x i8] c"Arguments required: filename, bytes to read\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [25 x i8] c"Could not open file: %s\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [24 x i8] c"Read %d, bytes from %s\0A\00"
@.str.6 = [26 x i8] c"Read wrong bytes: %d, %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [9 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32* null, i32** %10
store i8* null, i8** %11
store i8* null, i8** %12
%14 = bitcast [9 x i8]* %13 to i8*
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 2
br i1 %16, label %17, label %28
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = load i8*, i8** %6
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %25, i8* %26)
br label %30
28: ; preds = %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %77
30: ; preds = %17
%31 = load i8*, i8** %6
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %10
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %11
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %12
%41 = load i32*, i32** %10
%42 = icmp ne i32* %41, null
br i1 %42, label %46, label %43
43: ; preds = %30
%44 = load i8*, i8** %6
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %44)
store i32 1, i32* %3
br label %77
46: ; preds = %30
%47 = load i32*, i32** %10
%48 = load i8*, i8** %11
%49 = load i32, i32* %7
%50 = call i32 @readnb_1(i32* %47, i8* %48, i32 %49)
store i32 %50, i32* %8
%51 = load i32*, i32** %10
%52 = call i32 @rewind(i32* %51)
%53 = load i32*, i32** %10
%54 = load i8*, i8** %12
%55 = load i32, i32* %7
%56 = call i32 @readnb_2(i32* %53, i8* %54, i32 %55)
store i32 %56, i32* %9
%57 = load i32*, i32** %10
%58 = call i32 @fclose(i32* %57)
%59 = load i8*, i8** %11
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = load i8*, i8** %12
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %8
%64 = load i8*, i8** %6
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %63, i8* %64)
%66 = load i32, i32* %9
%67 = load i8*, i8** %6
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %66, i8* %67)
%69 = load i32, i32* %8
%70 = load i32, i32* %9
%71 = icmp ne i32 %69, %70
br i1 %71, label %72, label %76
72: ; preds = %46
%73 = load i32, i32* %8
%74 = load i32, i32* %9
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %73, i32 %74)
br label %76
76: ; preds = %72, %46
store i32 0, i32* %3
br label %77
77: ; preds = %76, %43, %28
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [26 x i8] c"Reading %d bytes from %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [25 x i8] c"Could not open file: %s\0A\00"
@.str.5 = [24 x i8] c"Read %d, bytes from %s\0A\00"
@.str.6 = [26 x i8] c"Read wrong bytes: %d, %d\0A\00"
@str = [44 x i8] c"Arguments required: filename, bytes to read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 2
br i1 %3, label %4, label %18
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %6)
%11 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = add nsw i32 %9, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i64 @malloc(i32 %12)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i32* %11, null
br i1 %17, label %20, label %22
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %34
20: ; preds = %4
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %34
22: ; preds = %4
%23 = tail call i32 @readnb_1(i32* %11, i8* %14, i32 %9)
%24 = tail call i32 @rewind(i32* %11)
%25 = tail call i32 @readnb_2(i32* %11, i8* %16, i32 %9)
%26 = tail call i32 @fclose(i32* %11)
%27 = tail call i32 @puts(i8* dereferenceable(1) %14)
%28 = tail call i32 @puts(i8* dereferenceable(1) %16)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %23, i8* %6)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %25, i8* %6)
%31 = icmp eq i32 %23, %25
br i1 %31, label %34, label %32
32: ; preds = %22
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %23, i32 %25)
br label %34
34: ; preds = %22, %32, %20, %18
%35 = phi i32 [ 1, %20 ], [ 0, %18 ], [ 0, %32 ], [ 0, %22 ]
ret i32 %35
}