#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*/ B4000000;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ configure_tty(int, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ switch_to_binary(int);
int samba_open(const char *device) {
  int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    perror("Could not open device");
    return -1;
  }
  if (!configure_tty(fd, B4000000)) {
    close(fd);
    return -1;
  }
  if (!switch_to_binary(fd)) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<samba_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a5 <samba_open+0x55>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <samba_open+0xae>
mov    -0x14(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1040 <configure_tty@plt>
cmp    $0x0,%eax
jne    11d3 <samba_open+0x83>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <samba_open+0xae>
mov    -0x14(%rbp),%edi
call   1070 <switch_to_binary@plt>
cmp    $0x0,%eax
jne    11f8 <samba_open+0xa8>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <samba_open+0xae>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<samba_open>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11a6 <samba_open+0x56>
mov    %eax,%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <configure_tty@plt>
test   %eax,%eax
je     1196 <samba_open+0x46>
mov    %ebx,%edi
call   1070 <switch_to_binary@plt>
test   %eax,%eax
jne    11a2 <samba_open+0x52>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
jmp    119d <samba_open+0x4d>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str = [22 x i8] c"Could not open device\00"
@B4000000 = global i32 0
define i32 @samba_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
16: ; preds = %1
%17 = load i32, i32* %4
%18 = load i32, i32* @B4000000
%19 = call i32 @configure_tty(i32 %17, i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %2
br label %33
24: ; preds = %16
%25 = load i32, i32* %4
%26 = call i32 @switch_to_binary(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %33
31: ; preds = %24
%32 = load i32, i32* %4
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %28, %21, %14
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str = [22 x i8] c"Could not open device\00"
@B4000000 = global i32 0
define i32 @samba_open(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_SYNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %22
11: ; preds = %1
%12 = load i32, i32* @B4000000
%13 = tail call i32 @configure_tty(i32 %7, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @close(i32 %7)
br label %22
17: ; preds = %11
%18 = tail call i32 @switch_to_binary(i32 %7)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @close(i32 %7)
br label %22
22: ; preds = %17, %20, %15, %9
%23 = phi i32 [ -1, %9 ], [ -1, %20 ], [ -1, %15 ], [ %7, %17 ]
ret i32 %23
}