#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_RDWR;
int /*<<< orphan*/ close(int);
int ftruncate(int, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int mkostemp(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pread(int, char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ pwrite(int, char *, int, int);
int random();
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, ...);
scalar_t__ strchr(char *, char);
int main(int argc, char *argv[]) {
  char tmp_name[256];
  char tmp_path[128];
  int flags;
  char *buf;
  int buf_sz;
  int file_sz;
  int do_pread, do_pwrite, do_ftrunc;
  int fd, ret, i, iters, nr_open, nr_arg;
  char cmd[16];
  if (argc < (4 + 1)) {
    printf("test_fs <path> <iters> <[rwt] <buf_sz>\n");
    printf("r - pread , w - pwrite, t - trunc\n");
    return 0;
  }
  nr_arg = 1;
  sscanf(argv[nr_arg++], "%s", tmp_path);
  sscanf(argv[nr_arg++], "%d", &iters);
  sscanf(argv[nr_arg++], "%s", cmd);
  sscanf(argv[nr_arg++], "%d", &buf_sz);
  do_pread = 0;
  do_pwrite = 0;
  do_ftrunc = 0;
  fd = 0;
  file_sz = iters * buf_sz;
  if (strchr(cmd, 'r'))
    do_pread = 1;
  if (strchr(cmd, 'w'))
    do_pwrite = 1;
  if (strchr(cmd, 't'))
    do_ftrunc = 1;
  if (do_pread || do_pwrite)
    nr_open = 1;
  else
    nr_open = iters;
  printf("path = %s iter = %d cmd = %s buf_sz = %d \n", tmp_path, iters, cmd,
         buf_sz);
  printf("file_sz = %d nr_open = %d\n", file_sz, nr_open);
  printf("do_pread = %d do_pwrite = %d do_ftrunc = %d\n", do_pread, do_pwrite,
         do_ftrunc);
  buf = malloc(buf_sz);
  memset(buf, 0xaa, buf_sz);
  for (i = 0; i < iters; i++) {
    if (nr_open > 0 && nr_open--) {
      if (fd > 0) {
        close(fd);
        remove(tmp_name);
      }
      sprintf(tmp_name, "%s/test_fscalls.tmp.XXXXXXX", tmp_path);
      fd = mkostemp(tmp_name, O_RDWR);
      if (fd < 0) {
        perror("mkostemp:");
        return -1;
      }
      if (do_ftrunc) {
        if ((ret = ftruncate(fd, file_sz)) < 0) {
          perror("ftruncate:");
          return ret;
        }
      }
    }
    if (do_pwrite) {
      if ((ret = pwrite(fd, buf, buf_sz, (i * buf_sz) / iters) < 0)) {
        perror("pwrite:");
        return ret;
      }
    }
    if (do_pread) {
      if ((ret = pread(fd, buf, buf_sz, random() % file_sz)) < 0) {
        perror("pread:");
        return ret;
      }
    }
  }
  if (fd > 0) {
    close(fd);
    remove(tmp_name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    122b <main+0x4b>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    16cb <main+0x4eb>
movl   $0x1,-0x1cc(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1cc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1cc(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x190(%rbp),%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   10c0 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    -0x1cc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1cc(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdc5(%rip),%rsi
lea    -0x1c4(%rbp),%rdx
mov    $0x0,%al
call   10c0 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    -0x1cc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1cc(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x1e0(%rbp),%rdx
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   10c0 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    -0x1cc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1cc(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd63(%rip),%rsi
lea    -0x1a4(%rbp),%rdx
mov    $0x0,%al
call   10c0 <sscanf@plt>
movl   $0x0,-0x1ac(%rbp)
movl   $0x0,-0x1b0(%rbp)
movl   $0x0,-0x1b4(%rbp)
movl   $0x0,-0x1b8(%rbp)
mov    -0x1c4(%rbp),%eax
imul   -0x1a4(%rbp),%eax
mov    %eax,-0x1a8(%rbp)
lea    -0x1e0(%rbp),%rdi
mov    $0x72,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1359 <main+0x179>
movl   $0x1,-0x1ac(%rbp)
lea    -0x1e0(%rbp),%rdi
mov    $0x77,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     137e <main+0x19e>
movl   $0x1,-0x1b0(%rbp)
lea    -0x1e0(%rbp),%rdi
mov    $0x74,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     13a3 <main+0x1c3>
movl   $0x1,-0x1b4(%rbp)
cmpl   $0x0,-0x1ac(%rbp)
jne    13bd <main+0x1dd>
cmpl   $0x0,-0x1b0(%rbp)
je     13cc <main+0x1ec>
movl   $0x1,-0x1c8(%rbp)
jmp    13d8 <main+0x1f8>
mov    -0x1c4(%rbp),%eax
mov    %eax,-0x1c8(%rbp)
lea    -0x190(%rbp),%rsi
mov    -0x1c4(%rbp),%edx
lea    -0x1e0(%rbp),%rcx
mov    -0x1a4(%rbp),%r8d
lea    0xc57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1a8(%rbp),%esi
mov    -0x1c8(%rbp),%edx
lea    0xc68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1ac(%rbp),%esi
mov    -0x1b0(%rbp),%edx
mov    -0x1b4(%rbp),%ecx
lea    0xc63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1a4(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x1a0(%rbp)
mov    -0x1a0(%rbp),%rdi
mov    -0x1a4(%rbp),%edx
mov    $0xaa,%esi
call   1080 <memset@plt>
movl   $0x0,-0x1c0(%rbp)
mov    -0x1c0(%rbp),%eax
cmp    -0x1c4(%rbp),%eax
jge    16a0 <main+0x4c0>
cmpl   $0x0,-0x1c8(%rbp)
jle    1576 <main+0x396>
mov    -0x1c8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c8(%rbp)
cmp    $0x0,%eax
je     1576 <main+0x396>
cmpl   $0x0,-0x1b8(%rbp)
jle    14cb <main+0x2eb>
mov    -0x1b8(%rbp),%edi
call   1090 <close@plt>
lea    -0x110(%rbp),%rdi
call   1030 <remove@plt>
lea    -0x110(%rbp),%rdi
lea    -0x190(%rbp),%rdx
lea    0xbe4(%rip),%rsi
call   1100 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2af5(%rip),%rax
mov    (%rax),%esi
call   1070 <mkostemp@plt>
mov    %eax,-0x1b8(%rbp)
cmpl   $0x0,-0x1b8(%rbp)
jge    1525 <main+0x345>
lea    0xbcc(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16cb <main+0x4eb>
cmpl   $0x0,-0x1b4(%rbp)
je     1571 <main+0x391>
mov    -0x1b8(%rbp),%edi
mov    -0x1a8(%rbp),%esi
call   1060 <ftruncate@plt>
mov    %eax,-0x1bc(%rbp)
cmp    $0x0,%eax
jge    156c <main+0x38c>
lea    0xb91(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x1bc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    16cb <main+0x4eb>
jmp    1571 <main+0x391>
jmp    1576 <main+0x396>
cmpl   $0x0,-0x1b0(%rbp)
je     15fe <main+0x41e>
mov    -0x1b8(%rbp),%edi
mov    -0x1a0(%rbp),%rsi
mov    -0x1a4(%rbp),%eax
mov    %eax,-0x1e4(%rbp)
mov    -0x1c0(%rbp),%eax
imul   -0x1a4(%rbp),%eax
cltd
idivl  -0x1c4(%rbp)
mov    -0x1e4(%rbp),%edx
mov    %eax,%ecx
call   10d0 <pwrite@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1bc(%rbp)
test   $0x1,%al
jne    15df <main+0x3ff>
jmp    15f9 <main+0x419>
lea    0xb0f(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x1bc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    16cb <main+0x4eb>
jmp    15fe <main+0x41e>
cmpl   $0x0,-0x1ac(%rbp)
je     1687 <main+0x4a7>
mov    -0x1b8(%rbp),%eax
mov    %eax,-0x1f4(%rbp)
mov    -0x1a0(%rbp),%rax
mov    %rax,-0x1f0(%rbp)
mov    -0x1a4(%rbp),%eax
mov    %eax,-0x1e8(%rbp)
mov    $0x0,%al
call   10a0 <random@plt>
mov    -0x1f4(%rbp),%edi
mov    -0x1f0(%rbp),%rsi
cltd
idivl  -0x1a8(%rbp)
mov    %edx,%ecx
mov    -0x1e8(%rbp),%edx
call   10f0 <pread@plt>
mov    %eax,-0x1bc(%rbp)
cmp    $0x0,%eax
jge    1682 <main+0x4a2>
lea    0xa8e(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x1bc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    16cb <main+0x4eb>
jmp    1687 <main+0x4a7>
jmp    168c <main+0x4ac>
mov    -0x1c0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c0(%rbp)
jmp    146e <main+0x28e>
cmpl   $0x0,-0x1b8(%rbp)
jle    16c4 <main+0x4e4>
mov    -0x1b8(%rbp),%edi
call   1090 <close@plt>
lea    -0x110(%rbp),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x200,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1c8,%rsp
cmp    $0x4,%edi
jg     1225 <main+0x35>
lea    0xeac(%rip),%rdi
call   1040 <puts@plt>
lea    0xec7(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
jmp    149b <main+0x2ab>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xdcd(%rip),%r14
xor    %ebp,%ebp
lea    0x40(%rsp),%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   10d0 <sscanf@plt>
mov    0x10(%rbx),%rdi
lea    0xdb4(%rip),%r15
lea    0xc(%rsp),%rdx
mov    %r15,%rsi
xor    %eax,%eax
call   10d0 <sscanf@plt>
mov    0x18(%rbx),%rdi
lea    0x30(%rsp),%rdx
mov    %r14,%rsi
mov    %rdx,%r14
xor    %eax,%eax
call   10d0 <sscanf@plt>
mov    0x20(%rbx),%rdi
lea    0x10(%rsp),%rdx
mov    %r15,%rsi
xor    %eax,%eax
call   10d0 <sscanf@plt>
mov    0x10(%rsp),%r13d
imul   0xc(%rsp),%r13d
mov    %r14,%r15
mov    %r14,%rdi
mov    $0x72,%esi
call   1050 <strchr@plt>
mov    %rax,%r14
xor    %eax,%eax
test   %r14,%r14
setne  %al
mov    %eax,0x14(%rsp)
mov    %r15,%rdi
mov    $0x77,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
xor    %r12d,%r12d
test   %rax,%rax
setne  %r12b
mov    %r15,%rdi
mov    $0x74,%esi
call   1050 <strchr@plt>
xor    %r15d,%r15d
mov    %rax,0x18(%rsp)
test   %rax,%rax
setne  %r15b
mov    0xc(%rsp),%edx
mov    %r14,0x20(%rsp)
mov    %rbx,0x28(%rsp)
or     %rbx,%r14
mov    $0x1,%ebx
cmove  %edx,%ebx
mov    0x10(%rsp),%r8d
lea    0xcfa(%rip),%rdi
lea    0x40(%rsp),%rsi
lea    0x30(%rsp),%rcx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd0d(%rip),%rdi
mov    %r13d,%r14d
mov    %r13d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd12(%rip),%rdi
mov    0x14(%rsp),%esi
mov    %r12d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%r12d
mov    %r12d,%edi
call   10c0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0xaa,%esi
mov    %r12d,%edx
call   1090 <memset@plt>
cmpl   $0x0,0xc(%rsp)
jle    149b <main+0x2ab>
lea    0xc0(%rsp),%rbp
xor    %r13d,%r13d
xor    %r12d,%r12d
jmp    13a7 <main+0x1b7>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r13d
cmp    0xc(%rsp),%r13d
lea    0xc0(%rsp),%rbp
jge    147f <main+0x28f>
test   %ebx,%ebx
jle    140e <main+0x21e>
test   %r12d,%r12d
jle    13c0 <main+0x1d0>
mov    %r12d,%edi
call   10a0 <close@plt>
mov    %rbp,%rdi
call   1030 <remove@plt>
mov    %rbp,%rdi
lea    0xcaf(%rip),%rsi
lea    0x40(%rsp),%rdx
call   1110 <sprintf@plt>
mov    0x2c0d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <mkostemp@plt>
test   %eax,%eax
js     14af <main+0x2bf>
mov    %eax,%r12d
add    $0xffffffff,%ebx
cmpq   $0x0,0x18(%rsp)
je     140e <main+0x21e>
mov    %r12d,%edi
mov    %r14d,%esi
call   1070 <ftruncate@plt>
test   %eax,%eax
js     14d5 <main+0x2e5>
cmpq   $0x0,0x28(%rsp)
je     143d <main+0x24d>
mov    0x10(%rsp),%ecx
mov    %ecx,%eax
imul   %r13d,%eax
cltd
idivl  0xc(%rsp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ecx,%edx
mov    %eax,%ecx
call   10e0 <pwrite@plt>
test   %rax,%rax
js     14c2 <main+0x2d2>
cmpq   $0x0,0x20(%rsp)
je     1390 <main+0x1a0>
mov    0x10(%rsp),%ebp
xor    %eax,%eax
call   10b0 <random@plt>
cltd
idiv   %r14d
mov    %edx,%ecx
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1100 <pread@plt>
test   %eax,%eax
jns    1390 <main+0x1a0>
mov    %eax,%ebp
lea    0xc3a(%rip),%rdi
call   10f0 <perror@plt>
jmp    149b <main+0x2ab>
xor    %ebp,%ebp
test   %r12d,%r12d
jle    149b <main+0x2ab>
mov    %r12d,%edi
call   10a0 <close@plt>
lea    0xc0(%rsp),%rdi
call   1030 <remove@plt>
mov    %ebp,%eax
add    $0x1c8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbdf(%rip),%rdi
call   10f0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    149b <main+0x2ab>
lea    0xbe1(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%ebp
jmp    149b <main+0x2ab>
mov    %eax,%ebp
lea    0xbc1(%rip),%rdi
call   10f0 <perror@plt>
jmp    149b <main+0x2ab>

LLVM IR (-O0):
@.str = [40 x i8] c"test_fs <path> <iters> <[rwt] <buf_sz>\0A\00"
@.str.1 = [35 x i8] c"r - pread , w - pwrite, t - trunc\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [43 x i8] c"path = %s iter = %d cmd = %s buf_sz = %d \0A\00"
@.str.5 = [27 x i8] c"file_sz = %d nr_open = %d\0A\00"
@.str.6 = [45 x i8] c"do_pread = %d do_pwrite = %d do_ftrunc = %d\0A\00"
@.str.7 = [28 x i8] c"%s/test_fscalls.tmp.XXXXXXX\00"
@O_RDWR = global i32 0
@.str.8 = [10 x i8] c"mkostemp:\00"
@.str.9 = [11 x i8] c"ftruncate:\00"
@.str.10 = [8 x i8] c"pwrite:\00"
@.str.11 = [7 x i8] c"pread:\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca [16 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 5
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %193
27: ; preds = %2
store i32 1, i32* %20
%28 = load i8**, i8*** %5
%29 = load i32, i32* %20
%30 = add nsw i32 %29, 1
store i32 %30, i32* %20
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%35 = call i32 (i8*, i8*, ...) @sscanf(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i8**, i8*** %5
%37 = load i32, i32* %20
%38 = add nsw i32 %37, 1
store i32 %38, i32* %20
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8*, i8** %36, i64 %39
%41 = load i8*, i8** %40
%42 = call i32 (i8*, i8*, ...) @sscanf(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %18)
%43 = load i8**, i8*** %5
%44 = load i32, i32* %20
%45 = add nsw i32 %44, 1
store i32 %45, i32* %20
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8*, i8** %43, i64 %46
%48 = load i8*, i8** %47
%49 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%50 = call i32 (i8*, i8*, ...) @sscanf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = load i8**, i8*** %5
%52 = load i32, i32* %20
%53 = add nsw i32 %52, 1
store i32 %53, i32* %20
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds i8*, i8** %51, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 (i8*, i8*, ...) @sscanf(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %10)
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
%58 = load i32, i32* %18
%59 = load i32, i32* %10
%60 = mul nsw i32 %58, %59
store i32 %60, i32* %11
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%62 = call i64 @strchr(i8* %61, i8 signext 114)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %27
store i32 1, i32* %12
br label %65
65: ; preds = %64, %27
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%67 = call i64 @strchr(i8* %66, i8 signext 119)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %65
store i32 1, i32* %13
br label %70
70: ; preds = %69, %65
%71 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%72 = call i64 @strchr(i8* %71, i8 signext 116)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %70
store i32 1, i32* %14
br label %75
75: ; preds = %74, %70
%76 = load i32, i32* %12
%77 = icmp ne i32 %76, 0
br i1 %77, label %81, label %78
78: ; preds = %75
%79 = load i32, i32* %13
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %78, %75
store i32 1, i32* %19
br label %84
82: ; preds = %78
%83 = load i32, i32* %18
store i32 %83, i32* %19
br label %84
84: ; preds = %82, %81
%85 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%86 = load i32, i32* %18
%87 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%88 = load i32, i32* %10
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %85, i32 %86, i8* %87, i32 %88)
%90 = load i32, i32* %11
%91 = load i32, i32* %19
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %90, i32 %91)
%93 = load i32, i32* %12
%94 = load i32, i32* %13
%95 = load i32, i32* %14
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %93, i32 %94, i32 %95)
%97 = load i32, i32* %10
%98 = call i8* @malloc(i32 %97)
store i8* %98, i8** %9
%99 = load i8*, i8** %9
%100 = load i32, i32* %10
%101 = call i32 @memset(i8* %99, i32 170, i32 %100)
store i32 0, i32* %17
br label %102
102: ; preds = %181, %84
%103 = load i32, i32* %17
%104 = load i32, i32* %18
%105 = icmp slt i32 %103, %104
br i1 %105, label %106, label %184
106: ; preds = %102
%107 = load i32, i32* %19
%108 = icmp sgt i32 %107, 0
br i1 %108, label %109, label %145
109: ; preds = %106
%110 = load i32, i32* %19
%111 = add nsw i32 %110, -1
store i32 %111, i32* %19
%112 = icmp ne i32 %110, 0
br i1 %112, label %113, label %145
113: ; preds = %109
%114 = load i32, i32* %15
%115 = icmp sgt i32 %114, 0
br i1 %115, label %116, label %121
116: ; preds = %113
%117 = load i32, i32* %15
%118 = call i32 @close(i32 %117)
%119 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%120 = call i32 @remove(i8* %119)
br label %121
121: ; preds = %116, %113
%122 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%123 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%124 = call i32 @sprintf(i8* %122, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i8* %123)
%125 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%126 = load i32, i32* @O_RDWR
%127 = call i32 @mkostemp(i8* %125, i32 %126)
store i32 %127, i32* %15
%128 = load i32, i32* %15
%129 = icmp slt i32 %128, 0
br i1 %129, label %130, label %132
130: ; preds = %121
%131 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %193
132: ; preds = %121
%133 = load i32, i32* %14
%134 = icmp ne i32 %133, 0
br i1 %134, label %135, label %144
135: ; preds = %132
%136 = load i32, i32* %15
%137 = load i32, i32* %11
%138 = call i32 @ftruncate(i32 %136, i32 %137)
store i32 %138, i32* %16
%139 = icmp slt i32 %138, 0
br i1 %139, label %140, label %143
140: ; preds = %135
%141 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%142 = load i32, i32* %16
store i32 %142, i32* %3
br label %193
143: ; preds = %135
br label %144
144: ; preds = %143, %132
br label %145
145: ; preds = %144, %109, %106
%146 = load i32, i32* %13
%147 = icmp ne i32 %146, 0
br i1 %147, label %148, label %164
148: ; preds = %145
%149 = load i32, i32* %15
%150 = load i8*, i8** %9
%151 = load i32, i32* %10
%152 = load i32, i32* %17
%153 = load i32, i32* %10
%154 = mul nsw i32 %152, %153
%155 = load i32, i32* %18
%156 = sdiv i32 %154, %155
%157 = call i64 @pwrite(i32 %149, i8* %150, i32 %151, i32 %156)
%158 = icmp slt i64 %157, 0
%159 = zext i1 %158 to i32
store i32 %159, i32* %16
br i1 %158, label %160, label %163
160: ; preds = %148
%161 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
%162 = load i32, i32* %16
store i32 %162, i32* %3
br label %193
163: ; preds = %148
br label %164
164: ; preds = %163, %145
%165 = load i32, i32* %12
%166 = icmp ne i32 %165, 0
br i1 %166, label %167, label %180
167: ; preds = %164
%168 = load i32, i32* %15
%169 = load i8*, i8** %9
%170 = load i32, i32* %10
%171 = call i32 (...) @random()
%172 = load i32, i32* %11
%173 = srem i32 %171, %172
%174 = call i32 @pread(i32 %168, i8* %169, i32 %170, i32 %173)
store i32 %174, i32* %16
%175 = icmp slt i32 %174, 0
br i1 %175, label %176, label %179
176: ; preds = %167
%177 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0))
%178 = load i32, i32* %16
store i32 %178, i32* %3
br label %193
179: ; preds = %167
br label %180
180: ; preds = %179, %164
br label %181
181: ; preds = %180
%182 = load i32, i32* %17
%183 = add nsw i32 %182, 1
store i32 %183, i32* %17
br label %102
184: ; preds = %102
%185 = load i32, i32* %15
%186 = icmp sgt i32 %185, 0
br i1 %186, label %187, label %192
187: ; preds = %184
%188 = load i32, i32* %15
%189 = call i32 @close(i32 %188)
%190 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%191 = call i32 @remove(i8* %190)
br label %192
192: ; preds = %187, %184
store i32 0, i32* %3
br label %193
193: ; preds = %192, %176, %160, %140, %130, %24
%194 = load i32, i32* %3
ret i32 %194
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [43 x i8] c"path = %s iter = %d cmd = %s buf_sz = %d \0A\00"
@.str.5 = [27 x i8] c"file_sz = %d nr_open = %d\0A\00"
@.str.6 = [45 x i8] c"do_pread = %d do_pwrite = %d do_ftrunc = %d\0A\00"
@.str.7 = [28 x i8] c"%s/test_fscalls.tmp.XXXXXXX\00"
@O_RDWR = global i32 0
@.str.8 = [10 x i8] c"mkostemp:\00"
@.str.9 = [11 x i8] c"ftruncate:\00"
@.str.10 = [8 x i8] c"pwrite:\00"
@.str.11 = [7 x i8] c"pread:\00"
@str = [39 x i8] c"test_fs <path> <iters> <[rwt] <buf_sz>\00"
@str.12 = [34 x i8] c"r - pread , w - pwrite, t - trunc\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca [128 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%10 = bitcast i32* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%13 = icmp slt i32 %0, 5
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.12, i64 0, i64 0))
br label %111
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8*, i8*, ...) @sscanf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = call i32 (i8*, i8*, ...) @sscanf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = call i32 (i8*, i8*, ...) @sscanf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%27 = getelementptr inbounds i8*, i8** %1, i64 4
%28 = load i8*, i8** %27
%29 = call i32 (i8*, i8*, ...) @sscanf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%30 = load i32, i32* %6
%31 = load i32, i32* %5
%32 = mul nsw i32 %31, %30
%33 = call i64 @strchr(i8* %12, i8 signext 114)
%34 = icmp eq i64 %33, 0
%35 = xor i1 %34, true
%36 = zext i1 %35 to i32
%37 = call i64 @strchr(i8* %12, i8 signext 119)
%38 = icmp eq i64 %37, 0
%39 = xor i1 %38, true
%40 = zext i1 %39 to i32
%41 = call i64 @strchr(i8* %12, i8 signext 116)
%42 = icmp eq i64 %41, 0
%43 = xor i1 %42, true
%44 = zext i1 %43 to i32
%45 = select i1 %34, i1 %38, i1 false
%46 = load i32, i32* %6
%47 = select i1 %45, i32 %46, i32 1
%48 = load i32, i32* %5
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %9, i32 %46, i8* %12, i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 %47)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %36, i32 %40, i32 %44)
%52 = load i32, i32* %5
%53 = call i8* @malloc(i32 %52)
%54 = call i32 @memset(i8* %53, i32 170, i32 %52)
%55 = load i32, i32* %6
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %111
57: ; preds = %17, %102
%58 = phi i32 [ %83, %102 ], [ %47, %17 ]
%59 = phi i32 [ %103, %102 ], [ 0, %17 ]
%60 = phi i32 [ %82, %102 ], [ 0, %17 ]
%61 = icmp sgt i32 %58, 0
br i1 %61, label %62, label %81
62: ; preds = %57
%63 = add nsw i32 %58, -1
%64 = icmp sgt i32 %60, 0
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = call i32 @close(i32 %60)
%67 = call i32 @remove(i8* %8)
br label %68
68: ; preds = %65, %62
%69 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i8* %9)
%70 = load i32, i32* @O_RDWR
%71 = call i32 @mkostemp(i8* %8, i32 %70)
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %111
75: ; preds = %68
br i1 %42, label %81, label %76
76: ; preds = %75
%77 = call i32 @ftruncate(i32 %71, i32 %32)
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
br label %111
81: ; preds = %75, %76, %57
%82 = phi i32 [ %71, %76 ], [ %71, %75 ], [ %60, %57 ]
%83 = phi i32 [ %63, %76 ], [ %63, %75 ], [ %58, %57 ]
br i1 %38, label %93, label %84
84: ; preds = %81
%85 = load i32, i32* %5
%86 = mul nsw i32 %85, %59
%87 = load i32, i32* %6
%88 = sdiv i32 %86, %87
%89 = call i64 @pwrite(i32 %82, i8* %53, i32 %85, i32 %88)
%90 = icmp slt i64 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %84
%92 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
br label %111
93: ; preds = %84, %81
br i1 %34, label %102, label %94
94: ; preds = %93
%95 = load i32, i32* %5
%96 = call i32 (...) @random()
%97 = srem i32 %96, %32
%98 = call i32 @pread(i32 %82, i8* %53, i32 %95, i32 %97)
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %94
%101 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0))
br label %111
102: ; preds = %93, %94
%103 = add nuw nsw i32 %59, 1
%104 = load i32, i32* %6
%105 = icmp slt i32 %103, %104
br i1 %105, label %57, label %106
106: ; preds = %102
%107 = icmp sgt i32 %82, 0
br i1 %107, label %108, label %111
108: ; preds = %106
%109 = call i32 @close(i32 %82)
%110 = call i32 @remove(i8* %8)
br label %111
111: ; preds = %17, %106, %108, %100, %91, %79, %73, %14
%112 = phi i32 [ 0, %14 ], [ -1, %73 ], [ %77, %79 ], [ 1, %91 ], [ %98, %100 ], [ 0, %108 ], [ 0, %106 ], [ 0, %17 ]
ret i32 %112
}