#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 pptcapi_connection {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int send_command(struct pptcapi_connection *, char *, int);
int read_commands(struct pptcapi_connection *connection, int ifd, int ofd) {
  char buffer[4096];
  int index = 0;
  char done = 0;
  char c = 0;
  while (!done) {
    int bytes_read = read(ifd, &c, 1);
    if (bytes_read < 0) {
      printf("failed to read from file\n");
      close(ifd);
      if (ofd != 1)
        close(ofd);
      return 1;
    }
    if (bytes_read == 0) {
      done = 1;
    } else {
      buffer[index] = c;
      index++;
    }
  }
  int result = 0;
  if (index != 0) {
    buffer[index] = '\0';
    result = send_command(connection, buffer, ofd);
  }
  close(ifd);
  if (ofd != 1)
    close(ofd);
  return result;
}


x86 assembly (clang, -O0):
<read_commands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1024(%rbp)
movb   $0x0,-0x1025(%rbp)
movb   $0x0,-0x1026(%rbp)
cmpb   $0x0,-0x1025(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1186 <read_commands+0x46>
jmp    1220 <read_commands+0xe0>
mov    -0x14(%rbp),%edi
lea    -0x1026(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jge    11df <read_commands+0x9f>
lea    0xe4c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x1,-0x18(%rbp)
je     11d3 <read_commands+0x93>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1282 <read_commands+0x142>
cmpl   $0x0,-0x102c(%rbp)
jne    11f8 <read_commands+0xb8>
movb   $0x1,-0x1025(%rbp)
jmp    121b <read_commands+0xdb>
mov    -0x1026(%rbp),%cl
movslq -0x1024(%rbp),%rax
mov    %cl,-0x1020(%rbp,%rax,1)
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    116d <read_commands+0x2d>
movl   $0x0,-0x1030(%rbp)
cmpl   $0x0,-0x1024(%rbp)
je     125f <read_commands+0x11f>
movslq -0x1024(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <send_command@plt>
mov    %eax,-0x1030(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x1,-0x18(%rbp)
je     1279 <read_commands+0x139>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1030(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_commands>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r15
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%r14d
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11a4 <read_commands+0x64>
xor    %ebx,%ebx
lea    0xf(%rsp),%r13
test   %eax,%eax
je     11b2 <read_commands+0x72>
movzbl 0xf(%rsp),%eax
mov    %al,0x10(%rsp,%rbx,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
add    $0x1,%rbx
test   %eax,%eax
jns    1180 <read_commands+0x40>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
jmp    11d3 <read_commands+0x93>
test   %ebx,%ebx
je     11d0 <read_commands+0x90>
movb   $0x0,0x10(%rsp,%rbx,1)
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
mov    %r12d,%edx
call   1060 <send_command@plt>
mov    %eax,%r14d
jmp    11d3 <read_commands+0x93>
xor    %r14d,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0x1,%r12d
je     11e8 <read_commands+0xa8>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pptcapi_connection = type { i32 }
@.str = [26 x i8] c"failed to read from file\0A\00"
define i32 @read_commands(%struct.pptcapi_connection* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.pptcapi_connection*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
store %struct.pptcapi_connection* %0, %struct.pptcapi_connection** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
store i8 0, i8* %10
store i8 0, i8* %11
br label %14
14: ; preds = %44, %3
%15 = load i8, i8* %10
%16 = icmp ne i8 %15, 0
%17 = xor i1 %16, true
br i1 %17, label %18, label %45
18: ; preds = %14
%19 = load i32, i32* %6
%20 = call i32 @read(i32 %19, i8* %11, i32 1)
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %23
store i32 1, i32* %4
br label %66
33: ; preds = %18
%34 = load i32, i32* %12
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i8 1, i8* %10
br label %44
37: ; preds = %33
%38 = load i8, i8* %11
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %40
store i8 %38, i8* %41
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %44
44: ; preds = %37, %36
br label %14
45: ; preds = %14
store i32 0, i32* %13
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %45
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %50
store i8 0, i8* %51
%52 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %5
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%54 = load i32, i32* %7
%55 = call i32 @send_command(%struct.pptcapi_connection* %52, i8* %53, i32 %54)
store i32 %55, i32* %13
br label %56
56: ; preds = %48, %45
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 1
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %56
%65 = load i32, i32* %13
store i32 %65, i32* %4
br label %66
66: ; preds = %64, %32
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.pptcapi_connection = type { i32 }
@str = [25 x i8] c"failed to read from file\00"
define i32 @read_commands(%struct.pptcapi_connection* %0, i32 %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca i8
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
store i8 0, i8* %5
%7 = call i32 @read(i32 %1, i8* %5, i32 1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %19, %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @close(i32 %1)
%12 = icmp eq i32 %2, 1
br i1 %12, label %38, label %13
13: ; preds = %9
%14 = tail call i32 @close(i32 %2)
br label %38
15: ; preds = %3, %19
%16 = phi i32 [ %24, %19 ], [ %7, %3 ]
%17 = phi i32 [ %23, %19 ], [ 0, %3 ]
%18 = icmp eq i32 %16, 0
br i1 %18, label %26, label %19
19: ; preds = %15
%20 = load i8, i8* %5
%21 = zext i32 %17 to i64
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %21
store i8 %20, i8* %22
%23 = add nuw nsw i32 %17, 1
%24 = call i32 @read(i32 %1, i8* %5, i32 1)
%25 = icmp slt i32 %24, 0
br i1 %25, label %9, label %15
26: ; preds = %15
%27 = icmp eq i32 %17, 0
br i1 %27, label %32, label %28
28: ; preds = %26
%29 = zext i32 %17 to i64
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 @send_command(%struct.pptcapi_connection* %0, i8* %6, i32 %2)
br label %32
32: ; preds = %28, %26
%33 = phi i32 [ %31, %28 ], [ 0, %26 ]
%34 = call i32 @close(i32 %1)
%35 = icmp eq i32 %2, 1
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @close(i32 %2)
br label %38
38: ; preds = %13, %9, %32, %36
%39 = phi i32 [ %33, %36 ], [ %33, %32 ], [ 1, %9 ], [ 1, %13 ]
ret i32 %39
}