#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*/ j;

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, int *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void checkn(const char *d, const char *prefix, int nf) {
  int fd, i, cc, j;
  char n[512];
  for (i = 0; i < nf; i++) {
    sprintf(n, "%s/%s-%d", d, prefix, i);
    fd = open(n, 0);
    if (fd < 0) {
      fprintf(stderr, "test-lab-3-b: open(%s): %s\n", n, strerror(errno));
      exit(1);
    }
    j = -1;
    cc = read(fd, &j, sizeof(j));
    if (cc != sizeof(j)) {
      fprintf(stderr, "test-lab-3-b: read(%s) returned too little %d%s%s\n", n,
              cc, errno ? ": " : "", errno ? strerror(errno) : "");
      exit(1);
    }
    if (j != i) {
      fprintf(stderr, "test-lab-3-b: checkn %s contained %d not %d\n", n, j, i);
      exit(1);
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<checkn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1366 <checkn+0x1f6>
lea    -0x230(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xe4d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1227 <checkn+0xb7>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x23c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x23c(%rbp),%edi
mov    -0x238(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    $0x4,%rax
je     1315 <checkn+0x1a5>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x25c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x258(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24c(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%rdx
lea    0xe00(%rip),%rax
lea    0xdca(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x248(%rbp)
mov    0x2d34(%rip),%rax
cmpq   $0x0,(%rax)
je     12c9 <checkn+0x159>
mov    0x2d23(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    %rax,-0x268(%rbp)
jmp    12dc <checkn+0x16c>
lea    0xdb7(%rip),%rax
mov    %rax,-0x268(%rbp)
jmp    12dc <checkn+0x16c>
mov    -0x248(%rbp),%r8
mov    -0x24c(%rbp),%ecx
mov    -0x258(%rbp),%rdx
mov    -0x25c(%rbp),%edi
mov    -0x268(%rbp),%r9
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1350 <checkn+0x1e0>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    -0x230(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xd1c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118d <checkn+0x1d>
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,0x8(%rsp)
test   %edx,%edx
jle    1205 <checkn+0x95>
mov    %edx,%r13d
mov    %rdi,%r15
xor    %ebx,%ebx
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xe56(%rip),%rsi
mov    %r15,%rdx
mov    0x8(%rsp),%rcx
mov    %ebx,%r8d
call   1070 <sprintf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     1217 <checkn+0xa7>
mov    %eax,%r14d
movl   $0xffffffff,0x4(%rsp)
ff
mov    %eax,%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    1251 <checkn+0xe1>
mov    0x4(%rsp),%ecx
cmp    %ecx,%ebx
jne    1278 <checkn+0x108>
mov    %r14d,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    11a0 <checkn+0x30>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebx
mov    0x2db1(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xdd3(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r12d
mov    0x2d85(%rip),%rax
mov    (%rax),%ebx
mov    0x2d74(%rip),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
jne    12a1 <checkn+0x131>
lea    0xe14(%rip),%r8
mov    %r8,%r9
jmp    12b0 <checkn+0x140>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
call   1090 <strerror@plt>
mov    %rax,%r9
lea    0xda8(%rip),%r8
lea    0xd6e(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"%s/%s-%d\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"test-lab-3-b: open(%s): %s\0A\00"
@errno = global i64 0
@.str.2 = [51 x i8] c"test-lab-3-b: read(%s) returned too little %d%s%s\0A\00"
@.str.3 = [3 x i8] c": \00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [45 x i8] c"test-lab-3-b: checkn %s contained %d not %d\0A\00"
define void @checkn(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [512 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %12
12: ; preds = %71, %3
%13 = load i32, i32* %8
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %74
16: ; preds = %12
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = load i32, i32* %8
%21 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19, i32 %20)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%23 = call i32 @open(i8* %22, i32 0)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%29 = load i64, i64* @errno
%30 = call i8* @strerror(i64 %29)
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %16
store i32 -1, i32* %10
%34 = load i32, i32* %7
%35 = call i32 @read(i32 %34, i32* %10, i32 4)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = icmp ne i64 %37, 4
br i1 %38, label %39, label %57
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%42 = load i32, i32* %9
%43 = load i64, i64* @errno
%44 = icmp ne i64 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%47 = load i64, i64* @errno
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %39
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
br label %53
52: ; preds = %39
br label %53
53: ; preds = %52, %49
%54 = phi i8* [ %51, %49 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), %52 ]
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %42, i8* %46, i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %33
%58 = load i32, i32* %10
%59 = load i32, i32* %8
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%64 = load i32, i32* %10
%65 = load i32, i32* %8
%66 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %63, i32 %64, i32 %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %57
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 1
store i32 %73, i32* %8
br label %12
74: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s/%s-%d\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"test-lab-3-b: open(%s): %s\0A\00"
@errno = global i64 0
@.str.2 = [51 x i8] c"test-lab-3-b: read(%s) returned too little %d%s%s\0A\00"
@.str.3 = [3 x i8] c": \00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [45 x i8] c"test-lab-3-b: checkn %s contained %d not %d\0A\00"
define void @checkn(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %45
9: ; preds = %3, %41
%10 = phi i32 [ %43, %41 ], [ 0, %3 ]
%11 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %10)
%12 = call i32 @open(i8* %7, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i64, i64* @errno
%17 = tail call i8* @strerror(i64 %16)
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %9
store i32 -1, i32* %4
%21 = call i32 @read(i32 %12, i32* %4, i32 4)
%22 = icmp eq i32 %21, 4
br i1 %22, label %34, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i64, i64* @errno
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i8* @strerror(i64 %25)
br label %29
29: ; preds = %23, %27
%30 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), %23 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), %27 ]
%31 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), %23 ], [ %28, %27 ]
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %7, i32 %21, i8* %30, i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, %10
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %7, i32 %35, i32 %10)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = tail call i32 @close(i32 %12)
%43 = add nuw nsw i32 %10, 1
%44 = icmp eq i32 %43, %2
br i1 %44, label %45, label %9
45: ; preds = %41, %3
ret void
}