#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*/ B19200;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int /*<<< orphan*/ set_blocking(int, int /*<<< orphan*/);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/,
                                         int /*<<< orphan*/);
char *strerror(int);
int /*<<< orphan*/ write(int, char *, int);
int init_driver(int id, int buffer_size) {
  puts("init_driver ");
  char *portname = "/dev/ttyUSB0";
  int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    printf("error %d opening %s: %s", errno, portname, strerror(errno));
    return 0;
  }
  set_interface_attribs(fd, B19200, 0);
  set_blocking(fd, 0);
  write(fd, "ATI\r\n", 5);
  char buf[100];
  int n = read(fd, buf, sizeof buf);
  printf("%s | ", buf);
  return 0;
}


x86 assembly (clang, -O0):
<init_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
lea    0xe69(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122b <init_driver+0xab>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x94(%rbp),%esi
mov    -0x90(%rbp),%rdx
mov    %rax,%rcx
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128c <init_driver+0x10c>
mov    -0x1c(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <set_interface_attribs@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1070 <set_blocking@plt>
mov    -0x1c(%rbp),%edi
lea    0xde0(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
mov    %eax,-0x84(%rbp)
lea    -0x80(%rbp),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_driver>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e46(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2c(%rip),%rax
or     (%rax),%esi
lea    0xe58(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1213 <init_driver+0x93>
mov    %eax,%ebx
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <set_interface_attribs@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <set_blocking@plt>
lea    0xe50(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    0xe31(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    123d <init_driver+0xbd>
mov    0x2da6(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xdf0(%rip),%rdi
lea    0xddc(%rip),%rdx
mov    %ebx,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"init_driver \00"
@.str.1 = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.2 = [24 x i8] c"error %d opening %s: %s\00"
@errno = global i32 0
@B19200 = global i32 0
@.str.3 = [6 x i8] c"ATI\0D\0A\00"
@.str.4 = [6 x i8] c"%s | \00"
define i32 @init_driver(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %6
%11 = load i8*, i8** %6
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_NOCTTY
%14 = or i32 %12, %13
%15 = load i32, i32* @O_SYNC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i32, i32* @errno
%22 = load i8*, i8** %6
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %21, i8* %22, i8* %24)
store i32 0, i32* %3
br label %39
26: ; preds = %2
%27 = load i32, i32* %7
%28 = load i32, i32* @B19200
%29 = call i32 @set_interface_attribs(i32 %27, i32 %28, i32 0)
%30 = load i32, i32* %7
%31 = call i32 @set_blocking(i32 %30, i32 0)
%32 = load i32, i32* %7
%33 = call i32 @write(i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%34 = load i32, i32* %7
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 100)
store i32 %36, i32* %9
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %37)
store i32 0, i32* %3
br label %39
39: ; preds = %26, %20
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [13 x i8] c"init_driver \00"
@.str.1 = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.2 = [24 x i8] c"error %d opening %s: %s\00"
@errno = global i32 0
@B19200 = global i32 0
@.str.3 = [6 x i8] c"ATI\0D\0A\00"
@.str.4 = [6 x i8] c"%s | \00"
define i32 @init_driver(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %24
16: ; preds = %2
%17 = load i32, i32* @B19200
%18 = tail call i32 @set_interface_attribs(i32 %10, i32 %17, i32 0)
%19 = tail call i32 @set_blocking(i32 %10, i32 0)
%20 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%22 = call i32 @read(i32 %10, i8* %21, i32 100)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %24
24: ; preds = %16, %12
ret i32 0
}