#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ B57600;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ *fdopen(int, char *);
int open(char const *, int /*<<< orphan*/);
int serial_conf(int, int /*<<< orphan*/);
FILE *term_open(char const *fname) {
  int sfd = open(fname, O_RDWR);
  if (sfd < 0) {
    return NULL;
  }
  int ret = serial_conf(sfd, B57600);
  if (ret < 0) {
    return NULL;
  }
  FILE *sf = fdopen(sfd, "a+");
  if (!sf) {
    return NULL;
  }
  return sf;
}


x86 assembly (clang, -O0):
<term_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1168 <term_open+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <term_open+0x96>
mov    -0x14(%rbp),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1030 <serial_conf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1193 <term_open+0x63>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <term_open+0x96>
mov    -0x14(%rbp),%edi
lea    0xe63(%rip),%rsi
call   1040 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11be <term_open+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <term_open+0x96>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_open>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1168 <term_open+0x38>
mov    %eax,%ebx
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <serial_conf@plt>
test   %eax,%eax
js     1168 <term_open+0x38>
lea    0xea0(%rip),%rsi
mov    %ebx,%edi
pop    %rbx
jmp    1040 <fdopen@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@B57600 = global i32 0
@.str = [3 x i8] c"a+\00"
define i32* @term_open(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32* null, i32** %2
br label %28
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @B57600
%16 = call i32 @serial_conf(i32 %14, i32 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
store i32* null, i32** %2
br label %28
20: ; preds = %13
%21 = load i32, i32* %4
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
store i32* null, i32** %2
br label %28
26: ; preds = %20
%27 = load i32*, i32** %6
store i32* %27, i32** %2
br label %28
28: ; preds = %26, %25, %19, %12
%29 = load i32*, i32** %2
ret i32* %29
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@B57600 = global i32 0
@.str = [3 x i8] c"a+\00"
define noalias i32* @term_open(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = load i32, i32* @B57600
%7 = tail call i32 @serial_conf(i32 %3, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32* @fdopen(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5, %1
%12 = phi i32* [ null, %1 ], [ %10, %9 ], [ null, %5 ]
ret i32* %12
}