#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 read(int, char *, int);
int my_getline(int fd, char *buffer, int max) {
  char c;
  int i = 0;
  do {
    if (read(fd, &c, 1) < 1)
      return 0;
    if (i < max)
      buffer[i++] = c;
  } while (c != '\n');
  buffer[i] = (char)0;
  return i;
}


x86 assembly (clang, -O0):
<my_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jge    114f <my_getline+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    1197 <my_getline+0x87>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1173 <my_getline+0x63>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1178 <my_getline+0x68>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1129 <my_getline+0x19>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
jmp    1134 <my_getline+0x24>
nopl   (%rax)
cmp    $0xa,%al
je     115d <my_getline+0x4d>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1167 <my_getline+0x57>
movzbl 0xf(%rsp),%eax
cmp    %r15d,%ebx
jge    1130 <my_getline+0x20>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %al,(%r14,%rcx,1)
jmp    1130 <my_getline+0x20>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
jmp    1169 <my_getline+0x59>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @my_getline(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
br label %5
5: ; preds = %16, %3
%6 = phi i32 [ 0, %3 ], [ %17, %16 ]
%7 = call i32 @read(i32 %0, i8* %4, i32 1)
%8 = icmp slt i32 %7, 1
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = icmp slt i32 %6, %2
%11 = load i8, i8* %4
br i1 %10, label %12, label %16
12: ; preds = %9
%13 = add nsw i32 %6, 1
%14 = sext i32 %6 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %11, i8* %15
br label %16
16: ; preds = %9, %12
%17 = phi i32 [ %13, %12 ], [ %6, %9 ]
%18 = icmp eq i8 %11, 10
br i1 %18, label %19, label %5
19: ; preds = %16
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %5, %19
%23 = phi i32 [ %17, %19 ], [ 0, %5 ]
ret i32 %23
}