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

/* Variables and functions */
int iofgetc(struct img_io *);
char *iofgets(char *buf, int size, struct img_io *io) {
  int c;
  char *ptr = buf;
  while (--size > 0 && (c = iofgetc(io)) != -1) {
    *ptr++ = c;
    if (c == '\n')
      break;
  }
  *ptr = 0;
  return ptr == buf ? 0 : buf;
}


x86 assembly (clang, -O0):
<iofgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jle    1157 <iofgets+0x47>
mov    -0x18(%rbp),%rdi
call   1030 <iofgetc@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1167 <iofgets+0x57>
jmp    1191 <iofgets+0x81>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
cmpl   $0xa,-0x1c(%rbp)
jne    118c <iofgets+0x7c>
jmp    1191 <iofgets+0x81>
jmp    112b <iofgets+0x1b>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11b1 <iofgets+0xa1>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11b9 <iofgets+0xa9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iofgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2,%ebp
jl     1150 <iofgets+0x40>
mov    %r15,%rdi
call   1030 <iofgetc@plt>
cmp    $0xffffffff,%eax
je     1150 <iofgets+0x40>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    $0xa,%eax
jne    1130 <iofgets+0x20>
movb   $0x0,(%rbx)
xor    %eax,%eax
cmp    %r14,%rbx
cmovne %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.img_io = type { i32 }
define i8* @iofgets(i8* %0, i32 %1, %struct.img_io* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.img_io*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.img_io* %2, %struct.img_io** %6
%9 = load i8*, i8** %4
store i8* %9, i8** %8
br label %10
10: ; preds = %28, %3
%11 = load i32, i32* %5
%12 = add nsw i32 %11, -1
store i32 %12, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load %struct.img_io*, %struct.img_io** %6
%16 = call i32 @iofgetc(%struct.img_io* %15)
store i32 %16, i32* %7
%17 = icmp ne i32 %16, -1
br label %18
18: ; preds = %14, %10
%19 = phi i1 [ false, %10 ], [ %17, %14 ]
br i1 %19, label %20, label %29
20: ; preds = %18
%21 = load i32, i32* %7
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %8
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %8
store i8 %22, i8* %23
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %28
27: ; preds = %20
br label %29
28: ; preds = %20
br label %10
29: ; preds = %27, %18
%30 = load i8*, i8** %8
store i8 0, i8* %30
%31 = load i8*, i8** %8
%32 = load i8*, i8** %4
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %35
34: ; preds = %29
br label %37
35: ; preds = %29
%36 = load i8*, i8** %4
br label %37
37: ; preds = %35, %34
%38 = phi i8* [ null, %34 ], [ %36, %35 ]
ret i8* %38
}

LLVM IR (-O2):
%struct.img_io = type { i32 }
define i8* @iofgets(i8* writeonly %0, i32 %1, %struct.img_io* %2) {
br label %4
4: ; preds = %12, %3
%5 = phi i32 [ %1, %3 ], [ %7, %12 ]
%6 = phi i8* [ %0, %3 ], [ %14, %12 ]
%7 = add nsw i32 %5, -1
%8 = icmp sgt i32 %5, 1
br i1 %8, label %9, label %16
9: ; preds = %4
%10 = tail call i32 @iofgetc(%struct.img_io* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = trunc i32 %10 to i8
%14 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %13, i8* %6
%15 = icmp eq i32 %10, 10
br i1 %15, label %16, label %4
16: ; preds = %4, %12, %9
%17 = phi i8* [ %14, %12 ], [ %6, %9 ], [ %6, %4 ]
store i8 0, i8* %17
%18 = icmp eq i8* %17, %0
%19 = select i1 %18, i8* null, i8* %0
ret i8* %19
}