#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*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int strlen(char *);
int open_port(void) {
  char dir[256];
  int fd;
  printf("Type the port path:");
  fgets(dir, 256, stdin);
  dir[strlen(dir) - 1] = '\0';
  fd = open(dir, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open");
    printf("Path introduced:%s\n", dir);
  } else
    fcntl(fd, F_SETFL, 0);
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x100(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1060 <fgets@plt>
lea    -0x100(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x100(%rbp,%rax,1)
lea    -0x100(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x104(%rbp)
cmpl   $0xffffffff,-0x104(%rbp)
jne    120c <open_port+0xac>
lea    0xe27(%rip),%rdi
call   1080 <perror@plt>
lea    -0x100(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1222 <open_port+0xc2>
mov    -0x104(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x104(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbx
sub    $0x100,%rsp
lea    0xe91(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2e4a(%rip),%rcx
mov    0x2e2b(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11e5 <open_port+0x85>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
ret
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
lea    0xe36(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11da <open_port+0x7a>

LLVM IR (-O0):
@.str = [20 x i8] c"Type the port path:\00"
@stdin = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [26 x i8] c"open_port: Unable to open\00"
@.str.2 = [20 x i8] c"Path introduced:%s\0A\00"
@F_SETFL = global i32 0
define i32 @open_port() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %4, i32 256, i32 %5)
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%8 = call i32 @strlen(i8* %7)
%9 = sub nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_NDELAY
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %29
25: ; preds = %0
%26 = load i32, i32* %2
%27 = load i32, i32* @F_SETFL
%28 = call i32 @fcntl(i32 %26, i32 %27, i32 0)
br label %29
29: ; preds = %25, %21
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [20 x i8] c"Type the port path:\00"
@stdin = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [26 x i8] c"open_port: Unable to open\00"
@.str.2 = [20 x i8] c"Path introduced:%s\0A\00"
@F_SETFL = global i32 0
define i32 @open_port() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @stdin
%5 = call i32 @fgets(i8* %2, i32 256, i32 %4)
%6 = call i32 @strlen(i8* %2)
%7 = add nsw i32 %6, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %8
store i8 0, i8* %9
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %11, %10
%13 = load i32, i32* @O_NDELAY
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %2, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %23
20: ; preds = %0
%21 = load i32, i32* @F_SETFL
%22 = call i32 @fcntl(i32 %15, i32 %21, i32 0)
br label %23
23: ; preds = %20, %17
ret i32 %15
}