#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 */
struct termios {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
scalar_t__ cfsetispeed(struct termios *, int);
scalar_t__ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ makeraw(struct termios *);
int /*<<< orphan*/ stderr;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void initSerialPort(int handle, int baud) {
  struct termios ios;
  if (tcgetattr(handle, &ios) < 0) {
    fprintf(stderr, "Can't get terminal attributes.\n");
    exit(1);
  }
  if (cfsetispeed(&ios, baud) < 0) {
    fprintf(stderr, "Can't set input baud rate.\n");
    exit(1);
  }
  if (cfsetospeed(&ios, baud) < 0) {
    fprintf(stderr, "Can't set output baud rate.\n");
    exit(1);
  }
  makeraw(&ios);
  if (tcsetattr(handle, TCSANOW, &ios) < 0) {
    fprintf(stderr, "Can't set port attributes.\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<initSerialPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11b3 <initSerialPort+0x43>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1080 <cfsetispeed@plt>
cmp    $0x0,%rax
jge    11e8 <initSerialPort+0x78>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
jge    121d <initSerialPort+0xad>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
call   1040 <makeraw@plt>
mov    -0x4(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1264 <initSerialPort+0xf4>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initSerialPort>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
js     11cc <initSerialPort+0x5c>
mov    %rsp,%rdi
mov    %ebp,%esi
call   1080 <cfsetispeed@plt>
test   %rax,%rax
js     11de <initSerialPort+0x6e>
mov    %rsp,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
js     11f0 <initSerialPort+0x80>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1040 <makeraw@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
js     1202 <initSerialPort+0x92>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
jmp    1212 <initSerialPort+0xa2>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
jmp    1212 <initSerialPort+0xa2>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
jmp    1212 <initSerialPort+0xa2>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Can't get terminal attributes.\0A\00"
@.str.1 = [28 x i8] c"Can't set input baud rate.\0A\00"
@.str.2 = [29 x i8] c"Can't set output baud rate.\0A\00"
@TCSANOW = global i32 0
@.str.3 = [28 x i8] c"Can't set port attributes.\0A\00"
define void @initSerialPort(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = call i64 @tcgetattr(i32 %6, %struct.termios* %5)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* %4
%15 = call i64 @cfsetispeed(%struct.termios* %5, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = load i32, i32* %4
%23 = call i64 @cfsetospeed(%struct.termios* %5, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i32 @makeraw(%struct.termios* %5)
%31 = load i32, i32* %3
%32 = load i32, i32* @TCSANOW
%33 = call i64 @tcsetattr(i32 %31, i32 %32, %struct.termios* %5)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %29
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Can't get terminal attributes.\0A\00"
@.str.1 = [28 x i8] c"Can't set input baud rate.\0A\00"
@.str.2 = [29 x i8] c"Can't set output baud rate.\0A\00"
@TCSANOW = global i32 0
@.str.3 = [28 x i8] c"Can't set port attributes.\0A\00"
define void @initSerialPort(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = call i64 @cfsetispeed(%struct.termios* %3, i32 %1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i64 @cfsetospeed(%struct.termios* %3, i32 %1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = call i32 @makeraw(%struct.termios* %3)
%27 = load i32, i32* @TCSANOW
%28 = call i64 @tcsetattr(i32 %0, i32 %27, %struct.termios* %3)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %25
ret void
}