#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 *, unsigned long long);
scalar_t__ do_read_check(struct tracecmd_input *, char *, unsigned long long);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned long long);
int read_copy_data(struct tracecmd_input *handle, unsigned long long size,
                   int fd) {
  char *buf;
  buf = malloc(size);
  if (!buf)
    return -1;
  if (do_read_check(handle, buf, size))
    goto failed_read;
  if (__do_write_check(fd, buf, size))
    goto failed_read;
  free(buf);
  return 0;
failed_read:
  free(buf);
  return -1;
}


x86 assembly (clang, -O0):
<read_copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1177 <read_copy_data+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <read_copy_data+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <do_read_check@plt>
cmp    $0x0,%rax
je     1197 <read_copy_data+0x57>
jmp    11cb <read_copy_data+0x8b>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <__do_write_check@plt>
cmp    $0x0,%rax
je     11b6 <read_copy_data+0x76>
jmp    11cb <read_copy_data+0x8b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <read_copy_data+0x9b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_copy_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1195 <read_copy_data+0x55>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1050 <do_read_check@plt>
test   %rax,%rax
jne    1186 <read_copy_data+0x46>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <__do_write_check@plt>
test   %rax,%rax
je     11a7 <read_copy_data+0x67>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    119a <read_copy_data+0x5a>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    118b <read_copy_data+0x4b>

LLVM IR (-O0):
%struct.tracecmd_input = type { i32 }
define i32 @read_copy_data(%struct.tracecmd_input* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.tracecmd_input*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store %struct.tracecmd_input* %0, %struct.tracecmd_input** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i64, i64* %6
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 -1, i32* %4
br label %34
14: ; preds = %3
%15 = load %struct.tracecmd_input*, %struct.tracecmd_input** %5
%16 = load i8*, i8** %8
%17 = load i64, i64* %6
%18 = call i64 @do_read_check(%struct.tracecmd_input* %15, i8* %16, i64 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
br label %31
21: ; preds = %14
%22 = load i32, i32* %7
%23 = load i8*, i8** %8
%24 = load i64, i64* %6
%25 = call i64 @__do_write_check(i32 %22, i8* %23, i64 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %31
28: ; preds = %21
%29 = load i8*, i8** %8
%30 = call i32 @free(i8* %29)
store i32 0, i32* %4
br label %34
31: ; preds = %27, %20
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
store i32 -1, i32* %4
br label %34
34: ; preds = %31, %28, %13
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.tracecmd_input = type { i32 }
define i32 @read_copy_data(%struct.tracecmd_input* %0, i64 %1, i32 %2) {
%4 = tail call i8* @malloc(i64 %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = tail call i64 @do_read_check(%struct.tracecmd_input* %0, i8* %4, i64 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i64 @__do_write_check(i32 %2, i8* %4, i64 %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %9, %6
br label %13
13: ; preds = %9, %12
%14 = phi i32 [ -1, %12 ], [ 0, %9 ]
%15 = tail call i32 @free(i8* %4)
br label %16
16: ; preds = %13, %3
%17 = phi i32 [ -1, %3 ], [ %14, %13 ]
ret i32 %17
}