#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*/ close(int);
int /*<<< orphan*/ ioctl(int, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write_io(char cmd) {
  int fd;
  fd = open("/dev/car0", 0);
  if (fd < 0) {
    fd = open("/dev/car", 0);
  }
  if (fd < 0) {
    perror("open device car");
  }
  ioctl(fd, cmd, 1);
  close(fd);
}


x86 assembly (clang, -O0):
<write_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
lea    0xeab(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    117a <write_io+0x3a>
lea    0xe9a(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1190 <write_io+0x50>
lea    0xe88(%rip),%rdi
call   1060 <perror@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%edx
movsbl -0x5(%rbp),%esi
call   1030 <ioctl@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_io>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xeb4(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    116d <write_io+0x2d>
lea    0xeaa(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1189 <write_io+0x49>
movsbl %bl,%esi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <ioctl@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
jmp    116d <write_io+0x2d>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/car0\00"
@.str.1 = [9 x i8] c"/dev/car\00"
@.str.2 = [16 x i8] c"open device car\00"
define i32 @write_io(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i8 %0, i8* %3
%5 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %9, i32* %4
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* %4
%17 = load i8, i8* %3
%18 = call i32 @ioctl(i32 %16, i8 signext %17, i32 1)
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/car0\00"
@.str.1 = [9 x i8] c"/dev/car\00"
@.str.2 = [16 x i8] c"open device car\00"
define i32 @write_io(i8 signext %0) {
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %1, %7, %4
%10 = phi i32 [ %5, %7 ], [ %5, %4 ], [ %2, %1 ]
%11 = tail call i32 @ioctl(i32 %10, i8 signext %0, i32 1)
%12 = tail call i32 @close(i32 %10)
ret i32 undef
}