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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ exit(int);
int getc(int /*<<< orphan*/ *);
char *malloc(unsigned int);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
char *gethline(FILE *fd) {
  char line[512], *p;
  int c;
  c = getc(fd);
  if (c < 0) {
    syslog(LOG_ERR, "cannot read mail header");
    return (0);
  }
  for (p = line; c >= 0 && c != '\n'; c = getc(fd)) {
    *p++ = c;
    if (p - line >= sizeof(line) - 2) {
      syslog(LOG_ERR, "too long header line");
      return (0);
    }
  }
  *p = 0;
  if (!line[0])
    return (0);
  p = malloc((unsigned)(strlen(line) + 1));
  if (!p) {
    syslog(LOG_ERR, "out of memory, aborted");
    exit(-1);
  }
  strcpy(p, line);
  return (p);
}


x86 assembly (clang, -O0):
<gethline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jge    11ad <gethline+0x4d>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1060 <syslog@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f7 <gethline+0x197>
lea    -0x210(%rbp),%rax
mov    %rax,-0x218(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x21c(%rbp)
mov    %al,-0x21d(%rbp)
jl     11e0 <gethline+0x80>
cmpl   $0xa,-0x21c(%rbp)
setne  %al
mov    %al,-0x21d(%rbp)
mov    -0x21d(%rbp),%al
test   $0x1,%al
jne    11f3 <gethline+0x93>
jmp    126a <gethline+0x10a>
mov    -0x21c(%rbp),%eax
mov    %al,%cl
mov    -0x218(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x218(%rbp)
mov    %cl,(%rax)
mov    -0x218(%rbp),%rax
lea    -0x210(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x1fe,%rax
jb     1251 <gethline+0xf1>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1060 <syslog@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f7 <gethline+0x197>
jmp    1256 <gethline+0xf6>
mov    -0x10(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x21c(%rbp)
jmp    11bb <gethline+0x5b>
mov    -0x218(%rbp),%rax
movb   $0x0,(%rax)
cmpb   $0x0,-0x210(%rbp)
jne    128e <gethline+0x12e>
movq   $0x0,-0x8(%rbp)
jmp    12f7 <gethline+0x197>
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    12d9 <gethline+0x179>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x218(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x218(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethline>:
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
call   1080 <getc@plt>
test   %eax,%eax
js     1180 <gethline+0x20>
cmp    $0xa,%eax
jne    1192 <gethline+0x32>
mov    %rsp,%rax
jmp    11c5 <gethline+0x65>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
jmp    1203 <gethline+0xa3>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %al,(%rsp,%rbx,1)
cmp    $0x1fd,%rbx
je     11f3 <gethline+0x93>
mov    %r14,%rdi
call   1080 <getc@plt>
add    $0x1,%rbx
test   %eax,%eax
js     11c1 <gethline+0x61>
cmp    $0xa,%eax
jne    11a0 <gethline+0x40>
lea    (%rsp,%rbx,1),%rax
movb   $0x0,(%rax)
cmpb   $0x0,(%rsp)
je     1208 <gethline+0xa8>
mov    %rsp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1218 <gethline+0xb8>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
jmp    120a <gethline+0xaa>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
call   1060 <syslog@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [24 x i8] c"cannot read mail header\00"
@.str.1 = [21 x i8] c"too long header line\00"
@.str.2 = [23 x i8] c"out of memory, aborted\00"
define i8* @gethline(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [512 x i8]
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %3
%7 = load i32*, i32** %3
%8 = call i32 @getc(i32* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @LOG_ERR
%13 = call i32 @syslog(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %65
14: ; preds = %1
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
store i8* %15, i8** %5
br label %16
16: ; preds = %39, %14
%17 = load i32, i32* %6
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 10
br label %22
22: ; preds = %19, %16
%23 = phi i1 [ false, %16 ], [ %21, %19 ]
br i1 %23, label %24, label %42
24: ; preds = %22
%25 = load i32, i32* %6
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %5
store i8 %26, i8* %27
%29 = load i8*, i8** %5
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = icmp uge i64 %33, 510
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = load i32, i32* @LOG_ERR
%37 = call i32 @syslog(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %65
38: ; preds = %24
br label %39
39: ; preds = %38
%40 = load i32*, i32** %3
%41 = call i32 @getc(i32* %40)
store i32 %41, i32* %6
br label %16
42: ; preds = %22
%43 = load i8*, i8** %5
store i8 0, i8* %43
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%45 = load i8, i8* %44
%46 = icmp ne i8 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %42
store i8* null, i8** %2
br label %65
48: ; preds = %42
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%50 = call i64 @strlen(i8* %49)
%51 = add nsw i64 %50, 1
%52 = trunc i64 %51 to i32
%53 = call i8* @malloc(i32 %52)
store i8* %53, i8** %5
%54 = load i8*, i8** %5
%55 = icmp ne i8* %54, null
br i1 %55, label %60, label %56
56: ; preds = %48
%57 = load i32, i32* @LOG_ERR
%58 = call i32 @syslog(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%59 = call i32 @exit(i32 -1)
unreachable
60: ; preds = %48
%61 = load i8*, i8** %5
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%63 = call i32 @strcpy(i8* %61, i8* %62)
%64 = load i8*, i8** %5
store i8* %64, i8** %2
br label %65
65: ; preds = %60, %47, %35, %11
%66 = load i8*, i8** %2
ret i8* %66
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [24 x i8] c"cannot read mail header\00"
@.str.1 = [21 x i8] c"too long header line\00"
@.str.2 = [23 x i8] c"out of memory, aborted\00"
define i8* @gethline(i32* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @getc(i32* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = icmp eq i32 %4, 10
br i1 %7, label %27, label %8
8: ; preds = %6
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 509
br label %13
10: ; preds = %1
%11 = load i32, i32* @LOG_ERR
%12 = tail call i32 @syslog(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %43
13: ; preds = %8, %22
%14 = phi i32 [ %4, %8 ], [ %23, %22 ]
%15 = phi i8* [ %3, %8 ], [ %17, %22 ]
%16 = trunc i32 %14 to i8
%17 = getelementptr inbounds i8, i8* %15, i64 1
store i8 %16, i8* %15
%18 = icmp eq i8* %15, %9
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @LOG_ERR
%21 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %43
22: ; preds = %13
%23 = call i32 @getc(i32* %0)
%24 = icmp sgt i32 %23, -1
%25 = icmp ne i32 %23, 10
%26 = and i1 %24, %25
br i1 %26, label %13, label %27
27: ; preds = %22, %6
%28 = phi i8* [ %3, %6 ], [ %17, %22 ]
store i8 0, i8* %28
%29 = load i8, i8* %3
%30 = icmp eq i8 %29, 0
br i1 %30, label %43, label %31
31: ; preds = %27
%32 = call i64 @strlen(i8* %3)
%33 = trunc i64 %32 to i32
%34 = add i32 %33, 1
%35 = call i8* @malloc(i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i32, i32* @LOG_ERR
%39 = call i32 @syslog(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %31
%42 = call i32 @strcpy(i8* %35, i8* %3)
br label %43
43: ; preds = %27, %41, %19, %10
%44 = phi i8* [ null, %10 ], [ null, %19 ], [ %35, %41 ], [ null, %27 ]
ret i8* %44
}