#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 */
scalar_t__ ERANGE;
int PATH_MAX;
scalar_t__ errno;
int /*<<< orphan*/ *getcwd(char *, int);
char *smm_alloc(int);
int /*<<< orphan*/ smm_errno_update(int);
int /*<<< orphan*/ smm_free(char *);
char *smm_cwd_alloc(int extra) {
  char *path;
  int i, len = PATH_MAX;
  for (i = 0; i < 8; i++) {
    if ((path = smm_alloc(len + extra)) == NULL) {
      smm_errno_update((0xC0000000 | (1)));
      return NULL;
    }
    if (getcwd(path, len) != NULL) {
      return path;
    }
    smm_free(path);
    if (errno != ERANGE) {
      smm_errno_update((0xC0000000 | (6)));
      return NULL;
    }
    len <<= 1;
  }
  smm_errno_update((0xC0000000 | (1)));
  return NULL;
}


x86 assembly (clang, -O0):
<smm_cwd_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    120c <smm_cwd_alloc+0xcc>
mov    -0x20(%rbp),%edi
add    -0xc(%rbp),%edi
call   1040 <smm_alloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1198 <smm_cwd_alloc+0x58>
mov    $0xc0000001,%edi
call   1050 <smm_errno_update@plt>
movq   $0x0,-0x8(%rbp)
jmp    121e <smm_cwd_alloc+0xde>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <getcwd@plt>
cmp    $0x0,%rax
je     11bb <smm_cwd_alloc+0x7b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121e <smm_cwd_alloc+0xde>
mov    -0x18(%rbp),%rdi
call   1060 <smm_free@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <smm_cwd_alloc+0xb5>
mov    $0xc0000006,%edi
call   1050 <smm_errno_update@plt>
movq   $0x0,-0x8(%rbp)
jmp    121e <smm_cwd_alloc+0xde>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115e <smm_cwd_alloc+0x1e>
mov    $0xc0000001,%edi
call   1050 <smm_errno_update@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smm_cwd_alloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
mov    0x2e9b(%rip),%rax
mov    (%rax),%ebx
lea    (%rbx,%r15,1),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x2e42(%rip),%r13
mov    0x0(%r13),%rax
mov    0x2e47(%rip),%r12
mov    $0xc0000006,%r14d
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
lea    (%r15,%rbx,2),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
lea    (%rbx,%rbx,1),%esi
mov    %rax,%rdi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
lea    (%r15,%rbx,4),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
lea    0x0(,%rbx,4),%esi
mov    %rax,%rdi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
lea    (%r15,%rbx,8),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
lea    0x0(,%rbx,8),%esi
mov    %rax,%rdi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
mov    %ebx,%eax
shl    $0x4,%eax
mov    %rax,(%rsp)
lea    (%rax,%r15,1),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    (%rsp),%rsi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
mov    %ebx,%eax
shl    $0x5,%eax
mov    %rax,(%rsp)
lea    (%rax,%r15,1),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    (%rsp),%rsi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
mov    %ebx,%eax
shl    $0x6,%eax
mov    %rax,(%rsp)
lea    (%rax,%r15,1),%edi
call   1040 <smm_alloc@plt>
test   %rax,%rax
je     138a <smm_cwd_alloc+0x24a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    (%rsp),%rsi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1390 <smm_cwd_alloc+0x250>
shl    $0x7,%ebx
add    %ebx,%r15d
mov    %r15d,%edi
call   1040 <smm_alloc@plt>
mov    $0xc0000001,%r14d
test   %rax,%rax
je     1390 <smm_cwd_alloc+0x250>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    139a <smm_cwd_alloc+0x25a>
mov    %rbp,%rdi
call   1060 <smm_free@plt>
mov    0x0(%r13),%rax
xor    %ecx,%ecx
cmp    (%r12),%rax
setne  %cl
lea    (%rcx,%rcx,4),%r14d
add    $0xc0000001,%r14d
jmp    1390 <smm_cwd_alloc+0x250>
mov    $0xc0000001,%r14d
mov    %r14d,%edi
call   1050 <smm_errno_update@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
define i8* @smm_cwd_alloc(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @PATH_MAX
store i32 %7, i32* %6
store i32 0, i32* %5
br label %8
8: ; preds = %37, %1
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 8
br i1 %10, label %11, label %40
11: ; preds = %8
%12 = load i32, i32* %6
%13 = load i32, i32* %3
%14 = add nsw i32 %12, %13
%15 = call i8* @smm_alloc(i32 %14)
store i8* %15, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @smm_errno_update(i32 -1073741823)
store i8* null, i8** %2
br label %42
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = load i32, i32* %6
%22 = call i32* @getcwd(i8* %20, i32 %21)
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i8*, i8** %4
store i8* %25, i8** %2
br label %42
26: ; preds = %19
%27 = load i8*, i8** %4
%28 = call i32 @smm_free(i8* %27)
%29 = load i64, i64* @errno
%30 = load i64, i64* @ERANGE
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @smm_errno_update(i32 -1073741818)
store i8* null, i8** %2
br label %42
34: ; preds = %26
%35 = load i32, i32* %6
%36 = shl i32 %35, 1
store i32 %36, i32* %6
br label %37
37: ; preds = %34
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %8
40: ; preds = %8
%41 = call i32 @smm_errno_update(i32 -1073741823)
store i8* null, i8** %2
br label %42
42: ; preds = %40, %32, %24, %17
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
define i8* @smm_cwd_alloc(i32 %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, %0
%4 = tail call i8* @smm_alloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %106, label %6
6: ; preds = %1
%7 = tail call i32* @getcwd(i8* %4, i32 %2)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %109
9: ; preds = %6
%10 = tail call i32 @smm_free(i8* %4)
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %106
14: ; preds = %9
%15 = shl i32 %2, 1
%16 = add nsw i32 %15, %0
%17 = tail call i8* @smm_alloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %106, label %19
19: ; preds = %14
%20 = tail call i32* @getcwd(i8* %17, i32 %15)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %109
22: ; preds = %19
%23 = tail call i32 @smm_free(i8* %17)
%24 = load i64, i64* @errno
%25 = load i64, i64* @ERANGE
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %106
27: ; preds = %22
%28 = shl i32 %2, 2
%29 = add nsw i32 %28, %0
%30 = tail call i8* @smm_alloc(i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %106, label %32
32: ; preds = %27
%33 = tail call i32* @getcwd(i8* %30, i32 %28)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %109
35: ; preds = %32
%36 = tail call i32 @smm_free(i8* %30)
%37 = load i64, i64* @errno
%38 = load i64, i64* @ERANGE
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %106
40: ; preds = %35
%41 = shl i32 %2, 3
%42 = add nsw i32 %41, %0
%43 = tail call i8* @smm_alloc(i32 %42)
%44 = icmp eq i8* %43, null
br i1 %44, label %106, label %45
45: ; preds = %40
%46 = tail call i32* @getcwd(i8* %43, i32 %41)
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %109
48: ; preds = %45
%49 = tail call i32 @smm_free(i8* %43)
%50 = load i64, i64* @errno
%51 = load i64, i64* @ERANGE
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %106
53: ; preds = %48
%54 = shl i32 %2, 4
%55 = add nsw i32 %54, %0
%56 = tail call i8* @smm_alloc(i32 %55)
%57 = icmp eq i8* %56, null
br i1 %57, label %106, label %58
58: ; preds = %53
%59 = tail call i32* @getcwd(i8* %56, i32 %54)
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %109
61: ; preds = %58
%62 = tail call i32 @smm_free(i8* %56)
%63 = load i64, i64* @errno
%64 = load i64, i64* @ERANGE
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %106
66: ; preds = %61
%67 = shl i32 %2, 5
%68 = add nsw i32 %67, %0
%69 = tail call i8* @smm_alloc(i32 %68)
%70 = icmp eq i8* %69, null
br i1 %70, label %106, label %71
71: ; preds = %66
%72 = tail call i32* @getcwd(i8* %69, i32 %67)
%73 = icmp eq i32* %72, null
br i1 %73, label %74, label %109
74: ; preds = %71
%75 = tail call i32 @smm_free(i8* %69)
%76 = load i64, i64* @errno
%77 = load i64, i64* @ERANGE
%78 = icmp eq i64 %76, %77
br i1 %78, label %79, label %106
79: ; preds = %74
%80 = shl i32 %2, 6
%81 = add nsw i32 %80, %0
%82 = tail call i8* @smm_alloc(i32 %81)
%83 = icmp eq i8* %82, null
br i1 %83, label %106, label %84
84: ; preds = %79
%85 = tail call i32* @getcwd(i8* %82, i32 %80)
%86 = icmp eq i32* %85, null
br i1 %86, label %87, label %109
87: ; preds = %84
%88 = tail call i32 @smm_free(i8* %82)
%89 = load i64, i64* @errno
%90 = load i64, i64* @ERANGE
%91 = icmp eq i64 %89, %90
br i1 %91, label %92, label %106
92: ; preds = %87
%93 = shl i32 %2, 7
%94 = add nsw i32 %93, %0
%95 = tail call i8* @smm_alloc(i32 %94)
%96 = icmp eq i8* %95, null
br i1 %96, label %106, label %97
97: ; preds = %92
%98 = tail call i32* @getcwd(i8* %95, i32 %93)
%99 = icmp eq i32* %98, null
br i1 %99, label %100, label %109
100: ; preds = %97
%101 = tail call i32 @smm_free(i8* %95)
%102 = load i64, i64* @errno
%103 = load i64, i64* @ERANGE
%104 = icmp eq i64 %102, %103
%105 = select i1 %104, i32 -1073741823, i32 -1073741818
br label %106
106: ; preds = %100, %9, %22, %35, %48, %61, %74, %87, %1, %14, %27, %40, %53, %66, %79, %92
%107 = phi i32 [ -1073741823, %92 ], [ -1073741823, %79 ], [ -1073741823, %66 ], [ -1073741823, %53 ], [ -1073741823, %40 ], [ -1073741823, %27 ], [ -1073741823, %14 ], [ -1073741823, %1 ], [ -1073741818, %87 ], [ -1073741818, %74 ], [ -1073741818, %61 ], [ -1073741818, %48 ], [ -1073741818, %35 ], [ -1073741818, %22 ], [ -1073741818, %9 ], [ %105, %100 ]
%108 = tail call i32 @smm_errno_update(i32 %107)
br label %109
109: ; preds = %106, %6, %19, %32, %45, %58, %71, %84, %97
%110 = phi i8* [ %4, %6 ], [ %17, %19 ], [ %30, %32 ], [ %43, %45 ], [ %56, %58 ], [ %69, %71 ], [ %82, %84 ], [ %95, %97 ], [ null, %106 ]
ret i8* %110
}