#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*/ do_log(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ irlpdev_open();
int read_irlpdev(unsigned char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int write_irlpdev(unsigned char *, int);
int portctl(char mask, int pin, char *name) {
  unsigned char out;
  unsigned char c[2];
  char str[255];
  sprintf(str, "Doing: %s", name);
  do_log(str);
  if (irlpdev_open() < 0) {
    fprintf(stderr, "Can't open parallel port");
    return pin;
  }
  if (read_irlpdev(c, 2) != 2)
    return pin;
  if (pin == 1)
    out = (c[1] | mask);
  if (pin == 0)
    out = (c[1] & ~mask);
  if (write_irlpdev(&out, 1) != 1)
    return pin;
  return pin;
}


x86 assembly (clang, -O0):
<portctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe76(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1060 <do_log@plt>
mov    $0x0,%al
call   1030 <irlpdev_open@plt>
cmp    $0x0,%rax
jge    11cc <portctl+0x6c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <portctl+0xe7>
lea    -0x1b(%rbp),%rdi
mov    $0x2,%esi
call   1040 <read_irlpdev@plt>
cmp    $0x2,%eax
je     11ee <portctl+0x8e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <portctl+0xe7>
cmpl   $0x1,-0xc(%rbp)
jne    1205 <portctl+0xa5>
movzbl -0x1a(%rbp),%eax
movsbl -0x5(%rbp),%ecx
or     %ecx,%eax
mov    %al,-0x19(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    121f <portctl+0xbf>
movzbl -0x1a(%rbp),%eax
movsbl -0x5(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %al,-0x19(%rbp)
lea    -0x19(%rbp),%rdi
mov    $0x1,%esi
call   1050 <write_irlpdev@plt>
cmp    $0x1,%eax
je     1241 <portctl+0xe1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <portctl+0xe7>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portctl>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
lea    0xe89(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1060 <do_log@plt>
xor    %eax,%eax
call   1030 <irlpdev_open@plt>
test   %rax,%rax
js     11bc <portctl+0x5c>
lea    0xe(%rsp),%rdi
mov    $0x2,%esi
call   1040 <read_irlpdev@plt>
cmp    $0x2,%eax
jne    11ef <portctl+0x8f>
test   %ebx,%ebx
je     11d3 <portctl+0x73>
cmp    $0x1,%ebx
jne    11e0 <portctl+0x80>
or     0xf(%rsp),%r14b
jmp    11db <portctl+0x7b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11ef <portctl+0x8f>
not    %r14b
and    0xf(%rsp),%r14b
mov    %r14b,0xd(%rsp)
lea    0xd(%rsp),%rdi
mov    $0x1,%esi
call   1050 <write_irlpdev@plt>
mov    %ebx,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Doing: %s\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Can't open parallel port\00"
define i32 @portctl(i8 signext %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca [2 x i8]
%10 = alloca [255 x i8]
store i8 %0, i8* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%12 = load i8*, i8** %7
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%15 = call i32 @do_log(i8* %14)
%16 = call i64 (...) @irlpdev_open()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* %6
store i32 %21, i32* %4
br label %58
22: ; preds = %3
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%24 = call i32 @read_irlpdev(i8* %23, i32 2)
%25 = icmp ne i32 %24, 2
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = load i32, i32* %6
store i32 %27, i32* %4
br label %58
28: ; preds = %22
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 1
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = load i8, i8* %5
%36 = sext i8 %35 to i32
%37 = or i32 %34, %36
%38 = trunc i32 %37 to i8
store i8 %38, i8* %8
br label %39
39: ; preds = %31, %28
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 1
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = load i8, i8* %5
%47 = sext i8 %46 to i32
%48 = xor i32 %47, -1
%49 = and i32 %45, %48
%50 = trunc i32 %49 to i8
store i8 %50, i8* %8
br label %51
51: ; preds = %42, %39
%52 = call i32 @write_irlpdev(i8* %8, i32 1)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* %6
store i32 %55, i32* %4
br label %58
56: ; preds = %51
%57 = load i32, i32* %6
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %54, %26, %18
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [10 x i8] c"Doing: %s\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Can't open parallel port\00"
define i32 @portctl(i8 signext %0, i32 returned %1, i8* %2) {
%4 = alloca i8
%5 = alloca [2 x i8]
%6 = alloca [255 x i8]
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %2)
%10 = call i32 @do_log(i8* %8)
%11 = call i64 (...) @irlpdev_open()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %31
16: ; preds = %3
%17 = call i32 @read_irlpdev(i8* %7, i32 2)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %31
19: ; preds = %16
switch i32 %1, label %29 [
i32 1, label %20
i32 0, label %24
]
20: ; preds = %19
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%22 = load i8, i8* %21
%23 = or i8 %22, %0
store i8 %23, i8* %4
br label %29
24: ; preds = %19
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%26 = load i8, i8* %25
%27 = xor i8 %0, -1
%28 = and i8 %26, %27
store i8 %28, i8* %4
br label %29
29: ; preds = %20, %19, %24
%30 = call i32 @write_irlpdev(i8* %4, i32 1)
br label %31
31: ; preds = %29, %16, %13
ret i32 %1
}