#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EBADOP;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ send_error(int, int /*<<< orphan*/);
int /*<<< orphan*/ tftp_log(int /*<<< orphan*/, char *);
ssize_t get_field(int peer, char *buffer, ssize_t size) {
  char *cp = buffer;
  while (cp < buffer + size) {
    if (*cp == '\0')
      break;
    cp++;
  }
  if (*cp != '\0') {
    tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
    send_error(peer, EBADOP);
    exit(1);
  }
  return (cp - buffer + 1);
}


x86 assembly (clang, -O0):
<get_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1188 <get_field+0x58>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1177 <get_field+0x47>
jmp    1188 <get_field+0x58>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114a <get_field+0x1a>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c8 <get_field+0x98>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1030 <tftp_log@plt>
mov    -0x4(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1040 <send_error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_field>:
push   %rbx
mov    %edi,%ebx
mov    %rsi,%rax
test   %edx,%edx
jle    115e <get_field+0x2e>
movslq %edx,%rcx
add    %rsi,%rcx
mov    %rsi,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x0,(%rax)
je     1163 <get_field+0x33>
add    $0x1,%rax
cmp    %rcx,%rax
jb     1150 <get_field+0x20>
cmpb   $0x0,(%rax)
jne    116a <get_field+0x3a>
sub    %esi,%eax
add    $0x1,%eax
pop    %rbx
ret
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1030 <tftp_log@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <send_error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [34 x i8] c"Bad option - no trailing \\0 found\00"
@EBADOP = global i32 0
define i32 @get_field(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
store i8* %8, i8** %7
br label %9
9: ; preds = %22, %3
%10 = load i8*, i8** %7
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = icmp ult i8* %10, %14
br i1 %15, label %16, label %25
16: ; preds = %9
%17 = load i8*, i8** %7
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %25
22: ; preds = %16
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %7
br label %9
25: ; preds = %21, %9
%26 = load i8*, i8** %7
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %25
%31 = load i32, i32* @LOG_ERR
%32 = call i32 @tftp_log(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %4
%34 = load i32, i32* @EBADOP
%35 = call i32 @send_error(i32 %33, i32 %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %25
%38 = load i8*, i8** %7
%39 = load i8*, i8** %5
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = add nsw i64 %42, 1
%44 = trunc i64 %43 to i32
ret i32 %44
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [34 x i8] c"Bad option - no trailing \\0 found\00"
@EBADOP = global i32 0
define i32 @get_field(i32 %0, i8* %1, i32 %2) {
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds i8, i8* %1, i64 %4
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %14
7: ; preds = %3, %11
%8 = phi i8* [ %12, %11 ], [ %1, %3 ]
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %8, i64 1
%13 = icmp ult i8* %12, %5
br i1 %13, label %7, label %14
14: ; preds = %11, %3
%15 = phi i8* [ %1, %3 ], [ %12, %11 ]
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %14
%19 = load i32, i32* @LOG_ERR
%20 = tail call i32 @tftp_log(i32 %19, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EBADOP
%22 = tail call i32 @send_error(i32 %0, i32 %21)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %7, %14
%25 = phi i8* [ %15, %14 ], [ %8, %7 ]
%26 = ptrtoint i8* %25 to i64
%27 = ptrtoint i8* %1 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = add i32 %29, 1
ret i32 %30
}