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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int lex_mode(char *, int);
int oct_mode(char *, int);
char *strdup(char *);
char *strtok(char *, char *);
int bake_mode(char *str, mode_t *ret) {
  char *modestr = strdup(str);
  str = strtok(modestr, ",");
  do {
    int mode = lex_mode(str, *ret);
    if (mode < 0)
      mode = oct_mode(str, *ret);
    if (mode < 0) {
      free(modestr);
      return -1;
    }
    *ret = mode;
  } while ((str = strtok(NULL, ",")) != NULL);
  free(modestr);
  return 0;
}


x86 assembly (clang, -O0):
<bake_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1050 <lex_mode@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11af <bake_mode+0x5f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1030 <oct_mode@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ce <bake_mode+0x7e>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <bake_mode+0xb5>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe1e(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1181 <bake_mode+0x31>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bake_mode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
call   1070 <strdup@plt>
mov    %rax,%r14
lea    0xe95(%rip),%r15
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
jmp    1199 <bake_mode+0x49>
nopl   0x0(%rax,%rax,1)
mov    %eax,(%r12)
xor    %r13d,%r13d
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11bf <bake_mode+0x6f>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1050 <lex_mode@plt>
test   %eax,%eax
jns    1180 <bake_mode+0x30>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1030 <oct_mode@plt>
test   %eax,%eax
jns    1180 <bake_mode+0x30>
mov    $0xffffffff,%r13d
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r13d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
define i32 @bake_mode(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = call i8* @strtok(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
br label %12
12: ; preds = %33, %2
%13 = load i8*, i8** %4
%14 = load i32*, i32** %5
%15 = load i32, i32* %14
%16 = call i32 @lex_mode(i8* %13, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = load i32*, i32** %5
%22 = load i32, i32* %21
%23 = call i32 @oct_mode(i8* %20, i32 %22)
store i32 %23, i32* %7
br label %24
24: ; preds = %19, %12
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %6
%29 = call i32 @free(i8* %28)
store i32 -1, i32* %3
br label %39
30: ; preds = %24
%31 = load i32, i32* %7
%32 = load i32*, i32** %5
store i32 %31, i32* %32
br label %33
33: ; preds = %30
%34 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %34, i8** %4
%35 = icmp ne i8* %34, null
br i1 %35, label %12, label %36
36: ; preds = %33
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
store i32 0, i32* %3
br label %39
39: ; preds = %36, %27
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c",\00"
define i32 @bake_mode(i8* nocapture %0, i32* nocapture %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %14, %2
%6 = phi i8* [ %4, %2 ], [ %16, %14 ]
%7 = load i32, i32* %1
%8 = tail call i32 @lex_mode(i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i32, i32* %1
%12 = tail call i32 @oct_mode(i8* %6, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %5, %10
%15 = phi i32 [ %12, %10 ], [ %8, %5 ]
store i32 %15, i32* %1
%16 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %5
18: ; preds = %14, %10
%19 = phi i32 [ -1, %10 ], [ 0, %14 ]
%20 = tail call i32 @free(i8* %3)
ret i32 %19
}