#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t my_gets(char *ptr, size_t size, FILE *f) {
  size_t i;
  int c;
  i = 0;
  while ((c = getc(f)) > 0) {
    if (i >= size - 1) {
      fputs("String length exceeds decompiler limit.\n", stderr);
      exit(1);
    }
    ptr[i++] = c;
  }
  ptr[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<my_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <getc@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11b5 <my_gets+0x85>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     1195 <my_gets+0x65>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1030 <fputs@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    114c <my_gets+0x1c>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_gets>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
call   1050 <getc@plt>
test   %eax,%eax
jle    117b <my_gets+0x4b>
add    $0xffffffffffffffff,%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %rbx,%r12
je     1191 <my_gets+0x61>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1050 <getc@plt>
test   %eax,%eax
jg     1160 <my_gets+0x30>
jmp    117d <my_gets+0x4d>
xor    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1030 <fputs@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"String length exceeds decompiler limit.\0A\00"
@stderr = global i32 0
define i64 @my_gets(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
store i64 0, i64* %7
br label %9
9: ; preds = %22, %3
%10 = load i32*, i32** %6
%11 = call i32 @getc(i32* %10)
store i32 %11, i32* %8
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %9
%14 = load i64, i64* %7
%15 = load i64, i64* %5
%16 = sub i64 %15, 1
%17 = icmp uge i64 %14, %16
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i32, i32* %8
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %4
%26 = load i64, i64* %7
%27 = add i64 %26, 1
store i64 %27, i64* %7
%28 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 %24, i8* %28
br label %9
29: ; preds = %9
%30 = load i8*, i8** %4
%31 = load i64, i64* %7
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 0, i8* %32
%33 = load i64, i64* %7
ret i64 %33
}

LLVM IR (-O2):
@.str = [41 x i8] c"String length exceeds decompiler limit.\0A\00"
@stderr = global i32 0
define i64 @my_gets(i8* nocapture writeonly %0, i64 %1, i32* nocapture %2) {
%4 = tail call i32 @getc(i32* %2)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %22
6: ; preds = %3
%7 = add i64 %1, -1
br label %8
8: ; preds = %6, %16
%9 = phi i32 [ %4, %6 ], [ %20, %16 ]
%10 = phi i64 [ 0, %6 ], [ %18, %16 ]
%11 = icmp eq i64 %10, %7
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = trunc i32 %9 to i8
%18 = add i64 %10, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 %17, i8* %19
%20 = tail call i32 @getc(i32* %2)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %8, label %22
22: ; preds = %16, %3
%23 = phi i64 [ 0, %3 ], [ %18, %16 ]
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret i64 %23
}