#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 V {
  int dummy;
};

/* Variables and functions */
struct V *bar(unsigned int, unsigned int *);
void *dummy2(void *, struct V *);
int foo(unsigned int *x, unsigned int y, void **z) {
  void *v;
  unsigned int i, j;
  *z = v = (void *)0;
  for (i = 0; i < y; i++) {
    struct V *c;
    j = *x;
    switch (j) {
    case 1:
      c = bar(j, x);
      break;
    default:
      c = 0;
      break;
    }
    if (c)
      v = dummy2(v, c);
    else
      return 1;
  }
  *z = v;
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11d0 <foo+0xb0>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
jne    1188 <foo+0x68>
jmp    1173 <foo+0x53>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <bar@plt>
mov    %rax,-0x38(%rbp)
jmp    1190 <foo+0x70>
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11b1 <foo+0x91>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <dummy2@plt>
mov    %rax,-0x28(%rbp)
jmp    11bd <foo+0x9d>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <foo+0xc2>
jmp    11c2 <foo+0xa2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    114d <foo+0x2d>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
movq   $0x0,(%rdx)
xor    %r12d,%r12d
test   %esi,%esi
je     117b <foo+0x5b>
mov    %esi,%ebp
mov    %rdi,%r15
xor    %r12d,%r12d
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
cmpl   $0x1,(%r15)
jne    1182 <foo+0x62>
mov    $0x1,%edi
mov    %r15,%rsi
call   1030 <bar@plt>
test   %rax,%rax
je     1187 <foo+0x67>
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <dummy2@plt>
mov    %rax,%r12
add    $0xffffffff,%ebp
jne    1150 <foo+0x30>
mov    %r12,(%r14)
xor    %ebx,%ebx
jmp    1187 <foo+0x67>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.V = type { i32 }
define i32 @foo(i32* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.V*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i8* null, i8** %8
%12 = load i8**, i8*** %7
store i8* null, i8** %12
store i32 0, i32* %9
br label %13
13: ; preds = %35, %3
%14 = load i32, i32* %9
%15 = load i32, i32* %6
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %38
17: ; preds = %13
%18 = load i32*, i32** %5
%19 = load i32, i32* %18
store i32 %19, i32* %10
%20 = load i32, i32* %10
switch i32 %20, label %25 [
i32 1, label %21
]
21: ; preds = %17
%22 = load i32, i32* %10
%23 = load i32*, i32** %5
%24 = call %struct.V* @bar(i32 %22, i32* %23)
store %struct.V* %24, %struct.V** %11
br label %26
25: ; preds = %17
store %struct.V* null, %struct.V** %11
br label %26
26: ; preds = %25, %21
%27 = load %struct.V*, %struct.V** %11
%28 = icmp ne %struct.V* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %8
%31 = load %struct.V*, %struct.V** %11
%32 = call i8* @dummy2(i8* %30, %struct.V* %31)
store i8* %32, i8** %8
br label %34
33: ; preds = %26
store i32 1, i32* %4
br label %41
34: ; preds = %29
br label %35
35: ; preds = %34
%36 = load i32, i32* %9
%37 = add i32 %36, 1
store i32 %37, i32* %9
br label %13
38: ; preds = %13
%39 = load i8*, i8** %8
%40 = load i8**, i8*** %7
store i8* %39, i8** %40
store i32 0, i32* %4
br label %41
41: ; preds = %38, %33
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
%struct.V = type { i32 }
define i32 @foo(i32* %0, i32 %1, i8** nocapture writeonly %2) {
store i8* null, i8** %2
%4 = icmp eq i32 %1, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %13
%6 = phi i32 [ %15, %13 ], [ 0, %3 ]
%7 = phi i8* [ %14, %13 ], [ null, %3 ]
%8 = load i32, i32* %0
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %19
10: ; preds = %5
%11 = tail call %struct.V* @bar(i32 1, i32* %0)
%12 = icmp eq %struct.V* %11, null
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = tail call i8* @dummy2(i8* %7, %struct.V* %11)
%15 = add nuw i32 %6, 1
%16 = icmp eq i32 %15, %1
br i1 %16, label %17, label %5
17: ; preds = %13, %3
%18 = phi i8* [ null, %3 ], [ %14, %13 ]
store i8* %18, i8** %2
br label %19
19: ; preds = %5, %10, %17
%20 = phi i32 [ 0, %17 ], [ 1, %10 ], [ 1, %5 ]
ret i32 %20
}