#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
int /*<<< orphan*/ fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char **, int);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strcat(char *, char *);
int write(int, char **, int);
int main() {
  char *s;
  char *ss;
  char *buf[1];
  struct stat *bf;
  int now;
  int k;
  int err;
  int size;
  int n;
  int numread, numwrite, cursize;
  int sour, dest;
  printf("Enter a file name to split: \n");
  scanf("%s", s);
  sour = open(s, O_RDONLY);
  err = fstat(sour, bf);
  if (err == 0) {
    size = bf->st_size;
    printf("Please set a part size: \n");
    scanf("%d", &n);
    if (n > 1) {
      k = 1;
      numwrite = 0;
      cursize = 0;
      numread = 0;
      now = 0;
      do {
        sprintf(ss, "%d", k);
        ss = strcat(ss, s);
        dest = creat(ss, 00700);
        do {
          numread = read(sour, buf, 1);
          numwrite = write(dest, buf, 1);
          cursize = cursize + numwrite;
          now = now + numwrite;
        } while ((n > now) && (cursize < size));
        fsync(dest);
        close(dest);
        k = k + 1;
        now = 0;
      } while ((cursize < size));
      close(sour);
      return (1);
    } else {
      printf("Size must be greater than zero!\n");
      exit(0);
    }
  } else {
    printf("File does not exists!\n");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   10e0 <fstat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1358 <main+0x198>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
lea    0xe04(%rip),%rdi
lea    -0x3c(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
cmpl   $0x1,-0x3c(%rbp)
jle    1345 <main+0x185>
movl   $0x1,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xdbe(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1c0,%esi
call   10b0 <creat@plt>
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x40(%rbp)
mov    -0x50(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x3c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x2c(%rbp),%ecx
mov    %al,-0x51(%rbp)
jle    12fb <main+0x13b>
mov    -0x48(%rbp),%eax
cmp    -0x38(%rbp),%eax
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    12a4 <main+0xe4>
mov    -0x50(%rbp),%edi
call   1080 <fsync@plt>
mov    -0x50(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x38(%rbp),%eax
jl     126f <main+0xaf>
mov    -0x4c(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%eax
add    $0x60,%rsp
pop    %rbp
ret
lea    0xcf2(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd00(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
lea    0xe25(%rip),%rdi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   10e0 <fstat@plt>
test   %eax,%eax
jne    1292 <main+0xd2>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
cmpl   $0x2,0x4(%rsp)
jl     129b <main+0xdb>
lea    0xdd4(%rip),%rsi
mov    $0x1,%edx
call   10c0 <sprintf@plt>
call   10a0 <strcat@plt>
mov    $0x1c0,%esi
call   10b0 <creat@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1080 <fsync@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
jmp    12a2 <main+0xe2>
lea    0xdb0(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [30 x i8] c"Enter a file name to split: \0A\00"
@.str.1 = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"Please set a part size: \0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [33 x i8] c"Size must be greater than zero!\0A\00"
@.str.5 = [23 x i8] c"File does not exists!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1 x i8*]
%5 = alloca %struct.stat*
%6 = alloca i32
%7 = alloca i32
%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 i32 0, i32* %1
%16 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %2
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %2
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %14
%22 = load i32, i32* %14
%23 = load %struct.stat*, %struct.stat** %5
%24 = call i32 @fstat(i32 %22, %struct.stat* %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %85
27: ; preds = %0
%28 = load %struct.stat*, %struct.stat** %5
%29 = getelementptr inbounds %struct.stat, %struct.stat* %28, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %9
%31 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %10)
%33 = load i32, i32* %10
%34 = icmp sgt i32 %33, 1
br i1 %34, label %35, label %82
35: ; preds = %27
store i32 1, i32* %7
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %11
store i32 0, i32* %6
br label %36
36: ; preds = %75, %35
%37 = load i8*, i8** %3
%38 = load i32, i32* %7
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %3
%41 = load i8*, i8** %2
%42 = call i8* @strcat(i8* %40, i8* %41)
store i8* %42, i8** %3
%43 = load i8*, i8** %3
%44 = call i32 @creat(i8* %43, i32 448)
store i32 %44, i32* %15
br label %45
45: ; preds = %66, %36
%46 = load i32, i32* %14
%47 = getelementptr inbounds [1 x i8*], [1 x i8*]* %4, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8** %47, i32 1)
store i32 %48, i32* %11
%49 = load i32, i32* %15
%50 = getelementptr inbounds [1 x i8*], [1 x i8*]* %4, i64 0, i64 0
%51 = call i32 @write(i32 %49, i8** %50, i32 1)
store i32 %51, i32* %12
%52 = load i32, i32* %13
%53 = load i32, i32* %12
%54 = add nsw i32 %52, %53
store i32 %54, i32* %13
%55 = load i32, i32* %6
%56 = load i32, i32* %12
%57 = add nsw i32 %55, %56
store i32 %57, i32* %6
br label %58
58: ; preds = %45
%59 = load i32, i32* %10
%60 = load i32, i32* %6
%61 = icmp sgt i32 %59, %60
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = load i32, i32* %13
%64 = load i32, i32* %9
%65 = icmp slt i32 %63, %64
br label %66
66: ; preds = %62, %58
%67 = phi i1 [ false, %58 ], [ %65, %62 ]
br i1 %67, label %45, label %68
68: ; preds = %66
%69 = load i32, i32* %15
%70 = call i32 @fsync(i32 %69)
%71 = load i32, i32* %15
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %7
%74 = add nsw i32 %73, 1
store i32 %74, i32* %7
store i32 0, i32* %6
br label %75
75: ; preds = %68
%76 = load i32, i32* %13
%77 = load i32, i32* %9
%78 = icmp slt i32 %76, %77
br i1 %78, label %36, label %79
79: ; preds = %75
%80 = load i32, i32* %14
%81 = call i32 @close(i32 %80)
ret i32 1
82: ; preds = %27
%83 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%84 = call i32 @exit(i32 0)
unreachable
85: ; preds = %0
%86 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%87 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@str = [29 x i8] c"Enter a file name to split: \00"
@str.6 = [22 x i8] c"File does not exists!\00"
@str.7 = [25 x i8] c"Please set a part size: \00"
@str.8 = [32 x i8] c"Size must be greater than zero!\00"
define i32 @main() {
%1 = alloca [1 x i8*]
%2 = alloca i32
%3 = bitcast [1 x i8*]* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* undef)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* undef, i32 %7)
%9 = tail call i32 @fstat(i32 %8, %struct.stat* undef)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %29
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%14 = load i32, i32* %2
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = call i32 @sprintf(i8* dereferenceable(1) undef, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 1)
%18 = call i8* @strcat(i8* dereferenceable(1) undef, i8* dereferenceable(1) undef)
%19 = call i32 @creat(i8* undef, i32 448)
%20 = getelementptr inbounds [1 x i8*], [1 x i8*]* %1, i64 0, i64 0
%21 = call i32 @read(i32 %8, i8** %20, i32 1)
%22 = call i32 @write(i32 %19, i8** %20, i32 1)
%23 = call i32 @fsync(i32 %19)
%24 = call i32 @close(i32 %19)
%25 = call i32 @close(i32 %8)
ret i32 1
26: ; preds = %11
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %0
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
%31 = tail call i32 @exit(i32 0)
unreachable
}