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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ is_file(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int check_file(char *file, void *data) {
  int fd;
  char *buf;
  struct stat statbuf;
  int size;
  if (!is_file(file))
    return 0;
  size = 100;
  buf = malloc(size);
  if (buf == NULL) {
    printf("malloc failed\n");
    return 1;
  }
  fd = open(file, O_RDWR);
  if (fd < 0) {
    printf("open %s failed\n", file);
    perror("    ");
    exit(1);
  }
  if (read(fd, buf, size) < 0) {
    printf("read %s failed\n", file);
    perror("    ");
    exit(1);
  }
  if (close(fd) < 0) {
    printf("close %s failed\n", file);
    perror("    ");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <is_file@plt>
cmp    $0x0,%eax
jne    11ae <check_file+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    12af <check_file+0x12f>
movl   $0x64,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e6 <check_file+0x66>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <check_file+0x12f>
mov    -0x10(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122d <check_file+0xad>
mov    -0x10(%rbp),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    126e <check_file+0xee>
mov    -0x10(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    12a8 <check_file+0x128>
mov    -0x10(%rbp),%rsi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1080 <is_file@plt>
xor    %r15d,%r15d
test   %eax,%eax
je     120a <check_file+0x7a>
mov    $0x64,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f8 <check_file+0x68>
mov    %rax,%rbx
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1218 <check_file+0x88>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1221 <check_file+0x91>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    120a <check_file+0x7a>
lea    0xe2f(%rip),%rdi
jmp    1228 <check_file+0x98>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde1(%rip),%rdi
jmp    1228 <check_file+0x98>
lea    0xded(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdd7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"malloc failed\0A\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"open %s failed\0A\00"
@.str.2 = [5 x i8] c" \00"
@.str.3 = [16 x i8] c"read %s failed\0A\00"
@.str.4 = [17 x i8] c"close %s failed\0A\00"
define i32 @check_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32 @is_file(i8* %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 0, i32* %3
br label %53
14: ; preds = %2
store i32 100, i32* %9
%15 = load i32, i32* %9
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %53
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i32, i32* %6
%34 = load i8*, i8** %7
%35 = load i32, i32* %9
%36 = call i64 @read(i32 %33, i8* %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %32
%39 = load i8*, i8** %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = load i32, i32* %6
%45 = call i64 @close(i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i8*, i8** %4
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %43
store i32 0, i32* %3
br label %53
53: ; preds = %52, %19, %13
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"open %s failed\0A\00"
@.str.2 = [5 x i8] c" \00"
@.str.3 = [16 x i8] c"read %s failed\0A\00"
@.str.4 = [17 x i8] c"close %s failed\0A\00"
@str = [14 x i8] c"malloc failed\00"
define i32 @check_file(i8* %0, i8* nocapture readnone %1) {
%3 = tail call i32 @is_file(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %32, label %5
5: ; preds = %2
%6 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %32
10: ; preds = %5
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = tail call i64 @read(i32 %12, i8* %6, i32 100)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = tail call i64 @close(i32 %12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %25, %2, %8
%33 = phi i32 [ 1, %8 ], [ 0, %2 ], [ 0, %25 ]
ret i32 %33
}