#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 read(int, char *, int);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnc(int /*<<< orphan*/, char *);
int write(int, char *, int);
int get_slot_number(int so) {
  char buf[8];
  int rv;
  int nslot;
  if ((rv = write(so, "S", 1)) < 1) {
    warn("write");
    goto err;
  } else if (rv != 1) {
    warnc(0, "write: fail.");
    goto err;
  }
  if ((rv = read(so, buf, sizeof buf)) < 0) {
    warn("read");
    goto err;
  }
  buf[sizeof buf - 1] = 0;
  if (sscanf(buf, "%d", &nslot) != 1) {
    warnc(0, "Invalid response.");
    goto err;
  }
  return nslot;
err:
  return -1;
}


x86 assembly (clang, -O0):
<get_slot_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe9b(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
jge    118c <get_slot_number+0x3c>
lea    0xe80(%rip),%rdi
call   1030 <warn@plt>
jmp    121b <get_slot_number+0xcb>
cmpl   $0x1,-0x14(%rbp)
je     11a9 <get_slot_number+0x59>
xor    %edi,%edi
lea    0xe69(%rip),%rsi
call   1060 <warnc@plt>
jmp    121b <get_slot_number+0xcb>
jmp    11ae <get_slot_number+0x5e>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11dc <get_slot_number+0x8c>
lea    0xe43(%rip),%rdi
call   1030 <warn@plt>
jmp    121b <get_slot_number+0xcb>
movb   $0x0,-0x9(%rbp)
lea    -0x10(%rbp),%rdi
lea    0xe2f(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     1210 <get_slot_number+0xc0>
xor    %edi,%edi
lea    0xe17(%rip),%rsi
call   1060 <warnc@plt>
jmp    121b <get_slot_number+0xcb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <get_slot_number+0xd2>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_slot_number>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xea2(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
test   %eax,%eax
jle    11ac <get_slot_number+0x5c>
cmp    $0x1,%eax
jne    11b5 <get_slot_number+0x65>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11be <get_slot_number+0x6e>
movb   $0x0,0xf(%rsp)
lea    0xe88(%rip),%rsi
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11cc <get_slot_number+0x7c>
mov    0x4(%rsp),%eax
jmp    11df <get_slot_number+0x8f>
lea    0xe4f(%rip),%rdi
jmp    11c5 <get_slot_number+0x75>
lea    0xe4c(%rip),%rsi
jmp    11d3 <get_slot_number+0x83>
lea    0xe50(%rip),%rdi
call   1030 <warn@plt>
jmp    11da <get_slot_number+0x8a>
lea    0xe4a(%rip),%rsi
xor    %edi,%edi
call   1060 <warnc@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"S\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [13 x i8] c"write: fail.\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [18 x i8] c"Invalid response.\00"
define i32 @get_slot_number(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [8 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @write(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %8, i32* %5
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @warn(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %34
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @warnc(i32 0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %34
17: ; preds = %12
br label %18
18: ; preds = %17
%19 = load i32, i32* %3
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 8)
store i32 %21, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @warn(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %34
25: ; preds = %18
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 7
store i8 0, i8* %26
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %6)
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @warnc(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %34
32: ; preds = %25
%33 = load i32, i32* %6
store i32 %33, i32* %2
br label %35
34: ; preds = %30, %23, %15, %10
store i32 -1, i32* %2
br label %35
35: ; preds = %34, %32
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"S\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [13 x i8] c"write: fail.\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [18 x i8] c"Invalid response.\00"
define i32 @get_slot_number(i32 %0) {
%2 = alloca [8 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @warn(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %27
10: ; preds = %1
%11 = icmp eq i32 %6, 1
br i1 %11, label %14, label %12
12: ; preds = %10
%13 = tail call i32 @warnc(i32 0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %27
14: ; preds = %10
%15 = call i32 @read(i32 %0, i8* %4, i32 8)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @warn(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %27
19: ; preds = %14
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 7
store i8 0, i8* %20
%21 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%22 = icmp eq i32 %21, 1
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = call i32 @warnc(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %27
25: ; preds = %19
%26 = load i32, i32* %3
br label %27
27: ; preds = %8, %12, %17, %23, %25
%28 = phi i32 [ %26, %25 ], [ -1, %23 ], [ -1, %17 ], [ -1, %12 ], [ -1, %8 ]
ret i32 %28
}