#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*/ printf(char *, int);
int read(int, unsigned char *, int);
int ugets(int fd, unsigned char *buf) {
  int i = 0, j;
  unsigned char a;
  for (;;) {
    j = read(fd, &a, 1);
    if (j != 1) {
      printf("j=%d\n", j);
      continue;
    }
    if (a == '\n')
      break;
    if (a == '\r')
      continue;
    buf[i++] = a;
  }
  buf[i++] = 0;
  return --i;
}


x86 assembly (clang, -O0):
<ugets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     1168 <ugets+0x48>
mov    -0x18(%rbp),%esi
lea    0xea2(%rip),%rdi
call   1030 <printf@plt>
jmp    1136 <ugets+0x16>
movzbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    117a <ugets+0x5a>
jmp    11a9 <ugets+0x89>
movzbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    118c <ugets+0x6c>
jmp    1136 <ugets+0x16>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1136 <ugets+0x16>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ugets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %r15d,%r15d
lea    0xf(%rsp),%rbx
lea    0xec0(%rip),%r12
jmp    115a <ugets+0x3a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1150 <ugets+0x30>
movzbl 0xf(%rsp),%eax
cmp    $0xd,%al
je     115a <ugets+0x3a>
cmp    $0xa,%al
je     1185 <ugets+0x65>
mov    %al,(%r14,%r15,1)
add    $0x1,%r15
jmp    115a <ugets+0x3a>
mov    %r15d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"j=%d\0A\00"
define i32 @ugets(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i8
br label %4
4: ; preds = %14, %2
%5 = phi i64 [ %15, %14 ], [ 0, %2 ]
br label %6
6: ; preds = %11, %4
%7 = call i32 @read(i32 %0, i8* %3, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %11
11: ; preds = %9, %12
br label %6
12: ; preds = %6
%13 = load i8, i8* %3
switch i8 %13, label %14 [
i8 10, label %17
i8 13, label %11
]
14: ; preds = %12
%15 = add nuw i64 %5, 1
%16 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 %13, i8* %16
br label %4
17: ; preds = %12
%18 = trunc i64 %5 to i32
%19 = and i64 %5, 4294967295
%20 = getelementptr inbounds i8, i8* %1, i64 %19
store i8 0, i8* %20
ret i32 %18
}