#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*/ perror(char *);
int read(int, char *, int);
int com_gets(int fd, char *s, int size) {
  int i;
  int r;
  for (i = 0; i < size - 1; i++) {
    r = read(fd, s, 1);
    if (r < 0) {
      perror("read() failed");
      return -1;
    }
    if (r != 1)
      return 32;
    if (*s++ == '\n') {
      *s = '\0';
      return 0;
    }
  }
  return 34;
}


x86 assembly (clang, -O0):
<com_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11d7 <com_gets+0xb7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1180 <com_gets+0x60>
lea    0xe91(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <com_gets+0xbe>
cmpl   $0x1,-0x1c(%rbp)
je     1196 <com_gets+0x76>
movl   $0x20,-0x4(%rbp)
jmp    11de <com_gets+0xbe>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11c4 <com_gets+0xa4>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11de <com_gets+0xbe>
jmp    11c9 <com_gets+0xa9>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <com_gets+0x19>
movl   $0x22,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<com_gets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x22,%r14d
cmp    $0x2,%edx
jl     1178 <com_gets+0x58>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
add    $0xffffffff,%ebp
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1186 <com_gets+0x66>
cmp    $0x1,%eax
jne    1169 <com_gets+0x49>
cmpb   $0xa,(%rbx)
je     1171 <com_gets+0x51>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1140 <com_gets+0x20>
jmp    1178 <com_gets+0x58>
mov    $0x20,%r14d
jmp    1178 <com_gets+0x58>
movb   $0x0,0x1(%rbx)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1178 <com_gets+0x58>

LLVM IR (-O0):
@.str = [14 x i8] c"read() failed\00"
define i32 @com_gets(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %36, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %39
15: ; preds = %10
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = call i32 @read(i32 %16, i8* %17, i32 1)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %40
23: ; preds = %15
%24 = load i32, i32* %9
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 32, i32* %4
br label %40
27: ; preds = %23
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
%30 = load i8, i8* %28
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i8*, i8** %6
store i8 0, i8* %34
store i32 0, i32* %4
br label %40
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %10
39: ; preds = %10
store i32 34, i32* %4
br label %40
40: ; preds = %39, %33, %26, %21
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [14 x i8] c"read() failed\00"
define i32 @com_gets(i32 %0, i8* nocapture %1, i32 %2) {
%4 = add i32 %2, -1
%5 = icmp sgt i32 %2, 1
br i1 %5, label %8, label %23
6: ; preds = %17
%7 = icmp eq i32 %21, %4
br i1 %7, label %23, label %8
8: ; preds = %3, %6
%9 = phi i32 [ %21, %6 ], [ 0, %3 ]
%10 = phi i8* [ %18, %6 ], [ %1, %3 ]
%11 = tail call i32 @read(i32 %0, i8* %10, i32 1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %23
15: ; preds = %8
%16 = icmp eq i32 %11, 1
br i1 %16, label %17, label %23
17: ; preds = %15
%18 = getelementptr inbounds i8, i8* %10, i64 1
%19 = load i8, i8* %10
%20 = icmp eq i8 %19, 10
%21 = add nuw nsw i32 %9, 1
br i1 %20, label %22, label %6
22: ; preds = %17
store i8 0, i8* %18
br label %23
23: ; preds = %15, %6, %3, %22, %13
%24 = phi i32 [ -1, %13 ], [ 0, %22 ], [ 34, %3 ], [ 32, %15 ], [ 34, %6 ]
ret i32 %24
}