#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 termios {
  unsigned long c_iflag;
  unsigned long c_oflag;
  unsigned long c_cflag;
  unsigned long c_lflag;
  unsigned int *c_cc;
};

/* Variables and functions */
int NCCS;
int sscanf(char *, char *, ...);
int recover_mode(char *arg, struct termios *mode) {
  int i, n;
  unsigned int chr;
  unsigned long iflag, oflag, cflag, lflag;
  if (sscanf(arg, "%lx:%lx:%lx:%lx%n", &iflag, &oflag, &cflag, &lflag, &n) != 4)
    return 0;
  mode->c_iflag = iflag;
  mode->c_oflag = oflag;
  mode->c_cflag = cflag;
  mode->c_lflag = lflag;
  arg += n;
  for (i = 0; i < NCCS; ++i) {
    if (sscanf(arg, ":%x%n", &chr, &n) != 1)
      return 0;
    mode->c_cc[i] = chr;
    arg += n;
  }
  if (*arg != '\0')
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<recover_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xed5(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x40(%rbp),%r8
lea    -0x48(%rbp),%r9
lea    -0x20(%rbp),%rax
mov    %rax,(%rsp)
mov    $0x0,%al
call   1030 <sscanf@plt>
cmp    $0x4,%eax
je     115f <recover_mode+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    123b <recover_mode+0x12b>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%eax
jge    1218 <recover_mode+0x108>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     11e7 <recover_mode+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    123b <recover_mode+0x12b>
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    0x20(%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a6 <recover_mode+0x96>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1234 <recover_mode+0x124>
movl   $0x0,-0x4(%rbp)
jmp    123b <recover_mode+0x12b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recover_mode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%rax
mov    %rax,(%rsp)
lea    0xecc(%rip),%rsi
xor    %ebp,%ebp
lea    0x30(%rsp),%rdx
lea    0x28(%rsp),%rcx
lea    0x20(%rsp),%r8
lea    0x18(%rsp),%r9
xor    %eax,%eax
call   1030 <sscanf@plt>
cmp    $0x4,%eax
jne    11fc <recover_mode+0xec>
mov    0x30(%rsp),%rax
mov    %rax,(%r14)
mov    0x28(%rsp),%rax
mov    %rax,0x8(%r14)
mov    0x20(%rsp),%rax
mov    %rax,0x10(%r14)
mov    0x18(%rsp),%rax
mov    %rax,0x18(%r14)
movslq 0x10(%rsp),%rax
add    %rax,%rbx
mov    0x2e64(%rip),%rax
cmpl   $0x0,(%rax)
jle    11ef <recover_mode+0xdf>
lea    0xe7a(%rip),%r12
lea    0x14(%rsp),%r13
lea    0x10(%rsp),%rbp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    11fa <recover_mode+0xea>
mov    0x14(%rsp),%eax
mov    0x20(%r14),%rcx
mov    %eax,(%rcx,%r15,4)
movslq 0x10(%rsp),%rax
add    %rax,%rbx
add    $0x1,%r15
mov    0x2e09(%rip),%rax
movslq (%rax),%rax
cmp    %rax,%r15
jl     11b0 <recover_mode+0xa0>
xor    %ebp,%ebp
cmpb   $0x0,(%rbx)
sete   %bpl
jmp    11fc <recover_mode+0xec>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i64, i64, i64, i64, i32* }
@.str = [18 x i8] c"%lx:%lx:%lx:%lx%n\00"
@NCCS = global i32 0
@.str.1 = [6 x i8] c":%x%n\00"
define i32 @recover_mode(i8* %0, %struct.termios* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.termios*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %4
store %struct.termios* %1, %struct.termios** %5
%13 = load i8*, i8** %4
%14 = call i32 (i8*, i8*, ...) @sscanf(i8* %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64* %9, i64* %10, i64* %11, i64* %12, i32* %7)
%15 = icmp ne i32 %14, 4
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %3
br label %65
17: ; preds = %2
%18 = load i64, i64* %9
%19 = load %struct.termios*, %struct.termios** %5
%20 = getelementptr inbounds %struct.termios, %struct.termios* %19, i32 0, i32 0
store i64 %18, i64* %20
%21 = load i64, i64* %10
%22 = load %struct.termios*, %struct.termios** %5
%23 = getelementptr inbounds %struct.termios, %struct.termios* %22, i32 0, i32 1
store i64 %21, i64* %23
%24 = load i64, i64* %11
%25 = load %struct.termios*, %struct.termios** %5
%26 = getelementptr inbounds %struct.termios, %struct.termios* %25, i32 0, i32 2
store i64 %24, i64* %26
%27 = load i64, i64* %12
%28 = load %struct.termios*, %struct.termios** %5
%29 = getelementptr inbounds %struct.termios, %struct.termios* %28, i32 0, i32 3
store i64 %27, i64* %29
%30 = load i32, i32* %7
%31 = load i8*, i8** %4
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %4
store i32 0, i32* %6
br label %34
34: ; preds = %55, %17
%35 = load i32, i32* %6
%36 = load i32, i32* @NCCS
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %58
38: ; preds = %34
%39 = load i8*, i8** %4
%40 = call i32 (i8*, i8*, ...) @sscanf(i8* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %8, i32* %7)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 0, i32* %3
br label %65
43: ; preds = %38
%44 = load i32, i32* %8
%45 = load %struct.termios*, %struct.termios** %5
%46 = getelementptr inbounds %struct.termios, %struct.termios* %45, i32 0, i32 4
%47 = load i32*, i32** %46
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
store i32 %44, i32* %50
%51 = load i32, i32* %7
%52 = load i8*, i8** %4
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %4
br label %55
55: ; preds = %43
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %6
br label %34
58: ; preds = %34
%59 = load i8*, i8** %4
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58
store i32 0, i32* %3
br label %65
64: ; preds = %58
store i32 1, i32* %3
br label %65
65: ; preds = %64, %63, %42, %16
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.termios = type { i64, i64, i64, i64, i32* }
@.str = [18 x i8] c"%lx:%lx:%lx:%lx%n\00"
@NCCS = global i32 0
@.str.1 = [6 x i8] c":%x%n\00"
define i32 @recover_mode(i8* nocapture %0, %struct.termios* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i64* %5 to i8*
%12 = bitcast i64* %6 to i8*
%13 = bitcast i64* %7 to i8*
%14 = bitcast i64* %8 to i8*
%15 = call i32 (i8*, i8*, ...) @sscanf(i8* %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64* %5, i64* %6, i64* %7, i64* %8, i32* %3)
%16 = icmp eq i32 %15, 4
br i1 %16, label %17, label %54
17: ; preds = %2
%18 = load i64, i64* %5
%19 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
store i64 %18, i64* %19
%20 = load i64, i64* %6
%21 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
store i64 %20, i64* %21
%22 = load i64, i64* %7
%23 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 2
store i64 %22, i64* %23
%24 = load i64, i64* %8
%25 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
store i64 %24, i64* %25
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i32, i32* @NCCS
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %49
31: ; preds = %17
%32 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 4
br label %33
33: ; preds = %31, %38
%34 = phi i64 [ 0, %31 ], [ %45, %38 ]
%35 = phi i8* [ %28, %31 ], [ %44, %38 ]
%36 = call i32 (i8*, i8*, ...) @sscanf(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %3)
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %54
38: ; preds = %33
%39 = load i32, i32* %4
%40 = load i32*, i32** %32
%41 = getelementptr inbounds i32, i32* %40, i64 %34
store i32 %39, i32* %41
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %35, i64 %43
%45 = add nuw nsw i64 %34, 1
%46 = load i32, i32* @NCCS
%47 = sext i32 %46 to i64
%48 = icmp slt i64 %45, %47
br i1 %48, label %33, label %49
49: ; preds = %38, %17
%50 = phi i8* [ %28, %17 ], [ %44, %38 ]
%51 = load i8, i8* %50
%52 = icmp eq i8 %51, 0
%53 = zext i1 %52 to i32
br label %54
54: ; preds = %33, %49, %2
%55 = phi i32 [ 0, %2 ], [ %53, %49 ], [ 0, %33 ]
ret i32 %55
}