#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 uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ yap_sysquit(char *, char *);
void read_didwid(char *wfile, char *dfile, int N, uint32_t *d, uint32_t *w) {
  int i, wt, dt;
  FILE *fp1 = fopen(wfile, "r");
  FILE *fp2 = fopen(dfile, "r");
  if (!fp1)
    yap_sysquit("Cannot open file '%s' for read\n", wfile);
  if (!fp2)
    yap_sysquit("Cannot open file '%s' for read\n", dfile);
  for (i = 0; i < N; i++) {
    if (fscanf(fp1, "%d", &wt))
      w[i] = wt - 1;
    if (fscanf(fp2, "%d", &dt))
      d[i] = dt - 1;
  }
  if (ferror(fp1))
    yap_sysquit("Error on reading file '%s' ", wfile);
  if (ferror(fp2))
    yap_sysquit("Error on reading file '%s' ", dfile);
  fclose(fp1);
  fclose(fp2);
}


x86 assembly (clang, -O0):
<read_didwid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11ae <read_didwid+0x5e>
mov    -0x8(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1070 <yap_sysquit@plt>
cmpq   $0x0,-0x48(%rbp)
jne    11c9 <read_didwid+0x79>
mov    -0x10(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1070 <yap_sysquit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    124d <read_didwid+0xfd>
mov    -0x40(%rbp),%rdi
lea    0xe3b(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x0,%rax
je     120b <read_didwid+0xbb>
mov    -0x30(%rbp),%edx
sub    $0x1,%edx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x48(%rbp),%rdi
lea    0xe0c(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x0,%rax
je     123a <read_didwid+0xea>
mov    -0x34(%rbp),%edx
sub    $0x1,%edx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    123f <read_didwid+0xef>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d0 <read_didwid+0x80>
mov    -0x40(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1270 <read_didwid+0x120>
mov    -0x8(%rbp),%rsi
lea    0xdba(%rip),%rdi
call   1070 <yap_sysquit@plt>
mov    -0x48(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1293 <read_didwid+0x143>
mov    -0x10(%rbp),%rsi
lea    0xd97(%rip),%rdi
call   1070 <yap_sysquit@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_didwid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,0x20(%rsp)
mov    %rcx,0x18(%rsp)
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r12
lea    0xe88(%rip),%rbx
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %r15,%r15
jne    11aa <read_didwid+0x5a>
lea    0xe60(%rip),%rdi
mov    %r12,%rsi
call   1070 <yap_sysquit@plt>
mov    %r12,0x10(%rsp)
test   %rbx,%rbx
je     11be <read_didwid+0x6e>
test   %r14d,%r14d
jg     11d4 <read_didwid+0x84>
jmp    1242 <read_didwid+0xf2>
lea    0xe3d(%rip),%rdi
mov    0x8(%rsp),%rsi
call   1070 <yap_sysquit@plt>
test   %r14d,%r14d
jle    1242 <read_didwid+0xf2>
mov    %r14d,%r12d
xor    %r14d,%r14d
lea    0xe41(%rip),%rbp
mov    %rsp,%r13
jmp    11f9 <read_didwid+0xa9>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r14,%r12
je     1242 <read_didwid+0xf2>
mov    %r15,%rdi
mov    %rbp,%rsi
lea    0x4(%rsp),%rdx
call   1050 <fscanf@plt>
test   %rax,%rax
je     121e <read_didwid+0xce>
mov    0x4(%rsp),%eax
add    $0xffffffff,%eax
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx,%r14,4)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1050 <fscanf@plt>
test   %rax,%rax
je     11f0 <read_didwid+0xa0>
mov    (%rsp),%eax
add    $0xffffffff,%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx,%r14,4)
jmp    11f0 <read_didwid+0xa0>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1260 <read_didwid+0x110>
lea    0xdcf(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1070 <yap_sysquit@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     127e <read_didwid+0x12e>
lea    0xdb1(%rip),%rdi
mov    0x8(%rsp),%rsi
call   1070 <yap_sysquit@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [28 x i8] c"Error on reading file '%s' \00"
define void @read_didwid(i8* %0, i8* %1, i32 %2, i32* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %14
%18 = load i8*, i8** %7
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %15
%20 = load i32*, i32** %14
%21 = icmp ne i32* %20, null
br i1 %21, label %25, label %22
22: ; preds = %5
%23 = load i8*, i8** %6
%24 = call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %5
%26 = load i32*, i32** %15
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %28, %25
store i32 0, i32* %11
br label %32
32: ; preds = %59, %31
%33 = load i32, i32* %11
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %62
36: ; preds = %32
%37 = load i32*, i32** %14
%38 = call i64 @fscanf(i32* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %12)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i32, i32* %12
%42 = sub nsw i32 %41, 1
%43 = load i32*, i32** %10
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
store i32 %42, i32* %46
br label %47
47: ; preds = %40, %36
%48 = load i32*, i32** %15
%49 = call i64 @fscanf(i32* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %13)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i32, i32* %13
%53 = sub nsw i32 %52, 1
%54 = load i32*, i32** %9
%55 = load i32, i32* %11
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
store i32 %53, i32* %57
br label %58
58: ; preds = %51, %47
br label %59
59: ; preds = %58
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 1
store i32 %61, i32* %11
br label %32
62: ; preds = %32
%63 = load i32*, i32** %14
%64 = call i64 @ferror(i32* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i8*, i8** %6
%68 = call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %62
%70 = load i32*, i32** %15
%71 = call i64 @ferror(i32* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i8*, i8** %7
%75 = call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %73, %69
%77 = load i32*, i32** %14
%78 = call i32 @fclose(i32* %77)
%79 = load i32*, i32** %15
%80 = call i32 @fclose(i32* %79)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [28 x i8] c"Error on reading file '%s' \00"
define void @read_didwid(i8* %0, i8* %1, i32 %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %10, null
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %5
%16 = icmp eq i32* %11, null
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %19
19: ; preds = %17, %15
%20 = icmp sgt i32 %2, 0
br i1 %20, label %21, label %41
21: ; preds = %19
%22 = zext i32 %2 to i64
br label %23
23: ; preds = %21, %38
%24 = phi i64 [ 0, %21 ], [ %39, %38 ]
%25 = call i64 @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32, i32* %6
%29 = add nsw i32 %28, -1
%30 = getelementptr inbounds i32, i32* %4, i64 %24
store i32 %29, i32* %30
br label %31
31: ; preds = %27, %23
%32 = call i64 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i32, i32* %7
%36 = add nsw i32 %35, -1
%37 = getelementptr inbounds i32, i32* %3, i64 %24
store i32 %36, i32* %37
br label %38
38: ; preds = %31, %34
%39 = add nuw nsw i64 %24, 1
%40 = icmp eq i64 %39, %22
br i1 %40, label %41, label %23
41: ; preds = %38, %19
%42 = call i64 @ferror(i32* %10)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %46
46: ; preds = %44, %41
%47 = call i64 @ferror(i32* %11)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %51
51: ; preds = %49, %46
%52 = call i32 @fclose(i32* %10)
%53 = call i32 @fclose(i32* %11)
ret void
}