#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 */
struct netio {
  int dummy;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int netio_printf(struct netio *, char *);
size_t netio_read_until(struct netio *, char *, char *, int,
                        int /*<<< orphan*/ *);
unsigned long strtoul(char *, char **, int /*<<< orphan*/);
int get_num(struct netio *netio, int last, int *ret) {
  char buf[10];
  ssize_t got;
  unsigned long n;
  char *end;
  got = netio_read_until(netio, last ? "\n" : " ", buf, sizeof(buf), NULL);
  if (got < 0) {
    *ret = -1;
    return -1;
  }
  buf[got] = 0;
  n = strtoul(buf, &end, 0);
  if (*end) {
    *ret = netio_printf(netio, "-bad number\n");
    return -1;
  }
  return n;
}


x86 assembly (clang, -O0):
<get_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%ecx
lea    0xeaf(%rip),%rsi
lea    0xeb5(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    -0x2a(%rbp),%rdx
mov    $0xa,%ecx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <netio_read_until@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jae    1197 <get_num+0x67>
mov    -0x20(%rbp),%rax
movl   $0xffffffff,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <get_num+0xbb>
mov    -0x38(%rbp),%rax
movb   $0x0,-0x2a(%rbp,%rax,1)
lea    -0x2a(%rbp),%rdi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rax
cmpb   $0x0,(%rax)
je     11e4 <get_num+0xb4>
mov    -0x10(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1050 <netio_printf@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <get_num+0xbb>
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rdi,%rbx
test   %esi,%esi
lea    0xeb8(%rip),%rax
lea    0xebe(%rip),%rsi
cmove  %rax,%rsi
lea    0x16(%rsp),%r15
mov    %r15,%rdx
mov    $0xa,%ecx
xor    %r8d,%r8d
call   1030 <netio_read_until@plt>
movb   $0x0,0x16(%rsp,%rax,1)
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     119d <get_num+0x6d>
lea    0xe75(%rip),%rsi
mov    %rbx,%rdi
call   1050 <netio_printf@plt>
mov    %eax,(%r14)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.netio = type { i32 }
@.str = [2 x i8] c"\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [13 x i8] c"-bad number\0A\00"
define i32 @get_num(%struct.netio* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.netio*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [10 x i8]
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store %struct.netio* %0, %struct.netio** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%12 = load %struct.netio*, %struct.netio** %5
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 0
%15 = zext i1 %14 to i64
%16 = select i1 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%18 = call i64 @netio_read_until(%struct.netio* %12, i8* %16, i8* %17, i32 10, i32* null)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp ult i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32*, i32** %7
store i32 -1, i32* %22
store i32 -1, i32* %4
br label %38
23: ; preds = %3
%24 = load i64, i64* %9
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%27 = call i64 @strtoul(i8* %26, i8** %11, i32 0)
store i64 %27, i64* %10
%28 = load i8*, i8** %11
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = load %struct.netio*, %struct.netio** %5
%33 = call i32 @netio_printf(%struct.netio* %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32*, i32** %7
store i32 %33, i32* %34
store i32 -1, i32* %4
br label %38
35: ; preds = %23
%36 = load i64, i64* %10
%37 = trunc i64 %36 to i32
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %31, %21
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.netio = type { i32 }
@.str = [2 x i8] c"\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [13 x i8] c"-bad number\0A\00"
define i32 @get_num(%struct.netio* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca [10 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
%8 = icmp eq i32 %1, 0
%9 = select i1 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)
%10 = call i64 @netio_read_until(%struct.netio* %0, i8* %9, i8* %6, i32 10, i32* null)
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i64 @strtoul(i8* %6, i8** %5, i32 0)
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 @netio_printf(%struct.netio* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 %17, i32* %2
br label %20
18: ; preds = %3
%19 = trunc i64 %12 to i32
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ -1, %16 ], [ %19, %18 ]
ret i32 %21
}