#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 getDebug();
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int open_port(void) {
  int fd;
  if (getDebug() > 1)
    printf("I: Open port: ");
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
    perror("E: open_port: Unable to open /dev/ttyS0 - ");
  else
    fcntl(fd, F_SETFL, 0);
  if (getDebug() > 1)
    puts("OK");
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <getDebug@plt>
cmp    $0x1,%eax
jle    1184 <open_port+0x24>
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
lea    0xe69(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11c9 <open_port+0x69>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
jmp    11dc <open_port+0x7c>
mov    -0x4(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    $0x0,%al
call   1060 <getDebug@plt>
cmp    $0x1,%eax
jle    11f8 <open_port+0x98>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbx
xor    %eax,%eax
call   1060 <getDebug@plt>
cmp    $0x2,%eax
jl     1179 <open_port+0x19>
lea    0xe8c(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e68(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11d5 <open_port+0x75>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
xor    %eax,%eax
call   1060 <getDebug@plt>
cmp    $0x2,%eax
jl     11d1 <open_port+0x71>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe3e(%rip),%rdi
call   1080 <perror@plt>
jmp    11b9 <open_port+0x59>

LLVM IR (-O0):
@.str = [15 x i8] c"I: Open port: \00"
@.str.1 = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [43 x i8] c"E: open_port: Unable to open /dev/ttyS0 - \00"
@F_SETFL = global i32 0
@.str.3 = [3 x i8] c"OK\00"
define i32 @open_port() {
%1 = alloca i32
%2 = call i32 (...) @getDebug()
%3 = icmp sgt i32 %2, 1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NDELAY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %1
%13 = load i32, i32* %1
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %21
17: ; preds = %6
%18 = load i32, i32* %1
%19 = load i32, i32* @F_SETFL
%20 = call i32 @fcntl(i32 %18, i32 %19, i32 0)
br label %21
21: ; preds = %17, %15
%22 = call i32 (...) @getDebug()
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [15 x i8] c"I: Open port: \00"
@.str.1 = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [43 x i8] c"E: open_port: Unable to open /dev/ttyS0 - \00"
@F_SETFL = global i32 0
@.str.3 = [3 x i8] c"OK\00"
define i32 @open_port() {
%1 = tail call i32 (...) @getDebug()
%2 = icmp sgt i32 %1, 1
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %18
15: ; preds = %5
%16 = load i32, i32* @F_SETFL
%17 = tail call i32 @fcntl(i32 %11, i32 %16, i32 0)
br label %18
18: ; preds = %15, %13
%19 = tail call i32 (...) @getDebug()
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret i32 %11
}