#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_RDWR;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int open_port(int usbPortNum) {
  int fd;
  char device[30];
  sprintf(device, "/dev/ttyUSB%d", usbPortNum);
  fd = open(device, O_RDWR | O_NDELAY);
  if (fd == -1) {
    char errString[30];
    sprintf(errString, "open_port: Unable to open %s", device);
    perror(errString);
  } else {
    fcntl(fd, F_SETFL, 0);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xea7(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ac <open_port+0x6c>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <perror@plt>
jmp    11bf <open_port+0x7f>
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%edx
lea    0xeb0(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    0x2e84(%rip),%rax
mov    0x2e65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     119a <open_port+0x5a>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6d(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r14,%rdi
call   1050 <perror@plt>
jmp    1190 <open_port+0x50>

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

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/ttyUSB%d\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [29 x i8] c"open_port: Unable to open %s\00"
@F_SETFL = global i32 0
define i32 @open_port(i32 %0) {
%2 = alloca [30 x i8]
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%5 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NDELAY
%8 = or i32 %7, %6
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%14 = call i32 @perror(i8* %12)
br label %18
15: ; preds = %1
%16 = load i32, i32* @F_SETFL
%17 = tail call i32 @fcntl(i32 %9, i32 %16, i32 0)
br label %18
18: ; preds = %15, %11
ret i32 %9
}