#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 tracecmd_input {
  int dummy;
};

/* Variables and functions */
scalar_t__ __do_write_check(int, char *, scalar_t__);
int /*<<< orphan*/ free(char *);
int read_copy_data(struct tracecmd_input *, unsigned long long, int);
int read_copy_size4(struct tracecmd_input *, int);
unsigned long long read_copy_size8(struct tracecmd_input *, int);
char *read_string(struct tracecmd_input *);
scalar_t__ strlen(char *);
int copy_event_files(struct tracecmd_input *handle, int fd) {
  unsigned long long size;
  char *system;
  int systems;
  int count;
  int ret;
  int i, x;
  systems = read_copy_size4(handle, fd);
  if (systems < 0)
    return -1;
  for (i = 0; i < systems; i++) {
    system = read_string(handle);
    if (!system)
      return -1;
    if (__do_write_check(fd, system, strlen(system) + 1)) {
      free(system);
      return -1;
    }
    free(system);
    count = read_copy_size4(handle, fd);
    if (count < 0)
      return -1;
    for (x = 0; x < count; x++) {
      size = read_copy_size8(handle, fd);
      if (size < 0)
        return -1;
      ret = read_copy_data(handle, size, fd);
      if (ret < 0)
        return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_event_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <read_copy_size4@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a4 <copy_event_files+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <copy_event_files+0x172>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12db <copy_event_files+0x16b>
mov    -0x10(%rbp),%rdi
call   1090 <read_string@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11db <copy_event_files+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <copy_event_files+0x172>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1060 <__do_write_check@plt>
cmp    $0x0,%rax
je     1224 <copy_event_files+0xb4>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <copy_event_files+0x172>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <read_copy_size4@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1252 <copy_event_files+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <copy_event_files+0x172>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    12c8 <copy_event_files+0x158>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <read_copy_size8@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    128c <copy_event_files+0x11c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <copy_event_files+0x172>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read_copy_data@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12b5 <copy_event_files+0x145>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <copy_event_files+0x172>
jmp    12ba <copy_event_files+0x14a>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1259 <copy_event_files+0xe9>
jmp    12cd <copy_event_files+0x15d>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ab <copy_event_files+0x3b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_event_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%rbx
call   1070 <read_copy_size4@plt>
mov    %eax,%r15d
mov    $0xffffffff,%eax
test   %r15d,%r15d
js     1240 <copy_event_files+0xd0>
je     123e <copy_event_files+0xce>
xor    %r14d,%r14d
jmp    11bd <copy_event_files+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r14d
cmp    %r15d,%r14d
je     123e <copy_event_files+0xce>
mov    %rbx,%rdi
call   1090 <read_string@plt>
test   %rax,%rax
je     1237 <copy_event_files+0xc7>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <__do_write_check@plt>
mov    %rax,%r13
mov    %rbp,%rdi
call   1030 <free@plt>
test   %r13,%r13
jne    1237 <copy_event_files+0xc7>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1070 <read_copy_size4@plt>
test   %eax,%eax
js     1237 <copy_event_files+0xc7>
mov    %eax,%ebp
je     11b0 <copy_event_files+0x40>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <read_copy_size8@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r12d,%edx
call   1050 <read_copy_data@plt>
test   %eax,%eax
js     1237 <copy_event_files+0xc7>
add    $0xffffffff,%ebp
jne    1210 <copy_event_files+0xa0>
jmp    11b0 <copy_event_files+0x40>
mov    $0xffffffff,%eax
jmp    1240 <copy_event_files+0xd0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tracecmd_input = type { i32 }
define i32 @copy_event_files(%struct.tracecmd_input* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.tracecmd_input*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.tracecmd_input* %0, %struct.tracecmd_input** %4
store i32 %1, i32* %5
%13 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%14 = load i32, i32* %5
%15 = call i32 @read_copy_size4(%struct.tracecmd_input* %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %79
19: ; preds = %2
store i32 0, i32* %11
br label %20
20: ; preds = %75, %19
%21 = load i32, i32* %11
%22 = load i32, i32* %8
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %78
24: ; preds = %20
%25 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%26 = call i8* @read_string(%struct.tracecmd_input* %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %24
store i32 -1, i32* %3
br label %79
30: ; preds = %24
%31 = load i32, i32* %5
%32 = load i8*, i8** %7
%33 = load i8*, i8** %7
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = call i64 @__do_write_check(i32 %31, i8* %32, i64 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i8*, i8** %7
%40 = call i32 @free(i8* %39)
store i32 -1, i32* %3
br label %79
41: ; preds = %30
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
%44 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%45 = load i32, i32* %5
%46 = call i32 @read_copy_size4(%struct.tracecmd_input* %44, i32 %45)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %41
store i32 -1, i32* %3
br label %79
50: ; preds = %41
store i32 0, i32* %12
br label %51
51: ; preds = %71, %50
%52 = load i32, i32* %12
%53 = load i32, i32* %9
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %74
55: ; preds = %51
%56 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%57 = load i32, i32* %5
%58 = call i64 @read_copy_size8(%struct.tracecmd_input* %56, i32 %57)
store i64 %58, i64* %6
%59 = load i64, i64* %6
%60 = icmp ult i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
store i32 -1, i32* %3
br label %79
62: ; preds = %55
%63 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%64 = load i64, i64* %6
%65 = load i32, i32* %5
%66 = call i32 @read_copy_data(%struct.tracecmd_input* %63, i64 %64, i32 %65)
store i32 %66, i32* %10
%67 = load i32, i32* %10
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %62
store i32 -1, i32* %3
br label %79
70: ; preds = %62
br label %71
71: ; preds = %70
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 1
store i32 %73, i32* %12
br label %51
74: ; preds = %51
br label %75
75: ; preds = %74
%76 = load i32, i32* %11
%77 = add nsw i32 %76, 1
store i32 %77, i32* %11
br label %20
78: ; preds = %20
store i32 0, i32* %3
br label %79
79: ; preds = %78, %69, %61, %49, %38, %29, %18
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.tracecmd_input = type { i32 }
define i32 @copy_event_files(%struct.tracecmd_input* %0, i32 %1) {
%3 = tail call i32 @read_copy_size4(%struct.tracecmd_input* %0, i32 %1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %33, label %5
5: ; preds = %2
%6 = icmp eq i32 %3, 0
br i1 %6, label %33, label %7
7: ; preds = %5, %30
%8 = phi i32 [ %31, %30 ], [ 0, %5 ]
%9 = tail call i8* @read_string(%struct.tracecmd_input* %0)
%10 = icmp eq i8* %9, null
br i1 %10, label %33, label %11
11: ; preds = %7
%12 = tail call i64 @strlen(i8* %9)
%13 = add nsw i64 %12, 1
%14 = tail call i64 @__do_write_check(i32 %1, i8* %9, i64 %13)
%15 = icmp eq i64 %14, 0
%16 = tail call i32 @free(i8* %9)
br i1 %15, label %17, label %33
17: ; preds = %11
%18 = tail call i32 @read_copy_size4(%struct.tracecmd_input* %0, i32 %1)
%19 = icmp slt i32 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %17
%21 = icmp eq i32 %18, 0
br i1 %21, label %30, label %24
22: ; preds = %24
%23 = icmp eq i32 %29, %18
br i1 %23, label %30, label %24
24: ; preds = %20, %22
%25 = phi i32 [ %29, %22 ], [ 0, %20 ]
%26 = tail call i64 @read_copy_size8(%struct.tracecmd_input* %0, i32 %1)
%27 = tail call i32 @read_copy_data(%struct.tracecmd_input* %0, i64 %26, i32 %1)
%28 = icmp slt i32 %27, 0
%29 = add nuw nsw i32 %25, 1
br i1 %28, label %33, label %22
30: ; preds = %22, %20
%31 = add nuw nsw i32 %8, 1
%32 = icmp eq i32 %31, %3
br i1 %32, label %33, label %7
33: ; preds = %7, %17, %30, %11, %24, %5, %2
%34 = phi i32 [ -1, %2 ], [ 0, %5 ], [ -1, %24 ], [ -1, %7 ], [ -1, %17 ], [ 0, %30 ], [ -1, %11 ]
ret i32 %34
}