#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 fd;
  scalar_t__ revents;
  int /*<<< orphan*/ events;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ reply;

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int snprintf(char *, int, char *);
int write(int, char *, int);
int doit(int fd, int i) {
  struct pollfd pfd;
  char cmd[16];
  char reply[16];
  ssize_t val;
  int ret;
  ret = snprintf(cmd, 3, "AT\r");
  if (ret < 0) {
    perror("snprintf");
    goto error;
  }
  if (write(fd, cmd, 3) != 3) {
    perror("write");
    goto error;
  }
  pfd.fd = fd;
  pfd.events = POLLIN;
  pfd.revents = 0;
  ret = poll(&pfd, 1, -1);
  if (ret < 1) {
    perror("pollin");
    goto error;
  }
  val = read(fd, reply, sizeof(reply));
  if (val < 0) {
    perror("read");
    goto error;
  }
  printf("%d. cmd %s reply %s\n", i, cmd, reply);
  return 0;
error:
  printf("failed\n");
  return -1;
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x3,%esi
lea    0xe82(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    11a1 <doit+0x41>
lea    0xe6d(%rip),%rdi
call   1080 <perror@plt>
jmp    126d <doit+0x10d>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
je     11cc <doit+0x6c>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
jmp    126d <doit+0x10d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x1,-0x5c(%rbp)
jge    1217 <doit+0xb7>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
jmp    126d <doit+0x10d>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jge    1248 <doit+0xe8>
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
jmp    126d <doit+0x10d>
mov    -0xc(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <doit+0x122>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doit>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xe7f(%rip),%rdx
mov    %rsp,%rdi
mov    $0x3,%esi
call   1060 <snprintf@plt>
test   %eax,%eax
js     120a <doit+0x9a>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
jne    1213 <doit+0xa3>
mov    %ebp,0x18(%rsp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x18(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
test   %eax,%eax
jle    121c <doit+0xac>
lea    0x30(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1070 <read@plt>
test   %rax,%rax
js     1225 <doit+0xb5>
lea    0xe2a(%rip),%rdi
xor    %ebp,%ebp
mov    %rsp,%rdx
lea    0x30(%rsp),%rcx
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1242 <doit+0xd2>
lea    0xdf3(%rip),%rdi
jmp    122c <doit+0xbc>
lea    0xdf3(%rip),%rdi
jmp    122c <doit+0xbc>
lea    0xdf0(%rip),%rdi
jmp    122c <doit+0xbc>
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32 }
@.str = [4 x i8] c"AT\0D\00"
@.str.1 = [9 x i8] c"snprintf\00"
@.str.2 = [6 x i8] c"write\00"
@POLLIN = global i32 0
@.str.3 = [7 x i8] c"pollin\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [21 x i8] c"%d. cmd %s reply %s\0A\00"
@.str.6 = [8 x i8] c"failed\0A\00"
define i32 @doit(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.pollfd
%7 = alloca [16 x i8]
%8 = alloca [16 x i8]
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%12 = call i32 @snprintf(i8* %11, i32 3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %48
17: ; preds = %2
%18 = load i32, i32* %4
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%20 = call i32 @write(i32 %18, i8* %19, i32 3)
%21 = icmp ne i32 %20, 3
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %48
24: ; preds = %17
%25 = load i32, i32* %4
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @POLLIN
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 1
store i64 0, i64* %29
%30 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 -1)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %48
35: ; preds = %24
%36 = load i32, i32* %4
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%38 = call i64 @read(i32 %36, i8* %37, i32 16)
store i64 %38, i64* %9
%39 = load i64, i64* %9
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %48
43: ; preds = %35
%44 = load i32, i32* %5
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %44, i8* %45, i8* %46)
store i32 0, i32* %3
br label %50
48: ; preds = %41, %33, %22, %15
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
50: ; preds = %48, %43
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i32 }
@.str = [4 x i8] c"AT\0D\00"
@.str.1 = [9 x i8] c"snprintf\00"
@.str.2 = [6 x i8] c"write\00"
@POLLIN = global i32 0
@.str.3 = [7 x i8] c"pollin\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [21 x i8] c"%d. cmd %s reply %s\0A\00"
@str = [7 x i8] c"failed\00"
define i32 @doit(i32 %0, i32 %1) {
%3 = alloca %struct.pollfd
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = bitcast %struct.pollfd* %3 to i8*
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%9 = call i32 @snprintf(i8* %7, i32 3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp slt i32 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %2
%12 = call i32 @write(i32 %0, i8* %7, i32 3)
%13 = icmp eq i32 %12, 3
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %0, i32* %15
%16 = load i32, i32* @POLLIN
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i64 0, i64* %18
%19 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 -1)
%20 = icmp slt i32 %19, 1
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = call i64 @read(i32 %0, i8* %8, i32 16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %1, i8* %7, i8* %8)
br label %30
26: ; preds = %21, %14, %11, %2
%27 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %14 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %21 ]
%28 = call i32 @perror(i8* %27)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %26, %24
%31 = phi i32 [ -1, %26 ], [ 0, %24 ]
ret i32 %31
}