#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_RDONLY;
int cat_fdio(int, int);
scalar_t__ close(int);
int open(char const *, int /*<<< orphan*/);
int catio(const char *fname, int fd_out) {
  int fd_in, success;
  if ((fd_in = open(fname, O_RDONLY)) == -1)
    return -1;
  success = cat_fdio(fd_in, fd_out);
  if (close(fd_in) != 0)
    return -1;
  return success;
}


x86 assembly (clang, -O0):
<catio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1169 <catio+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    119b <catio+0x6b>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <cat_fdio@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     1195 <catio+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    119b <catio+0x6b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catio>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1165 <catio+0x35>
mov    %eax,%ebp
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <cat_fdio@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
jmp    116a <catio+0x3a>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @catio(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %23
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = call i32 @cat_fdio(i32 %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %6
%18 = call i64 @close(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -1, i32* %3
br label %23
21: ; preds = %13
%22 = load i32, i32* %7
store i32 %22, i32* %3
br label %23
23: ; preds = %21, %20, %12
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @catio(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i32 @cat_fdio(i32 %4, i32 %1)
%8 = tail call i64 @close(i32 %4)
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i32 %7, i32 -1
br label %11
11: ; preds = %6, %2
%12 = phi i32 [ -1, %2 ], [ %10, %6 ]
ret i32 %12
}