#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*/ TIOCMGET;
int /*<<< orphan*/ TIOCMSET;
int TIOCM_DTR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int set_DTR(int fd, int value) {
  int status;
  int result;
  result = ioctl(fd, TIOCMGET, &status);
  if (result < 0) {
    fprintf(stderr, "Error TIOCMGET: %s\n", strerror(errno));
    return -1;
  }
  if (value) {
    status |= TIOCM_DTR;
  } else {
    status &= ~TIOCM_DTR;
  }
  result = ioctl(fd, TIOCMSET, &status);
  if (result < 0) {
    fprintf(stderr, "Error TIOCMSET %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_DTR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1198 <set_DTR+0x68>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe79(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <set_DTR+0xf9>
cmpl   $0x0,-0xc(%rbp)
je     11b6 <set_DTR+0x86>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11c8 <set_DTR+0x98>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1222 <set_DTR+0xf2>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe03(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <set_DTR+0xf9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_DTR>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
js     1161 <set_DTR+0x31>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
test   %ebp,%ebp
je     116a <set_DTR+0x3a>
or     0x4(%rsp),%eax
jmp    1170 <set_DTR+0x40>
lea    0xe98(%rip),%rbx
jmp    1198 <set_DTR+0x68>
not    %eax
and    0x4(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    11c1 <set_DTR+0x91>
lea    0xe7c(%rip),%rbx
mov    0x2e29(%rip),%rax
mov    (%rax),%ebp
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@TIOCMGET = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Error TIOCMGET: %s\0A\00"
@errno = global i32 0
@TIOCM_DTR = global i32 0
@TIOCMSET = global i32 0
@.str.1 = [19 x i8] c"Error TIOCMSET %s\0A\00"
define i32 @set_DTR(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* @TIOCMGET
%10 = call i32 @ioctl(i32 %8, i32 %9, i32* %6)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %42
18: ; preds = %2
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @TIOCM_DTR
%23 = load i32, i32* %6
%24 = or i32 %23, %22
store i32 %24, i32* %6
br label %30
25: ; preds = %18
%26 = load i32, i32* @TIOCM_DTR
%27 = xor i32 %26, -1
%28 = load i32, i32* %6
%29 = and i32 %28, %27
store i32 %29, i32* %6
br label %30
30: ; preds = %25, %21
%31 = load i32, i32* %4
%32 = load i32, i32* @TIOCMSET
%33 = call i32 @ioctl(i32 %31, i32 %32, i32* %6)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32 -1, i32* %3
br label %42
41: ; preds = %30
store i32 0, i32* %3
br label %42
42: ; preds = %41, %36, %13
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@TIOCMGET = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Error TIOCMGET: %s\0A\00"
@errno = global i32 0
@TIOCM_DTR = global i32 0
@TIOCMSET = global i32 0
@.str.1 = [19 x i8] c"Error TIOCMSET %s\0A\00"
define i32 @set_DTR(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @TIOCMGET
%6 = call i32 @ioctl(i32 %0, i32 %5, i32* %3)
%7 = icmp slt i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %2
%9 = icmp eq i32 %1, 0
%10 = load i32, i32* @TIOCM_DTR
br i1 %9, label %14, label %11
11: ; preds = %8
%12 = load i32, i32* %3
%13 = or i32 %12, %10
br label %18
14: ; preds = %8
%15 = xor i32 %10, -1
%16 = load i32, i32* %3
%17 = and i32 %16, %15
br label %18
18: ; preds = %14, %11
%19 = phi i32 [ %17, %14 ], [ %13, %11 ]
store i32 %19, i32* %3
%20 = load i32, i32* @TIOCMSET
%21 = call i32 @ioctl(i32 %0, i32 %20, i32* %3)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18, %2
%24 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %18 ]
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* %24, i8* %27)
br label %29
29: ; preds = %23, %18
%30 = phi i32 [ 0, %18 ], [ -1, %23 ]
ret i32 %30
}