#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char **, int);
int main(int argc, char *argv[]) {
  int input_fd;
  ssize_t ret_in;
  char buffer[20];
  input_fd = open("/dev/stm32_usb", O_RDONLY);
  if (input_fd == -1) {
    printf("Could not open it!\n");
    return 2;
  }
  ret_in = read(input_fd, &buffer, 20);
  if (ret_in < 0) {
    printf("Error with reading.\n");
  } else {
    printf("%d bytes read.\n", (int)ret_in);
  }
  if (close(input_fd) < 0) {
    printf("Problem with closing.\n");
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1192 <main+0x52>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1205 <main+0xc5>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c5 <main+0x85>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d9 <main+0x99>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    11f9 <main+0xb9>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1197 <main+0x47>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1060 <read@plt>
test   %rax,%rax
js     11aa <main+0x5a>
lea    0xe83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b6 <main+0x66>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11d7 <main+0x87>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    11ce <main+0x7e>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/stm32_usb\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Could not open it!\0A\00"
@.str.2 = [21 x i8] c"Error with reading.\0A\00"
@.str.3 = [16 x i8] c"%d bytes read.\0A\00"
@.str.4 = [23 x i8] c"Problem with closing.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %35
15: ; preds = %2
%16 = load i32, i32* %6
%17 = bitcast [20 x i8]* %8 to i8**
%18 = call i64 @read(i32 %16, i8** %17, i32 20)
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %27
23: ; preds = %15
%24 = load i64, i64* %7
%25 = trunc i64 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %21
%28 = load i32, i32* %6
%29 = call i64 @close(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = load i32, i32* @EXIT_SUCCESS
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %13
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/stm32_usb\00"
@O_RDONLY = global i32 0
@.str.3 = [16 x i8] c"%d bytes read.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [22 x i8] c"Problem with closing.\00"
@str.5 = [20 x i8] c"Error with reading.\00"
@str.6 = [19 x i8] c"Could not open it!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %26
10: ; preds = %2
%11 = bitcast [20 x i8]* %3 to i8**
%12 = call i64 @read(i32 %6, i8** %11, i32 20)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %19
16: ; preds = %10
%17 = trunc i64 %12 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %14
%20 = tail call i64 @close(i32 %6)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* @EXIT_SUCCESS
br label %26
26: ; preds = %24, %8
%27 = phi i32 [ 2, %8 ], [ %25, %24 ]
ret i32 %27
}