#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 c_cflag;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int gps_serial_config(int fd) {
  struct termios options;
  if (tcgetattr(fd, &options) == -1) {
    fprintf(stderr, "%s: %s\n", "Error obtaining serial socket parameters",
            strerror(errno));
    return -1;
  }
  options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  if (tcflush(fd, TCIFLUSH) == -1) {
    fprintf(stderr, "%s: %s\n", "Error flushing serial socket input buffer",
            strerror(errno));
    return -1;
  }
  if (tcsetattr(fd, TCSANOW, &options) == -1) {
    fprintf(stderr, "%s: %s\n", "Error setting serial socket parameters",
            strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gps_serial_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    11af <gps_serial_config+0x5f>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rcx
lea    0xe69(%rip),%rsi
lea    0xe6a(%rip),%rdx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <gps_serial_config+0x15f>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    0x2e11(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd0(%rip),%rcx
or     (%rcx),%eax
mov    0x2e07(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0xffffffff,%eax
jne    124b <gps_serial_config+0xfb>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rcx
lea    0xdcd(%rip),%rsi
lea    0xdf7(%rip),%rdx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <gps_serial_config+0x15f>
mov    -0x8(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    12a8 <gps_serial_config+0x158>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rcx
lea    0xd70(%rip),%rsi
lea    0xdc4(%rip),%rdx
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <gps_serial_config+0x15f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gps_serial_config>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11e0 <gps_serial_config+0x90>
mov    0x2e72(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e18(%rip),%rax
or     (%rax),%ecx
mov    0x2e4f(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
cmp    $0xffffffff,%eax
je     11e9 <gps_serial_config+0x99>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1220 <gps_serial_config+0xd0>
lea    0xe7d(%rip),%rbx
jmp    11f0 <gps_serial_config+0xa0>
lea    0xe21(%rip),%rbx
jmp    11f0 <gps_serial_config+0xa0>
lea    0xe41(%rip),%rbx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ebp
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf2(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [41 x i8] c"Error obtaining serial socket parameters\00"
@errno = global i32 0
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@.str.2 = [42 x i8] c"Error flushing serial socket input buffer\00"
@TCSANOW = global i32 0
@.str.3 = [39 x i8] c"Error setting serial socket parameters\00"
define i32 @gps_serial_config(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
br label %46
13: ; preds = %1
%14 = load i32, i32* @B9600
%15 = load i32, i32* @CS8
%16 = or i32 %14, %15
%17 = load i32, i32* @CLOCAL
%18 = or i32 %16, %17
%19 = load i32, i32* @CREAD
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @IGNPAR
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
store i64 0, i64* %25
%26 = load i32, i32* %3
%27 = load i32, i32* @TCIFLUSH
%28 = call i32 @tcflush(i32 %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %13
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %33)
store i32 -1, i32* %2
br label %46
35: ; preds = %13
%36 = load i32, i32* %3
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %43)
store i32 -1, i32* %2
br label %46
45: ; preds = %35
store i32 0, i32* %2
br label %46
46: ; preds = %45, %40, %30, %8
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [41 x i8] c"Error obtaining serial socket parameters\00"
@errno = global i32 0
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@.str.2 = [42 x i8] c"Error flushing serial socket input buffer\00"
@TCSANOW = global i32 0
@.str.3 = [39 x i8] c"Error setting serial socket parameters\00"
define i32 @gps_serial_config(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = load i32, i32* @B9600
%8 = load i32, i32* @CS8
%9 = or i32 %8, %7
%10 = load i32, i32* @CLOCAL
%11 = or i32 %9, %10
%12 = load i32, i32* @CREAD
%13 = or i32 %11, %12
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @IGNPAR
%16 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%18 = bitcast i64* %17 to i8*
%19 = load i32, i32* @TCIFLUSH
%20 = call i32 @tcflush(i32 %0, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %26, label %22
22: ; preds = %6
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %0, i32 %23, %struct.termios* %2)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %22, %6, %1
%27 = phi i8* [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %22 ]
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %27, i8* %30)
br label %32
32: ; preds = %26, %22
%33 = phi i32 [ 0, %22 ], [ -1, %26 ]
ret i32 %33
}