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

/* Variables and functions */
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
scalar_t__ strlen(char *);
char *readcmd(FILE *f) {
  static char *buf;
  char *str;
  ssize_t ret;
  if (!buf) {
    buf = malloc(4096);
    if (!buf)
      return NULL;
  }
  str = buf;
  *str = 0;
  while (1) {
    ret = fread(str, 1, 1, f);
    if (!ret)
      return strlen(buf) ? buf : NULL;
    if (*str == '\n' || *str == ';') {
      *str = 0;
      return buf;
    }
    str++;
  }
}


x86 assembly (clang, -O0):
<readcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,0x2efc(%rip)
jne    117b <readcmd+0x4b>
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ee5(%rip)
cmpq   $0x0,0x2edd(%rip)
jne    1176 <readcmd+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1234 <readcmd+0x104>
jmp    117b <readcmd+0x4b>
mov    0x2ebe(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11ec <readcmd+0xbc>
mov    0x2e8b(%rip),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
je     11d4 <readcmd+0xa4>
mov    0x2e75(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11df <readcmd+0xaf>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11df <readcmd+0xaf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <readcmd+0x104>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     120c <readcmd+0xdc>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3b,%eax
jne    1223 <readcmd+0xf3>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e26(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <readcmd+0x104>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    118d <readcmd+0x5d>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcmd>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2f0a(%rip),%rbx
test   %rbx,%rbx
jne    114c <readcmd+0x2c>
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x2ef1(%rip)
test   %rax,%rax
je     11b8 <readcmd+0x98>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     1199 <readcmd+0x79>
add    $0x1,%rbx
nopl   0x0(%rax)
movzbl -0x1(%rbx),%eax
cmp    $0x3b,%al
je     11ab <readcmd+0x8b>
cmp    $0xa,%al
je     11ab <readcmd+0x8b>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    1170 <readcmd+0x50>
mov    0x2e98(%rip),%rcx
xor    %eax,%eax
cmpb   $0x0,(%rcx)
cmovne %rcx,%rax
jmp    11ba <readcmd+0x9a>
movb   $0x0,-0x1(%rbx)
mov    0x2e82(%rip),%rax
jmp    11ba <readcmd+0x9a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@readcmd.buf = internal global i8* null
define i8* @readcmd(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i8*, i8** @readcmd.buf
%7 = icmp ne i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = call i8* @malloc(i32 4096)
store i8* %9, i8** @readcmd.buf
%10 = load i8*, i8** @readcmd.buf
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %8
store i8* null, i8** %2
br label %48
13: ; preds = %8
br label %14
14: ; preds = %13, %1
%15 = load i8*, i8** @readcmd.buf
store i8* %15, i8** %4
%16 = load i8*, i8** %4
store i8 0, i8* %16
br label %17
17: ; preds = %14, %45
%18 = load i8*, i8** %4
%19 = load i32*, i32** %3
%20 = call i32 @fread(i8* %18, i32 1, i32 1, i32* %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %17
%24 = load i8*, i8** @readcmd.buf
%25 = call i64 @strlen(i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i8*, i8** @readcmd.buf
br label %30
29: ; preds = %23
br label %30
30: ; preds = %29, %27
%31 = phi i8* [ %28, %27 ], [ null, %29 ]
store i8* %31, i8** %2
br label %48
32: ; preds = %17
%33 = load i8*, i8** %4
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %42, label %37
37: ; preds = %32
%38 = load i8*, i8** %4
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 59
br i1 %41, label %42, label %45
42: ; preds = %37, %32
%43 = load i8*, i8** %4
store i8 0, i8* %43
%44 = load i8*, i8** @readcmd.buf
store i8* %44, i8** %2
br label %48
45: ; preds = %37
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %4
br label %17
48: ; preds = %42, %30, %12
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@readcmd.buf = internal unnamed_addr global i8* null
define i8* @readcmd(i32* nocapture %0) {
%2 = load i8*, i8** @readcmd.buf
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
store i8* %5, i8** @readcmd.buf
%6 = icmp eq i8* %5, null
br i1 %6, label %25, label %7
7: ; preds = %4, %1
%8 = phi i8* [ %5, %4 ], [ %2, %1 ]
store i8 0, i8* %8
%9 = tail call i32 @fread(i8* %8, i32 1, i32 1, i32* %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %21, %7
%12 = load i8*, i8** @readcmd.buf
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
%15 = select i1 %14, i8* null, i8* %12
br label %25
16: ; preds = %7, %21
%17 = phi i8* [ %22, %21 ], [ %8, %7 ]
%18 = load i8, i8* %17
switch i8 %18, label %21 [
i8 10, label %19
i8 59, label %19
]
19: ; preds = %16, %16
store i8 0, i8* %17
%20 = load i8*, i8** @readcmd.buf
br label %25
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %17, i64 1
%23 = tail call i32 @fread(i8* %22, i32 1, i32 1, i32* %0)
%24 = icmp eq i32 %23, 0
br i1 %24, label %11, label %16
25: ; preds = %4, %19, %11
%26 = phi i8* [ %20, %19 ], [ %15, %11 ], [ null, %4 ]
ret i8* %26
}