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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ write(int, char const *, int);
int main(int argc, char **argv) {
  if (argc < 3)
    exit(1);
  int count_files = atoi(argv[1]), count_num = atoi(argv[2]);
  for (int i = 0; i < count_files; i++)
    if (!fork()) {
      char i_str[10];
      sprintf(i_str, "%d", i);
      int fd_read = open(i_str, O_RDONLY);
      char i_sum_str[10];
      sprintf(i_sum_str, "%d.sum", i);
      int fd_write = creat(i_sum_str, 0644);
      int sum = 0;
      for (int i = 0; i < count_num; i++) {
        int num;
        if (read(fd_read, (char *)&num, sizeof(int)))
          sum += num;
      }
      write(fd_write, (const char *)&sum, sizeof(int));
      close(fd_write);
      close(fd_read);
    }
  int sum = 0;
  for (int i = 0; i < count_files; i++) {
    char i_sum_str[10];
    sprintf(i_sum_str, "%d.sum", i);
    int fd = open(i_sum_str, O_RDONLY), num;
    read(fd, (char *)&num, sizeof(int));
    sum += num;
  }
  printf("%d\n", sum);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ca <main+0x2a>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12de <main+0x13e>
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%eax
jne    12cb <main+0x12b>
lea    -0x26(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xde5(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x26(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x36(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xdc0(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x36(%rbp),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12aa <main+0x10a>
mov    -0x2c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
je     1297 <main+0xf7>
mov    -0x48(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    129c <main+0xfc>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1267 <main+0xc7>
mov    -0x3c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    12d0 <main+0x130>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f1 <main+0x51>
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1348 <main+0x1a8>
lea    -0x5a(%rbp),%rdi
mov    -0x50(%rbp),%edx
lea    0xcfd(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x5a(%rbp),%rdi
mov    0x2cd2(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%edi
lea    -0x64(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0x64(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    12ec <main+0x14c>
mov    -0x4c(%rbp),%esi
lea    0xcb8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jle    135a <main+0x1ba>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r13d
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
test   %r13d,%r13d
jle    1339 <main+0x199>
mov    %eax,%ebp
mov    0x2e07(%rip),%r15
lea    0x8(%rsp),%r14
xor    %r12d,%r12d
mov    %eax,0xc(%rsp)
mov    %r13d,0x10(%rsp)
jmp    1241 <main+0xa1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,0x8(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x10(%rsp),%r13d
mov    0x2db8(%rip),%r15
mov    0xc(%rsp),%ebp
add    $0x1,%r12d
cmp    %r13d,%r12d
je     12db <main+0x13b>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1234 <main+0x94>
mov    %ebp,%ebx
lea    0x1e(%rsp),%rbp
mov    %rbp,%rdi
lea    0xda3(%rip),%rsi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
mov    (%r15),%esi
mov    %rbp,%rdi
mov    %ebx,%ebp
call   1070 <open@plt>
mov    %eax,%r15d
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
lea    0xd7f(%rip),%rsi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,%r13d
xor    %ebx,%ebx
test   %ebp,%ebp
jg     12bb <main+0x11b>
jmp    1200 <main+0x60>
nopw   0x0(%rax,%rax,1)
add    %ecx,%ebx
add    $0xffffffff,%ebp
je     1200 <main+0x60>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    $0x0,%ecx
test   %rax,%rax
je     12b0 <main+0x110>
mov    0x8(%rsp),%ecx
jmp    12b0 <main+0x110>
test   %r13d,%r13d
jle    1339 <main+0x199>
mov    %r15,%r14
xor    %ebx,%ebx
lea    0x1e(%rsp),%r15
lea    0x14(%rsp),%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
lea    0xcf9(%rip),%rsi
mov    %ebx,%edx
call   10a0 <sprintf@plt>
mov    (%r14),%esi
mov    %r15,%rdi
call   1070 <open@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
add    0x14(%rsp),%ebp
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1300 <main+0x160>
jmp    133b <main+0x19b>
xor    %ebp,%ebp
lea    0xcc8(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%d.sum\00"
@.str.2 = [4 x i8] c"%d\0A\00"
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 [10 x i8]
%10 = alloca i32
%11 = alloca [10 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca [10 x i8]
%19 = alloca i32
%20 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 3
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %7
store i32 0, i32* %8
br label %34
34: ; preds = %79, %25
%35 = load i32, i32* %8
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %82
38: ; preds = %34
%39 = call i32 (...) @fork()
%40 = icmp ne i32 %39, 0
br i1 %40, label %78, label %41
41: ; preds = %38
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%43 = load i32, i32* %8
%44 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* @O_RDONLY
%47 = call i32 @open(i8* %45, i32 %46)
store i32 %47, i32* %10
%48 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%49 = load i32, i32* %8
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %49)
%51 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%52 = call i32 @creat(i8* %51, i32 420)
store i32 %52, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
br label %53
53: ; preds = %67, %41
%54 = load i32, i32* %14
%55 = load i32, i32* %7
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %70
57: ; preds = %53
%58 = load i32, i32* %10
%59 = bitcast i32* %15 to i8*
%60 = call i64 @read(i32 %58, i8* %59, i32 4)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = load i32, i32* %15
%64 = load i32, i32* %13
%65 = add nsw i32 %64, %63
store i32 %65, i32* %13
br label %66
66: ; preds = %62, %57
br label %67
67: ; preds = %66
%68 = load i32, i32* %14
%69 = add nsw i32 %68, 1
store i32 %69, i32* %14
br label %53
70: ; preds = %53
%71 = load i32, i32* %12
%72 = bitcast i32* %13 to i8*
%73 = call i32 @write(i32 %71, i8* %72, i32 4)
%74 = load i32, i32* %12
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %10
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %70, %38
br label %79
79: ; preds = %78
%80 = load i32, i32* %8
%81 = add nsw i32 %80, 1
store i32 %81, i32* %8
br label %34
82: ; preds = %34
store i32 0, i32* %16
store i32 0, i32* %17
br label %83
83: ; preds = %100, %82
%84 = load i32, i32* %17
%85 = load i32, i32* %6
%86 = icmp slt i32 %84, %85
br i1 %86, label %87, label %103
87: ; preds = %83
%88 = getelementptr inbounds [10 x i8], [10 x i8]* %18, i64 0, i64 0
%89 = load i32, i32* %17
%90 = call i32 @sprintf(i8* %88, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %89)
%91 = getelementptr inbounds [10 x i8], [10 x i8]* %18, i64 0, i64 0
%92 = load i32, i32* @O_RDONLY
%93 = call i32 @open(i8* %91, i32 %92)
store i32 %93, i32* %19
%94 = load i32, i32* %19
%95 = bitcast i32* %20 to i8*
%96 = call i64 @read(i32 %94, i8* %95, i32 4)
%97 = load i32, i32* %20
%98 = load i32, i32* %16
%99 = add nsw i32 %98, %97
store i32 %99, i32* %16
br label %100
100: ; preds = %87
%101 = load i32, i32* %17
%102 = add nsw i32 %101, 1
store i32 %102, i32* %17
br label %83
103: ; preds = %83
%104 = load i32, i32* %16
%105 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %104)
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%d.sum\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca [10 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca i32
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = icmp sgt i32 %15, 0
br i1 %19, label %20, label %58
20: ; preds = %12
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%23 = bitcast i32* %5 to i8*
%24 = icmp sgt i32 %18, 0
%25 = bitcast i32* %6 to i8*
br label %30
26: ; preds = %55
br i1 %19, label %27, label %58
27: ; preds = %26
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%29 = bitcast i32* %8 to i8*
br label %61
30: ; preds = %20, %55
%31 = phi i32 [ 0, %20 ], [ %56, %55 ]
%32 = call i32 (...) @fork()
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %55
34: ; preds = %30
%35 = call i32 @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %31)
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %21, i32 %36)
%38 = call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%39 = call i32 @creat(i8* %22, i32 420)
br i1 %24, label %45, label %40
40: ; preds = %45, %34
%41 = phi i32 [ 0, %34 ], [ %52, %45 ]
store i32 %41, i32* %5
%42 = call i32 @write(i32 %39, i8* %23, i32 4)
%43 = call i32 @close(i32 %39)
%44 = call i32 @close(i32 %37)
br label %55
45: ; preds = %34, %45
%46 = phi i32 [ %53, %45 ], [ 0, %34 ]
%47 = phi i32 [ %52, %45 ], [ 0, %34 ]
%48 = call i64 @read(i32 %37, i8* %25, i32 4)
%49 = icmp eq i64 %48, 0
%50 = load i32, i32* %6
%51 = select i1 %49, i32 0, i32 %50
%52 = add nsw i32 %47, %51
%53 = add nuw nsw i32 %46, 1
%54 = icmp eq i32 %53, %18
br i1 %54, label %40, label %45
55: ; preds = %30, %40
%56 = add nuw nsw i32 %31, 1
%57 = icmp eq i32 %56, %15
br i1 %57, label %26, label %30
58: ; preds = %61, %12, %26
%59 = phi i32 [ 0, %26 ], [ 0, %12 ], [ %69, %61 ]
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %59)
ret i32 0
61: ; preds = %27, %61
%62 = phi i32 [ 0, %27 ], [ %70, %61 ]
%63 = phi i32 [ 0, %27 ], [ %69, %61 ]
%64 = call i32 @sprintf(i8* %28, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %62)
%65 = load i32, i32* @O_RDONLY
%66 = call i32 @open(i8* %28, i32 %65)
%67 = call i64 @read(i32 %66, i8* %29, i32 4)
%68 = load i32, i32* %8
%69 = add nsw i32 %68, %63
%70 = add nuw nsw i32 %62, 1
%71 = icmp eq i32 %70, %15
br i1 %71, label %58, label %61
}