#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 */
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ smp_print_error();
int smp_read_file(char const *, char *, int);
int smp_test_read_file(const char *file) {
  int ret = 0;
  int size = 2048;
  char *buf = malloc(size);
  if (file == NULL || buf == NULL)
    return (-1);
  ret = smp_read_file(file, buf, size);
  if (ret == (-1)) {
    smp_print_error();
    return (-1);
  }
  printf("%s\n", buf);
  printf("read size %d\n", ret);
  return (0);
}


x86 assembly (clang, -O0):
<smp_test_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x800,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117c <smp_test_read_file+0x3c>
cmpq   $0x0,-0x20(%rbp)
jne    1188 <smp_test_read_file+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <smp_test_read_file+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <smp_read_file@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b8 <smp_test_read_file+0x78>
mov    $0x0,%al
call   1030 <smp_print_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <smp_test_read_file+0xa2>
mov    -0x20(%rbp),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smp_test_read_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
mov    $0x800,%edi
call   1060 <malloc@plt>
mov    $0xffffffff,%r14d
test   %rbp,%rbp
je     11af <smp_test_read_file+0x5f>
mov    %rax,%rbx
test   %rax,%rax
je     11af <smp_test_read_file+0x5f>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1070 <smp_read_file@plt>
cmp    $0xffffffff,%eax
je     11a8 <smp_test_read_file+0x58>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xe66(%rip),%rdi
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11af <smp_test_read_file+0x5f>
xor    %eax,%eax
call   1040 <smp_print_error@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [14 x i8] c"read size %d\0A\00"
define i32 @smp_test_read_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 2048, i32* %5
%7 = load i32, i32* %5
%8 = call i8* @malloc(i32 %7)
store i8* %8, i8** %6
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %11, %1
store i32 -1, i32* %2
br label %29
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %5
%19 = call i32 @smp_read_file(i8* %16, i8* %17, i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 (...) @smp_print_error()
store i32 -1, i32* %2
br label %29
24: ; preds = %15
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %4
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 0, i32* %2
br label %29
29: ; preds = %24, %22, %14
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"read size %d\0A\00"
define i32 @smp_test_read_file(i8* %0) {
%2 = tail call dereferenceable_or_null(2048) i8* @malloc(i32 2048)
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %2, null
%5 = or i1 %3, %4
br i1 %5, label %14, label %6
6: ; preds = %1
%7 = tail call i32 @smp_read_file(i8* %0, i8* %2, i32 2048)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 (...) @smp_print_error()
br label %14
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) %2)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %14
14: ; preds = %1, %11, %9
%15 = phi i32 [ -1, %9 ], [ 0, %11 ], [ -1, %1 ]
ret i32 %15
}