int countDown(void **context, int n) {
  struct __ctx {
    long __s;
    int i;
  };
  struct __ctx *ctx;
  if (!*context) {
    *context = malloc(sizeof(struct __ctx));
    (*(struct __ctx *)(*context)) = (struct __ctx){.__s = 0, n};
  };
  ctx = (struct __ctx *)*context;
  long *__state__ = &ctx->__s;
  switch (*__state__) {
  case 0:;
    while (ctx->i > 0) {
      *__state__ = 49;
      return ctx->i--;
    case 49:;
    };
    free(*context);
    return 0;
  };
  ;
}


x86 assembly (clang, -O0):
<countDown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1175 <countDown+0x55>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,-0x28(%rbp)
mov    -0x30(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
test   %rax,%rax
je     11b4 <countDown+0x94>
jmp    11a1 <countDown+0x81>
mov    -0x40(%rbp),%rax
sub    $0x31,%rax
je     11e9 <countDown+0xc9>
jmp    1201 <countDown+0xe1>
jmp    11b9 <countDown+0x99>
mov    -0x20(%rbp),%rax
cmpl   $0x0,0x8(%rax)
jle    11ee <countDown+0xce>
mov    -0x38(%rbp),%rax
movq   $0x31,(%rax)
mov    -0x20(%rbp),%rcx
mov    0x8(%rcx),%eax
mov    %eax,%edx
add    $0xffffffff,%edx
mov    %edx,0x8(%rcx)
mov    %eax,-0x4(%rbp)
jmp    1201 <countDown+0xe1>
jmp    11b9 <countDown+0x99>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countDown>:
push   %rbp
push   %rbx
push   %rax
mov    (%rdi),%rax
test   %rax,%rax
je     113b <countDown+0x1b>
mov    (%rax),%rdx
cmp    $0x31,%rdx
je     1157 <countDown+0x37>
test   %rdx,%rdx
je     1157 <countDown+0x37>
jmp    1177 <countDown+0x57>
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,(%rbx)
movq   $0x0,(%rax)
mov    %ebp,0x8(%rax)
mov    0x8(%rax),%ecx
test   %ecx,%ecx
jle    116d <countDown+0x4d>
movq   $0x31,(%rax)
lea    -0x1(%rcx),%edx
mov    %edx,0x8(%rax)
jmp    1177 <countDown+0x57>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.__ctx = type { i64, i32 }
define i32 @countDown(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca %struct.__ctx*
%7 = alloca %struct.__ctx
%8 = alloca i64*
store i8** %0, i8*** %4
store i32 %1, i32* %5
%9 = load i8**, i8*** %4
%10 = load i8*, i8** %9
%11 = icmp ne i8* %10, null
br i1 %11, label %23, label %12
12: ; preds = %2
%13 = call i8* @malloc(i64 16)
%14 = load i8**, i8*** %4
store i8* %13, i8** %14
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = bitcast i8* %16 to %struct.__ctx*
%18 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %7, i32 0, i32 0
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %7, i32 0, i32 1
%20 = load i32, i32* %5
store i32 %20, i32* %19
%21 = bitcast %struct.__ctx* %17 to i8*
%22 = bitcast %struct.__ctx* %7 to i8*
br label %23
23: ; preds = %12, %2
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = bitcast i8* %25 to %struct.__ctx*
store %struct.__ctx* %26, %struct.__ctx** %6
%27 = load %struct.__ctx*, %struct.__ctx** %6
%28 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %27, i32 0, i32 0
store i64* %28, i64** %8
%29 = load i64*, i64** %8
%30 = load i64, i64* %29
switch i64 %30, label %47 [
i64 0, label %31
i64 49, label %43
]
31: ; preds = %23
br label %32
32: ; preds = %43, %31
%33 = load %struct.__ctx*, %struct.__ctx** %6
%34 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i64*, i64** %8
store i64 49, i64* %38
%39 = load %struct.__ctx*, %struct.__ctx** %6
%40 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = add nsw i32 %41, -1
store i32 %42, i32* %40
store i32 %41, i32* %3
br label %47
43: ; preds = %23
br label %32
44: ; preds = %32
%45 = load i8**, i8*** %4
%46 = load i8*, i8** %45
store i32 0, i32* %3
br label %47
47: ; preds = %37, %44, %23
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.__ctx = type { i64, i32 }
define i32 @countDown(i8** nocapture %0, i32 %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = tail call dereferenceable_or_null(16) i8* @malloc(i64 16)
store i8* %6, i8** %0
%7 = bitcast i8* %6 to %struct.__ctx*
%8 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %7, i64 0, i32 0
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %7, i64 0, i32 1
store i32 %1, i32* %9
%10 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %7, i64 0, i32 0
br label %16
11: ; preds = %2
%12 = bitcast i8* %3 to %struct.__ctx*
%13 = bitcast i8* %3 to i64*
%14 = load i64, i64* %13
%15 = bitcast i8* %3 to i64*
switch i64 %14, label %26 [
i64 0, label %16
i64 49, label %16
]
16: ; preds = %5, %11, %11
%17 = phi i64* [ %10, %5 ], [ %15, %11 ], [ %15, %11 ]
%18 = phi %struct.__ctx* [ %7, %5 ], [ %12, %11 ], [ %12, %11 ]
%19 = phi i8* [ %6, %5 ], [ %3, %11 ], [ %3, %11 ]
%20 = getelementptr inbounds %struct.__ctx, %struct.__ctx* %18, i64 0, i32 1
%21 = load i32, i32* %20
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
store i64 49, i64* %17
%24 = add nsw i32 %21, -1
store i32 %24, i32* %20
br label %26
25: ; preds = %16
tail call void @free(i8* %19)
br label %26
26: ; preds = %11, %25, %23
%27 = phi i32 [ %21, %23 ], [ 0, %25 ], [ undef, %11 ]
ret i32 %27
}