#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*/ B9600;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ set_blocking(int, int /*<<< orphan*/);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
char *strerror(int);
int open_port(void) {
  char portname[20];
  int fd;
  printf("\n");
  printf("Portname of selected device: ");
  fflush(stdout);
  scanf("%s", portname);
  fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    printf("error %d opening %s: %s\n", errno, portname, strerror(errno));
    return fd;
  }
  set_interface_attribs(fd, B9600, 0);
  set_blocking(fd, 0);
  return fd;
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    -0x20(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1228 <open_port+0xb8>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <open_port+0xdb>
mov    -0x24(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <set_interface_attribs@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1050 <set_blocking@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xe6b(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    0x2e23(%rip),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1204 <open_port+0x84>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <set_interface_attribs@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <set_blocking@plt>
jmp    122a <open_port+0xaa>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xe06(%rip),%rdi
mov    %rsp,%rdx
mov    %ebp,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [30 x i8] c"Portname of selected device: \00"
@stdout = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.3 = [25 x i8] c"error %d opening %s: %s\0A\00"
@errno = global i32 0
@B9600 = global i32 0
define i32 @open_port() {
%1 = alloca i32
%2 = alloca [20 x i8]
%3 = alloca i32
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @stdout
%7 = call i32 @fflush(i32 %6)
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_SYNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %0
%20 = load i32, i32* @errno
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %20, i8* %21, i8* %23)
%25 = load i32, i32* %3
store i32 %25, i32* %1
br label %33
26: ; preds = %0
%27 = load i32, i32* %3
%28 = load i32, i32* @B9600
%29 = call i32 @set_interface_attribs(i32 %27, i32 %28, i32 0)
%30 = load i32, i32* %3
%31 = call i32 @set_blocking(i32 %30, i32 0)
%32 = load i32, i32* %3
store i32 %32, i32* %1
br label %33
33: ; preds = %26, %19
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str.1 = [30 x i8] c"Portname of selected device: \00"
@stdout = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.3 = [25 x i8] c"error %d opening %s: %s\0A\00"
@errno = global i32 0
@B9600 = global i32 0
define i32 @open_port() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @putchar(i32 10)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = load i32, i32* @O_SYNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %2, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %16, i8* %2, i8* %17)
br label %23
19: ; preds = %0
%20 = load i32, i32* @B9600
%21 = call i32 @set_interface_attribs(i32 %13, i32 %20, i32 0)
%22 = call i32 @set_blocking(i32 %13, i32 0)
br label %23
23: ; preds = %19, %15
ret i32 %13
}