#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char const *);
void openf(const char *p, int tfd, int mode) {
  int fd;
  if (p == NULL)
    return;
  fd = open(p, mode, 0666);
  if (fd == -1) {
    perror(p);
    exit(1);
  }
  dup2(fd, tfd);
  close(fd);
}


x86 assembly (clang, -O0):
<openf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1172 <openf+0x22>
jmp    11b6 <openf+0x66>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a3 <openf+0x53>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openf>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1187 <openf+0x37>
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %edx,%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     118c <openf+0x3c>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14d,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
define void @openf(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %3
br label %27
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = load i32, i32* %6
%14 = call i32 @open(i8* %12, i32 %13, i32 438)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = call i32 @perror(i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = call i32 @dup2(i32 %22, i32 %23)
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %21, %10
ret void
}

LLVM IR (-O2):
define void @openf(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = tail call i32 @open(i8* %0, i32 %2, i32 438)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @perror(i8* %0)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %5
%12 = tail call i32 @dup2(i32 %6, i32 %1)
%13 = tail call i32 @close(i32 %6)
br label %14
14: ; preds = %3, %11
ret void
}