#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 */
struct pollfd {
  int /*<<< orphan*/ events;
  int /*<<< orphan*/ fd;
};
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int poll(struct pollfd *, int, int);
int read(int /*<<< orphan*/, char *, int);
scalar_t__ strncmp(char *, char *, int);
int relay_set(FILE *adam, int port, int op) {
  fprintf(adam, "#011%d0%d\r", port, op);
  struct pollfd pfd;
  pfd.fd = fileno(adam);
  pfd.events = POLLIN;
  if (poll(&pfd, 1, 250) != 1)
    return 1;
  char buffer[30];
  if (read(fileno(adam), buffer, sizeof(buffer)) == 2 &&
      strncmp(">\r", buffer, 2) == 0)
    return 0;
  else
    return 1;
}


x86 assembly (clang, -O0):
<relay_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe8d(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xfa,%edx
call   1070 <poll@plt>
cmp    $0x1,%eax
je     11b8 <relay_set+0x68>
movl   $0x1,-0x4(%rbp)
jmp    120c <relay_set+0xbc>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
mov    $0x1e,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
jne    1205 <relay_set+0xb5>
lea    -0x40(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1205 <relay_set+0xb5>
movl   $0x0,-0x4(%rbp)
jmp    120c <relay_set+0xbc>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relay_set>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edx,%ecx
mov    %esi,%edx
mov    %rdi,%rbx
lea    0xe9c(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,0xc(%rsp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0xfa,%edx
call   1070 <poll@plt>
cmp    $0x1,%eax
jne    11e2 <relay_set+0x92>
mov    %rbx,%rdi
call   1060 <fileno@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x1e,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
jne    11d9 <relay_set+0x89>
lea    0xe46(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11e0 <relay_set+0x90>
mov    $0x1,%ebp
jmp    11e2 <relay_set+0x92>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@.str = [11 x i8] c"%d0%d\0D\00"
@POLLIN = global i32 0
@.str.1 = [3 x i8] c">\0D\00"
define i32 @relay_set(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.pollfd
%9 = alloca [30 x i8]
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32*, i32** %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = load i32*, i32** %5
%15 = call i32 @fileno(i32* %14)
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @POLLIN
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 0
store i32 %17, i32* %18
%19 = call i32 @poll(%struct.pollfd* %8, i32 1, i32 250)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 1, i32* %4
br label %34
22: ; preds = %3
%23 = load i32*, i32** %5
%24 = call i32 @fileno(i32* %23)
%25 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 30)
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%30 = call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 0, i32* %4
br label %34
33: ; preds = %28, %22
store i32 1, i32* %4
br label %34
34: ; preds = %33, %32, %21
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@.str = [11 x i8] c"%d0%d\0D\00"
@POLLIN = global i32 0
@.str.1 = [3 x i8] c">\0D\00"
define i32 @relay_set(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.pollfd
%5 = alloca [30 x i8]
%6 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%7 = bitcast %struct.pollfd* %4 to i8*
%8 = tail call i32 @fileno(i32* %0)
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @POLLIN
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %10, i32* %11
%12 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 250)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %25
14: ; preds = %3
%15 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%16 = call i32 @fileno(i32* %0)
%17 = call i32 @read(i32 %16, i8* %15, i32 30)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %19, %14
br label %23
23: ; preds = %19, %22
%24 = phi i32 [ 1, %22 ], [ 0, %19 ]
br label %25
25: ; preds = %3, %23
%26 = phi i32 [ %24, %23 ], [ 1, %3 ]
ret i32 %26
}